how to extract part of an image by knowing MatOfPoint
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
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)
you could iterate through the contour, and query the pixel under each point, but would that be, what you want ?
original.get(y,x), btw. same for put()
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]
anddata[3]
always 0it does not make sense. contours are points, you cannot put a color into that