首页 文章

如何使用opecv在android中的边界矩形周围设置感兴趣区域

提问于
浏览
1

我在图像中检测到了轮廓 . 检测到轮廓后,我根据区域过滤轮廓 . 然后我在牌照周围绘制了一个边界矩形,只围绕一个轮廓 .

如何裁剪剩余的图像,只获得绘制的矩形区域,也就是说,我只想获得一个牌照,我已经绘制了一个矩形 . 我没有矩形坐标 . 我只是在opencv中使用Core.rectangle()函数来绘制一个矩形 .

谁能帮我这个 . 请在android opencv中提供代码,使用该代码可以获得所需的结果 . 我还附上了一张图片 . Here I have drawn rectangle using Core.rectangle(). I want to get the rectangle part and crop other part.

1 回答

  • -1
    List<MatOfPoint> contour = new ArrayList<MatOfPoint>();
                    Mat hierarch = new Mat();
                    //find contours
                    Imgproc.findContours(gray1, contour, hierarch, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
                    //itrerate through each contour
                    for (int Idx = 0; Idx < contours.size(); Idx++) {
                        double area = Imgproc.contourArea(contours.get(contourIdx));
                        if (area > largest_area) {
                            largest_area = (int) area1;
                            largest_contour_index = Idx;
                            Rect bounding_rect = Imgproc.boundingRect(contour.get(Idx));
                            Mat img = ROI.submat(bounding_rect1);
    
                            Bitmap resultBitmap = Bitmap.createBitmap(gray.cols(), gray.rows(), Bitmap.Config.ARGB_8888);
                            Utils.matToBitmap(gray, resultBitmap);
                            ((ImageView) findViewById(R.id.imageView)).setImageBitmap(resultBitmap);
    
                        }
    
                    }
    

相关问题