[FIXED] Using OpenCV4Android, how to create an ROI (Region-Of-Interest or a submat) from a contour?

Issue

Given an image Mat, and a contour (which is a MatOfPoint) in it, how can I create a ROI (Region Of Interest)/submat?

I can see three interesting methods on href="http://docs.opencv.org/java/2.4.9/org/opencv/core/Mat.html" rel="nofollow">docs of Mat,

Mat submat(int rowStart, int rowEnd, int colStart, int colEnd) Extracts a rectangular submatrix.

Mat submat(Range rowRange, Range colRange) Extracts a rectangular submatrix.

Mat submat(Rect roi) Extracts a rectangular submatrix.

  1. Is there a way to find out rowStart, rowEnd, colStart and colEnd from the contour?

or

  1. Is there a way to get rowRange and colRange from the contour?

or

  1. Can I make a Rect from the contour?

Solution

Use Imgproc.boundingRect(MatOfPoint contour) method. This way you can use the third of the submat() methods you have listed:

Rect roiRect = Imgproc.boundingRect(contour);
Mat roiSubmat = originalMat.submat(roiRect);

roiSubmat is your region of interest (stored in a Mat).



Answered By - NXA
Answer Checked By - Mildred Charles (sql.easybugfix Admin)

Related Posts

Post a Comment