Sunday, 30 March 2014

#9a. Hierarchical contour extraction

Contours are curves joining all the continuous points (along the boundary), having same color or intensity.


#include
#include
#include
using namespace std;
using namespace cv;

Mat img;
vector > contours;
vector heirarchy;
int levels=0;

void on_trackbar(int, void *){
	Mat img_show=img.clone();
	drawContours(img_show,contours,-1,Scalar(0,0,255),3,8,heirarchy,levels);
	imshow("Contours",img_show);
}

int main()
{
	img=imread("images/circles3.jpg");
	Mat imgb;
	cvtColor(img,imgb,CV_RGB2GRAY);
	Mat edges;
	Canny(imgb,edges,50,100);
	findContours(edges,contours,heirarchy,CV_RETR_TREE,CV_CHAIN_APPROX_NONE);

	namedWindow("Contours");
	createTrackbar("levels","Contours",&levels,15,on_trackbar);
	on_trackbar(0,0);
	while(char(waitKey(1))!='q'){}
	return 0;
}



Firstly, in Main(), the image is read, converted to gray scale, and edges are detected by Canny edge detection algorithm.

findContours() finds all the contours in the image.
You may look at the documentation here.

Now, the contours have a hierarchy, in the sense that, there are outermost contours and there are contours inside contours which may be noise.
You should have the concept of 'contours' clear.
You may go through the documentation here.

Then we created a trackbar by which we can change the level of hierarchy.
drawContours() simply draws the contours on the image.
Find the documentation of drawContours() here.
-------------------------------------
Input Image:


Output:
Level 1:


Level 5:



Level 11(Saturation Point, for this image):


No comments:

Post a Comment