Thursday, October 29, 2009

Experiences with OpenCV

OpenCV has really lots of useful classes and features.
The square detection for my virtual reality class work is being made with it.

Unfortunately, on my tests, I was detecting squares which I didn't want the software to detect. Like on the image below:It would be much better if I could do the image analysis only inside the laptop screen. "Googling" on "OpenCV image crop" I had reached the nashruddin.com (OpenCV Region of Interest - ROI). This blog has a good explanation on how to crop (extract the region of interest) from a image.
With those informations I got the following result for the same frame:

Now I have to check the area of the detected squares. At the web archives of the computer geometry class mailing list, from the Illinois University, google had shown me one algorithm to calculate the area of a closed polygon, as follows:
Let 'vertices' be an array of N pairs (x,y), indexed from 0
Let 'area' = 0.0
for i = 0 to N-1, do
Let j = (i+1) mod N
Let area = area + vertices[i].x * vertices[j].y
Let area = area - vertices[i].y * vertices[j].x
end for

In C, to use with OpenCV:
double polygonArea(CvPoint vertices[]){
double area = 0.0;
int i, j;
for (i=0;i<4;i++){
j = (i+1) % 4;
area = area + vertices[i].x * vertices[j].y;
area = area - vertices[i].y * vertices[j].x;
}
if (area<0) area = -area;
return area;
}

No comments:

Post a Comment