Saturday, 12 April 2014

#10a. Point Polygon Test: Part I

Point Polygon Test determines whether the point is inside a contour, outside, or lies on an edge (or coincides with a vertex). It returns positive (inside), negative (outside), or zero (on an edge) value, correspondingly. When flag is turned off , the return value is +1, -1, and 0, respectively. Otherwise, the return value is a signed distance between the point and the nearest contour edge.
I've divided the tutorial in two parts to make it easy to understand.
---------------------------------------------------
#include<iostream>
#include<highgui.h>
#include<cv.h>
using namespace std;
using namespace cv;

Mat img_all_contours;
vector<vector<Point> > closed_contours;
vector<Vec4i> heirarchy;

vector<vector<Point> > make_contours_closed(vector<vector<Point> > contours){
vector<vector<Point> > closed_contours;
closed_contours.resize(contours.size());
for(int i=0;i<contours.size();i++)
approxPolyDP(contours[i],closed_contours[i],0.1,true);
return closed_contours;
}
int main()
{
Mat img=imread("images/circles3.jpg");
img_all_contours=img.clone();

Mat imgb;
cvtColor(img,imgb,CV_RGB2GRAY);

Mat edges;
Canny(imgb,edges,50,100);
vector<vector<Point> > contours;

findContours(edges,contours,heirarchy,CV_RETR_TREE,CV_CHAIN_APPROX_NONE);

closed_contours=make_contours_closed(contours);

drawContours(img_all_contours,closed_contours,-1,Scalar(0,255,0));
imshow("Contours",img_all_contours);

while(char(waitKey(1))!='q'){}
return 0;

}

---------------------------------------------
Here, the Main() function is similar to the Hierarchical Contour Detection code(#9a).
I've introduced a function 'make_contours_closed' which basically closes all the open contours(if any), so that whenever it is clicked, there's always a closed contour associated with the position of click.
approxPolyDP(contours[i],closed_contours[i],0.1,true);
contours[i]: Input Array
closed_contours[i]: Output Array
0.1Parameter specifying the approximation accuracy. This is the maximum distance between the original curve and its approximation.
truethe approximated curve is closed (its first and last vertices are connected). If false, it is not closed
You may look at the documentation of 'approxPolyDP' here.
------------------------------------------------------
Input Image:

Output Image:


No comments:

Post a Comment