Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically detect and crop ROI in OpenCV

I have these images to compare with each other. However, there are too many blacks that I think I can crop out to make comparison more effective.

Mars 1Mars 2Mars 3

What I want to do is crop Mars. Rectangle or round whichever may yield better results when compared. I was worrying that if the cropping would result to images of different sizes, comparison wouldn't work out as well as expected? Ideas how to do it and sample codes if possible? Thanks in advance

UPDATE: Tried using cvHoughCircles() it won't detect the planet :/

like image 896
Masochist Avatar asked Dec 20 '22 10:12

Masochist


2 Answers

Try to use color detection. You need to find all the colors except black. Here and here are nice explanations of this method.

like image 107
Ann Orlova Avatar answered Dec 22 '22 23:12

Ann Orlova


You can convert these images to gray scale images using cvCvtColor(img,imgGrayScale,CV_BGR2GRAY)

Then threshold them using cvThreshold(imgGrayScale,imgThresh,x,255,CV_THRESH_BINARY). Here, you have to find a good value for x(I think x=50 is ok).

CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgThresh, moments, 1);
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
int x = moment10/area;
int y = moment01/area;

Now you know the (x.y) coordinate of the blob. Then you can crop the image using cvSetImageROI(imgThresh, cvRect(x-10, y-10, x+10, y+10)). Here I have assumed that the radius of this blob is less than 10 pixel.

All cropped images are of same size and the white blob (planet) is exactly at the middle of the image.

Then you can compare images using normalized cross-correlation.

like image 22
SRF Avatar answered Dec 22 '22 23:12

SRF