Ask Your Question

Junru's profile - activity

2017-08-17 00:33:27 -0600 received badge  Enthusiast
2017-08-10 23:04:05 -0600 received badge  Editor (source)
2017-08-10 22:17:12 -0600 asked a question OpenCV error (-215)

Hi, current I get this error when I detect a rectangle

08-11 11:12:02.161 17423-20740/com.emedicare E/cv::error(): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv, jclass, jlong, jobject, jboolean), file /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97 08-11 11:12:02.161 17423-20740/com.emedicare E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv, jclass, jlong, jobject, jboolean) 08-11 11:12:02.161 17423-20740/com.emedicare E/CameraBridge: Mat type: Mat [ 00CV_8UC4, isCont=true, isSubmat=false, nativeObj=0xffffffffedfbd240, dataAddr=0x0 ] 08-11 11:12:02.161 17423-20740/com.emedicare E/CameraBridge: Bitmap type: 19201080 08-11 11:12:02.161 17423-20740/com.emedicare E/CameraBridge: Utils.matToBitmap() throws an exception: /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv, jclass, jlong, jobject, jboolean)

May I know how to solve it?

My current code is

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();

    Imgproc.pyrDown(mRgba, downscaled, new Size(mRgba.cols() / 2, mRgba.rows() / 2));
    Imgproc.pyrUp(downscaled, upscaled, mRgba.size());
    Imgproc.Canny(upscaled, bw, 0, 255);
    Imgproc.dilate(bw, bw, new Mat(), new Point(-1, 1), 1);

    List<MatOfPoint> contours = new ArrayList<>();
    contourImage = bw.clone();
    Imgproc.findContours(contourImage, contours, hierarchyOutputVector, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    for (MatOfPoint cnt : contours) {
        MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());
        Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);
        int numberVertices = (int) approxCurve.total();
        double contourArea = Imgproc.contourArea(cnt);
        if (Math.abs(contourArea) < 100)
            continue;

        if (numberVertices >= 4 && numberVertices <= 6) {
            List<Double> cos = new ArrayList<>();
            for (int j = 2; j < numberVertices + 1; j++)
                cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
            Collections.sort(cos);
            double mincos = cos.get(0);
            double maxcos = cos.get(cos.size() - 1);

            if (numberVertices == 4 && mincos >= -0.1 && maxcos <= 0.3) {
                if (DISPLAY_IMAGES) {
                    doSomethingWithContent("rectangle");
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imgRect.setImageResource(R.drawable.abc_camera_blue_support);
                        }
                    });
                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imgRect.setImageResource(R.drawable.abc_camera_red_support);
                    }
                });
            }
        }
    }

    return mRgba;
}

And this code is in click event

Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
                            Utils.matToBitmap(mRgba, bmp);

                            mRgba.release();
2017-08-10 07:33:02 -0600 received badge  Scholar (source)
2017-08-10 07:32:55 -0600 commented answer Photo capturing without imgproc rectangle

Hi, thank you for your helping. I get the output. :)

2017-08-09 23:45:00 -0600 asked a question Photo capturing without imgproc rectangle

image description

As the image shown the pink rectangle support box is in the photo. May I know that is possible to remove the rectangle box after I capture the image?

This is my onCameraFrame code

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    Mat mRgba2 = inputFrame.rgba();

    Point point = new Point((double) (mRgba2.width() / 4), (double) (mRgba2.height() / 5));
    Point point2 = new Point((double) (mRgba2.width() / 4 * 3), (double) (mRgba2.height() / 5 * 4));
    Imgproc.rectangle(mRgba2, point, point2, new Scalar(255, 0, 255, 255), 4);
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    if (this.mIsColorSelected) {
        this.mColorDetector.process(mRgba2);
        List<MatOfPoint> contours = this.mColorDetector.getContours();
        for (int i = 0; i < contours.size(); i++) {
            MatOfPoint2f contour2f = new MatOfPoint2f((contours.get(i)).toArray());
            Imgproc.approxPolyDP(contour2f, approxCurve, Imgproc.arcLength(contour2f, true) * 0.02, true);
            Rect rect = Imgproc.boundingRect(new MatOfPoint(approxCurve.toArray()));

            if (rect.tl().y <= point.y || rect.tl().y >= point2.y || rect.tl().x <= point.x || rect.tl().x >= point2.x || rect.br().y <= point.y || rect.br().y >= point2.y || rect.br().x <= point.x || rect.br().x >= point2.x) {
                Imgproc.rectangle(mRgba2, rect.tl(), rect.br(), this.CONTOUR_COLOR, 3, 8, 0);
            } else {
                Imgproc.putText(mRgba2, "Captured!", new Point(rect.tl().x, rect.tl().y - 10), 1, 2, new Scalar(255, 0, 0, 255), 2);
                Imgproc.rectangle(mRgba2, rect.tl(), rect.br(), new Scalar(255, 0, 0, 255), 3, 8, 0);
            }
        }
    }

    return mRgba;
}