3

I want to draw rectangle around contours. I did it perfectly in Python but when I translate that code into android, application gets crashed every time.

ImageView im = (ImageView) findViewById(R.id.sampleImageView);
// Bitmap bitmap =  ((BitmapDrawable) im.getDrawable()).getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.room);

Mat src = new Mat();
Utils.bitmapToMat(bitmap, src);
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGBA2GRAY);
Imgproc.Canny(gray, gray, 50, 200);
Imgproc.threshold(gray, gray, 10, 255, Imgproc.THRESH_OTSU);

List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
Imgproc.drawContours(src, contours, -1, new Scalar(0, 0, 255), -1);

/********************************/
MatOfPoint2f approxCurve = new MatOfPoint2f();

// For each contour found
for (int i = 0; i < contours.size(); i++)
{
    //Convert contours(i) from MatOfPoint to MatOfPoint2f
    MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
    //Processing on mMOP2f1 which is in type MatOfPoint2f
    double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;
    Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

    // Convert back to MatOfPoint
    MatOfPoint points = new MatOfPoint(approxCurve.toArray());

    // Get bounding rect of contour
    Rect rect = Imgproc.boundingRect(points);

    // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
    Imgproc.rectangle(src, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(255,0,255), 3);
}


/*******************************/
Utils.matToBitmap(src, bitmap);
im.setImageBitmap(bitmap);

Error

No implementation found for void org.opencv.imgproc.Imgproc.rectangle_1(long, double, double, double, double, double, double, double, double, int) (tried Java_org_opencv_imgproc_Imgproc_rectangle_11 and Java_org_opencv_imgproc_Imgproc_rectangle_11__JDDDDDDDDI)
04-27 11:38:30.655 12221-12221/com.objectdetection.mrawan.objectdetection D/AndroidRuntime: Shutting down VM
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime: FATAL EXCEPTION: main
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime: Process: com.objectdetection.mrawan.objectdetection, PID: 12221
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.view.View$1.onClick(View.java:4096)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.view.View.performClick(View.java:4856)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19956)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:211)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5371)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
04-27 11:38:30.665 12221-12221/com.objectdetection.mrawan.objectdetection E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)

1 Answer 1

9
 @Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    contours = new ArrayList<MatOfPoint>();
    hierarchy = new Mat();
    Imgproc.GaussianBlur(mRgba,mIntermediateMat,new Size(9,9),2,2);
    Imgproc.Canny(mRgba, mIntermediateMat, 80, 100);
    Imgproc.findContours(mIntermediateMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
/* Mat drawing = Mat.zeros( mIntermediateMat.size(), CvType.CV_8UC3 );
 for( int i = 0; i< contours.size(); i++ )
 {
Scalar color =new Scalar(Math.random()*255, Math.random()*255, Math.random()*255);
 Imgproc.drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, new Point() );
 }*/
    hierarchy.release();
            // Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4)
/* Mat drawing = Mat.zeros( mIntermediateMat.size(), CvType.CV_8UC3 );
 for( int i = 0; i< contours.size(); i++ )
 {
Scalar color =new Scalar(Math.random()*255, Math.random()*255, Math.random()*255);
 Imgproc.drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, new Point() );
 }*/
    for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
    {
        // Minimum size allowed for consideration
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(contourIdx).toArray() );
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);

            Core.rectangle(mRgba, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255), 3);



    }
    return mRgba;
}

Try this it worked perfectly for me (if any errors, code according to the opencv library version). core.rectangle changed to Imgproc.rectangle like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.