1. 程式人生 > >OpenCV函式cvFindContous提取影象輪廓

OpenCV函式cvFindContous提取影象輪廓

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

Opencv中提供了很多關於影象輪廓處理的函式,這裡我用cvFindContours函式來提取輪廓,並用cvDrawContours函式將提取的輪廓畫出來。函式cvFindContours的第一個引數就是我們要進行提取輪廓的目標影象,這裡要注意,這個影象

必須是一個二值圖。得到二值圖的方法有很多,這裡我採用的是cvThreshold函式,通過設定閥值來得到相應的二值圖。當然,這裡閥值的選擇根據不同的影象可以自己適當的調整,這樣效果就比較好了!下面是原始碼:

#include<cv.h>#include<highgui.h>int main(){ IplImage* src = NULL; IplImage* img = NULL; IplImage* dst = NULL; CvMemStorage* storage = cvCreateMemStorage (0);; CvSeq* contour = 0
int contours = 0; CvScalar external_color; CvScalar hole_color; src = cvLoadImage ("test.jpg", 1); img = cvCreateImage (cvGetSize(src), IPL_DEPTH_8U, 1); dst = cvCreateImage (cvGetSize(src), src->depth, src->nChannels); cvCvtColor (src, img, CV_BGR2GRAY); cvThreshold (img, img, 100
, 200, CV_THRESH_BINARY); contours = cvFindContours (img, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE); for (;contour != 0; contour = contour->h_next) {  external_color = CV_RGB(rand()&255, rand()&255, rand()&255);  hole_color = CV_RGB(rand()&255, rand()&255, rand()&255);  cvDrawContours (dst, contour, external_color, hole_color, 1, 2, 8); } cvNamedWindow ("Contour", 1); cvShowImage ("Contour", dst); cvWaitKey (0); cvReleaseMemStorage (&storage); cvReleaseImage (&src); cvReleaseImage (&img); cvReleaseImage (&dst); return 0;}

源圖: 執行結果圖:


           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述