Reputation: 11
I want to convert an inverted image to grayscale in openCV, i used with this method to inverting BGR image, but i get an Error regarding the Imgproc.cvtColor method.
This is the logcat :
01-06 13:43:01.085: E/cv::error()(8837): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3737
01-06 13:43:01.095: E/org.opencv.imgproc(8837): imgproc::cvtColor_11() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
01-06 13:43:01.095: E/AndroidRuntime(8837): FATAL EXCEPTION: main
01-06 13:43:01.095: E/AndroidRuntime(8837): Process: com.example.irisrecog, PID: 8837
01-06 13:43:01.095: E/AndroidRuntime(8837): java.lang.RuntimeException: Unable to resume activity {com.example.irisrecog/com.example.irisrecog.MainActivity}: CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
01-06 13:43:01.095: E/AndroidRuntime(8837): ]
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2850)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2879)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.os.Handler.dispatchMessage(Handler.java:102)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.os.Looper.loop(Looper.java:212)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.ActivityThread.main(ActivityThread.java:5151)
01-06 13:43:01.095: E/AndroidRuntime(8837): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 13:43:01.095: E/AndroidRuntime(8837): at java.lang.reflect.Method.invoke(Method.java:515)
01-06 13:43:01.095: E/AndroidRuntime(8837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
01-06 13:43:01.095: E/AndroidRuntime(8837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
01-06 13:43:01.095: E/AndroidRuntime(8837): at dalvik.system.NativeStart.main(Native Method)
01-06 13:43:01.095: E/AndroidRuntime(8837): Caused by: CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
01-06 13:43:01.095: E/AndroidRuntime(8837): ]
01-06 13:43:01.095: E/AndroidRuntime(8837): at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
01-06 13:43:01.095: E/AndroidRuntime(8837): at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4598)
01-06 13:43:01.095: E/AndroidRuntime(8837): at com.example.irisrecog.MainActivity.segmentasi(MainActivity.java:240)
01-06 13:43:01.095: E/AndroidRuntime(8837): at com.example.irisrecog.MainActivity.deteksiIrisMata(MainActivity.java:283)
01-06 13:43:01.095: E/AndroidRuntime(8837): at com.example.irisrecog.MainActivity.onResume(MainActivity.java:135)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.Activity.performResume(Activity.java:5310)
01-06 13:43:01.095: E/AndroidRuntime(8837): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2840)
01-06 13:43:01.095: E/AndroidRuntime(8837): ... 10 more
and below is my code for grayscaling an inverted image :
//invert matrix - bgrFrame
Mat invertedMat = new Mat(bgrFrame.rows(), bgrFrame.cols(), bgrFrame.type());
Mat invertColorMat = new Mat(bgrFrame.rows(), bgrFrame.cols(), bgrFrame.type(), new Scalar(255,255,255));
Core.subtract(invertColorMat, bgrFrame, invertedMat);
//grayscaling inverted matrix
Mat grayscaledMat = new Mat();
Imgproc.cvtColor(invertedMat, grayscaledMat, Imgproc.COLOR_BGR2GRAY); // this line caused an error
Is there any error in my code? Thank you very much
Upvotes: 0
Views: 4828
Reputation: 39796
you don't need to invert the image manually, there's a flag for the thresholding, that does that automatically:
Mat grayscaledMat = new Mat();
Imgproc.cvtColor(bgrFrame, grayscaledMat, Imgproc.COLOR_BGR2GRAY);
Mat thresh = new Mat();
Imgproc.threshold(grayscaledMat, thresh, 30, 255, Imgproc.THRESH_BINARY_INV);
but your error-msg complains, that the input to cvtColor had neither 3 nor 4 channels, so it was either a grayscale, or an total empty/invalid img.
Upvotes: 3