如何在Opencv(Android)中绘制轮廓周围的矩形?

时间:2016-04-27 06:47:02

标签: android opencv opencv4android

我想在轮廓周围绘制矩形。我在Python中完美地完成了它但是当我将该代码翻译成android时,应用程序每次都会崩溃。

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);

错误

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 个答案:

答案 0 :(得分:5)

modelFromCollection.decrease()

试试这个对我来说很有效(如果有任何错误,根据opencv库版本的代码)。 core.rectangle像这样改为Imgproc.rectangle

相关问题