OpenCV函數cvFindContous提取圖像輪廓
阿新 • • 發佈:2018-12-25
fin num nbsp lang 圖像 str amp ray ads
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;
}
源圖: 運行結果圖:
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://www.cnblogs.com/captainbed
OpenCV函數cvFindContous提取圖像輪廓