1

I have detected contour in my image. After detecting contour, I filtered contours based on area. Then I have drawn a bounding rectangle around license plate,around only one contour.

How to crop the remaining image and get only the region of rectangle drawn, that is, the I want to get only license plate around which I have drawn a rectangle. I dont have have coordinates of rectangle. I simply used Core.rectangle() function in opencv to draw a rectangle.

Can anyone help me with this. Please provide code in android + opencv using which i can ge desired results. I am also attaching an image.Here I have drawn rectangle using Core.rectangle(). I want to get the rectangle part and crop other part.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • did it cross your mind that you could google "opencv crop"? – Piglet Apr 03 '17 at 07:42
  • 2
    Possible duplicate of [How to crop a CvMat in OpenCV?](http://stackoverflow.com/questions/8267191/how-to-crop-a-cvmat-in-opencv) – Piglet Apr 03 '17 at 07:42

1 Answers1

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

                    }

                }
Maku
  • 87
  • 4
  • 9
  • While this code snippet may solve the problem, [including an explanation](//meta.stackexchange.com/q/114762) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Cody Gray - on strike Oct 08 '17 at 07:44