OpenCV代碼功能:檢測並畫出圖像中的輪廓
主要函數:cvFindContours、cvDrawContours
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <iostream.h>
void main()
{
int i=0;
int mode=CV_RETR_CCOMP; //the mode of contours
int num_contours=0;
CvScalar ext_color,inn_color;
CvMemStorage*storage=cvCreateMemStorage(0);
CvSeq*contour=0;
IplImage*pimg=NULL;
IplImage*CTImg=NULL;
IplImage*src=cvLoadImage("test.bmp",-1);
pimg=cvCreateImage(cvGetSize(src),src->depth,1);
CTImg=cvCreateImage(cvGetSize(pimg),IPL_DEPTH_8U,3);
cvCvtColor(src,pimg,CV_BGR2GRAY);
cvNamedWindow("source",CV_WINDOW_AUTOSIZE);
cvNamedWindow("gray",CV_WINDOW_AUTOSIZE);
cvNamedWindow("contours",CV_WINDOW_AUTOSIZE);
cvShowImage("source",src);
//cvThreshold(pimg,pimg,200,250,CV_THRESH_BINARY);
cvErode(pimg,pimg,0,1);
cvDilate(pimg,pimg,0,1);
cvCanny(pimg,pimg,2,20,3);
//cvErode(pimg,pimg,0,2);
//cvDilate(pimg,pimg,0,1);
//Find the Contours
mode=CV_RETR_LIST;
num_contours=cvFindContours(pimg,storage,&contour,sizeof(CvContour),mode,CV_CHAIN_APPROX_NONE);
cout<<"The number of the contours is :"<<num_contours<<endl;
//Draw the Contours
for (;contour!=0;contour=contour->h_next)
{
inn_color=CV_RGB(rand()&255,rand()&255,rand()&255);
ext_color=CV_RGB(rand()&255,rand()&255,rand()&255);
cvDrawContours(CTImg,contour,ext_color,inn_color,1,2,8,cvPoint(0,0));
}
cvShowImage("gray",pimg);
cvShowImage("contours",CTImg);
cvWaitKey(0);
cvSaveImage("Contours.bmp",CTImg);
cvSaveImage("Canny.bmp",pimg);
cvReleaseImage(&pimg);
cvReleaseImage(&CTImg);
cvReleaseImage(&src);
cvReleaseMemStorage(&storage);
cvDestroyAllWindows();
}
效果圖:
--------------------------------------分割線 --------------------------------------
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm
--------------------------------------分割線 --------------------------------------
OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡