OpenCV函式cvFindContous提取影象輪廓
阿新 • • 發佈:2018-11-04
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Opencv中提供了很多關於影象輪廓處理的函式,這裡我用cvFindContours函式來提取輪廓,並用cvDrawContours函式將提取的輪廓畫出來。函式cvFindContours的第一個引數就是我們要進行提取輪廓的目標影象,這裡要注意,這個影象
#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;}
源圖: 執行結果圖: