首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Opencv (Android)中绘制矩形轮廓线?

如何在Opencv (Android)中绘制矩形轮廓线?
EN

Stack Overflow用户
提问于 2016-04-27 06:47:01
回答 1查看 9.3K关注 0票数 3

我想在轮廓线周围画长方形。我在Python中做得很好,但是当我将代码转换为android时,应用程序每次都会崩溃。

代码语言:javascript
运行
复制
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);

误差

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2016-06-23 11:02:19

代码语言:javascript
运行
复制
 @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;
}

尝试一下,它对我非常有效(如果有任何错误,根据opencv库版本编写代码)。core.rectangle像这样变成了Imgproc.rectangle

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36882329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档