Ask Your Question
0

how to extract part of an image by knowing MatOfPoint

asked 2018-05-03 03:54:44 -0600

mihuilk gravatar image

updated 2018-05-03 10:36:22 -0600

To find shapes I created a new copy of the original image and applied to it many different filters like cvtColor, blur, canny.I want to get the original color of the shape. I have two same size images:

Mat original;
Mat changed;

from Mat changed, I did extract contours by

 Imgproc.findContours

So now I have List<MatOfPoint> contours; My next task is to get color from each MatOfPoint. I can do that by applying function Scalar color = mean(contour); But the problem is that I need to find color of exactly same pixels from MatOfPoint but from Mat original;I can do that with Rect like this :

Rect rect = Imgproc.boundingRect(contour); 
Mat originalImageCropped = new Mat(original, rect); 
Scalar color = mean(originalImageCropped);

My qustion is how can I get color for MatOfPoint from Mat original if I know MatOfPoint from Mat changed

I'm trying this solution:

MatOfPoint contour = contours.get(i);

Point[] points = contour.toArray();
Mat cloned = contour.clone();

for (int j = 0; j < points.length; j++) {
      Point point = points[j];
      double[] data = original.get((int)point.x, (int)point.y);
      cloned.put((int)point.x, (int)point.y, data);
}

Scalar scalar = mean(cloned);

But it dosn't works

edit retag flag offensive close merge delete

Comments

much better than the last question ;)

what do you need this for ? what is the purpose of it ? (we can help better, if we know)

berak gravatar imageberak ( 2018-05-03 04:22:31 -0600 )edit

you could iterate through the contour, and query the pixel under each point, but would that be, what you want ?

berak gravatar imageberak ( 2018-05-03 04:23:41 -0600 )edit

original.get(y,x), btw. same for put()

berak gravatar imageberak ( 2018-05-03 10:46:31 -0600 )edit

Thank's for your help.But event after fix it's not working.It looks like I get mean of the x,y and not of the data.data[2] and data[3] always 0

mihuilk gravatar imagemihuilk ( 2018-05-04 01:50:19 -0600 )edit

it does not make sense. contours are points, you cannot put a color into that

berak gravatar imageberak ( 2018-05-04 03:05:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-10-29 16:58:08 -0600

for (int i = 0; i < contours.size(); i++) { MatOfPoint contour = contours.get(i); Rect rect = Imgproc.boundingRect(contour); Mat subMat = new Mat(srcOriginal,rect); HighGui.imshow("contours : " + i , subMat); }

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-05-03 03:54:44 -0600

Seen: 964 times

Last updated: May 03 '18