我想在openCV中将反转图像转换为灰度,我使用this method来反转BGR图像,但是我得到了关于Imgproc.cvtColor方法的错误。
这是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
及以下是我用于灰度图像的代码:
//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
我的代码中有错误吗? 非常感谢你
答案 0 :(得分:3)
你不需要手动反转图像,有一个标志用于阈值处理,它会自动执行:
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);
但你的错误信息抱怨说,cvtColor的输入既没有3个也没有4个通道,所以它是灰度,或者是一个空的/无效的img。