我尝试在openCV示例代码“ColorBlobDetection”中使用阈值。但是我发现了一些错误。

错误。

11-13 20:10:03.798  28032-30391/app.heng.opencv E/cv::error()﹕ OpenCV Error: Assertion failed (src.type() == CV_8UC1) in double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp, line 719
11-13 20:10:03.828  28032-30391/app.heng.opencv E/org.opencv.imgproc﹕ imgproc::threshold_10() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp:719: error: (-215) src.type() == CV_8UC1 in function double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int)
11-13 20:10:03.858  28032-30391/app.heng.opencv E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-10693
CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp:719: error: (-215) src.type() == CV_8UC1 in function double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int)
]
        at org.opencv.imgproc.Imgproc.threshold_0(Native Method)
        at org.opencv.imgproc.Imgproc.threshold(Imgproc.java:9180)
        at app.heng.opencv.ColorBlobDetectionActivity.onCameraFrame(ColorBlobDetectionActivity.java:241)
        at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
        at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
        at java.lang.Thread.run(Thread.java:841)

在此使用阈值之前的示例图片。
android - 如何在CameraFrame Android + OpenCV上使用阈值-LMLPHP

这是我想要的使用阈值后的示例图片。
android - 如何在CameraFrame Android + OpenCV上使用阈值-LMLPHP

openCV示例代码
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();  // mRbga = input frame with color
    MatOfPoint2f  approxCurve = new MatOfPoint2f();

    if (mIsColorSelected) {
        mDetector.process(mRgba);
        final List<MatOfPoint> contours = mDetector.getContours();  //contour info is ready in detector
        Log.e(TAG, "Contours count: " + contours.size());

        Core.putText(mRgba, ""+contours.size(), new Point(mRgba.cols()/2, mRgba.rows()/2), 3, 1, new Scalar(255, 255, 255, 255), 2);

        //green contous and size=5
        Imgproc.drawContours(mRgba, contours, -1, new Scalar(0, 255, 0), 3);



        Imgproc.threshold(mRgba, mRgba, 0, 255, Imgproc.THRESH_OTSU);



        Mat colorLabel = mRgba.submat(4, 68, 4, 68);
        colorLabel.setTo(mBlobColorRgba);
        Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
        mSpectrum.copyTo(spectrumLabel);
    }

    return mRgba;
}

这是原始源代码Here

最佳答案

正如docs所说:



您需要将mRgba转换为单通道Mat

cvtColor:Imgproc.cvtColor(input, output, Imgproc.COLOR_BGRA2GRAY, 1);

关于android - 如何在CameraFrame Android + OpenCV上使用阈值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33693531/

10-10 10:14