How can I detect the color of a certain portion of an image if I know the coordinates of this portion as a rectangle?

asked 2017-07-11 08:31:44 -0500

I'm working on contour and color detection, I managed to detect the contours and determine the shapes (rect, tri ...) but I want to identify the color of the rectangle for example how can I do so?

Thanks in advance.

edit retag flag offensive close merge delete



To get the color of an image at a specific position you can use Vec3b color =<Vec3b>(Point(x,y)); What do you need exactly? How much different colors are in your images?

Franz Kaiser gravatar imageFranz Kaiser ( 2017-07-11 09:14:30 -0500 )edit

or you can use a Mask to delete everything from the image except what you want it to remain within your rectangle...

    cv::Mat in_mat;   // your image
cv::Mat mask_mat = cv::zeros(in_mat.size(), CV_8UC1); // an image with your rectangle same size as in_mask but b/w
cv::Mat masked_mat = cv::zeros(in_mat.size(), CV_8UC3); // an image with your rectangle same size as in_mask
cv::Mat out_mat = cv::zeros(in_mat.size(), CV_8UC3);  // output image same size of in_mat

mask_matrectangle(mask_mat, (yourIntX1, yourIntY1), (yourIntX2, yourIntY2), (255, 255, 255), -1)

masked_mat = cv::bitwise_and(in_mat, mask_mat)
in_mat.copyTo(out_mat, masked_mat);

imgshow("your result of colur masked image", out_mask);


gfx gravatar imagegfx ( 2017-07-12 01:37:06 -0500 )edit