Question

I am doing some ellipse recognition in an image and in order to do so I am opening a simple image:

img = imread("M:/Desktop/PsEyeRight.jpg", CV_LOAD_IMAGE_COLOR);

selecting a ROI (that's the only way I could see to set a ROI in OpenCV 2.4.6 where the old library had a cvSetImageROI() and cvResetImageROI() which I think were more simple):

Mat roi(img, Rect(Point(205, 72), Point(419,285)));

changing its color space with cvtColor:

cvtColor(roi, roi, CV_BGR2GRAY); 

applying Threshold:

threshold(roi, roi, 150, 255, THRESH_BINARY); 

Then I do the findContours with a cloned image since findContours modifies the image passed in the function and then I change the ROI back to BGR color space:

cvtColor(roi, roi, CV_GRAY2BGR);

And draw all the found ellipses in roi.

When I show roi I can see that everything worked 100% but I was expecting that when I showed the original image, it will be the original image with the ROI in threshold and the drawings inside of it, but instead I just get the original image itself, like nothing has changed. I believe this is happening because cvtColor is copying roi, so it doesn't "point" to img any more.

What's the best way (or recommended) to do this same processing and have the ROI inside the original image, showing the progress of the algorithm?

Was it helpful?

Solution

the main problem is, that you can't have an image, which is partly 3chan/rgb and partly 1chan/gray.

my solution would be , to work on a copy of the roi in the 1st place, and later convert it back to rgb and paste it into the original image.

img = imread("M:/Desktop/PsEyeRight.jpg", CV_LOAD_IMAGE_COLOR); // original
Mat roi(img, Rect(Point(205, 72), Point(419,285)));
Mat work = roi.clone();
cvtColor(work , work , CV_BGR2GRAY); 
threshold(work , work , 150, 255, THRESH_BINARY); 
// findContours(work,...);
cvtColor(work , roi, CV_GRAY2BGR); //here's the trick
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top