#include "cv.h"
#include "highgui.h"
#include "opencv2\core\core.hpp"
IplImage *src=cvLoadImage("D:\\pic\\3.jpg",0);
IplImage *gray;
int thresh=100;
//轮廓storage必须初始化
CvMemStorage *storage=cvCreateMemStorage(0);
void on_trackbar(int)
{
gray=cvCreateImage(cvGetSize(src),8,1);
CvMemStorage *storage=cvCreateMemStorage(0);
//Cvseq初始化为 NULL
CvSeq *firstcontours=NULL;
//灰度图
//cvCvtColor(src,gray,CV_BGR2GRAY);
//二值化
cvThreshold(src,gray,thresh,255,CV_THRESH_BINARY);
//寻找轮廓
cvFindContours(gray,storage,&firstcontours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
//画轮廓
for(; firstcontours != 0; firstcontours = firstcontours->h_next)
{
cvDrawContours(gray,firstcontours,cvScalarAll(255),cvScalarAll(255),100);
}
cvShowImage("contours",gray);
}
int main()
{
cvNamedWindow("contours",1);
cvCreateTrackbar("threshold","contours",&thresh,150,on_trackbar);
on_trackbar(0);
cvWaitKey();
return 0;
}