《OpenCV入门指南》系列文章地址:http://blog.csdn.net/morewindows/article/category/863841
上一篇《【OpenCV入门指南】第五篇轮廓检测上》介绍了cvFindContours函数和cvDrawContours函数,并作了一个简单的使用示范。本篇将展示一个实例,让大家对轮廓检测有个更加深入的认识。
代码如下:
- //图像的轮廓检测下
- //By MoreWindows (http://blog.csdn.net/MoreWindows)
- #include <opencv2/opencv.hpp>
- using namespace std;
- #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
- IplImage *g_pGrayImage = NULL;
- const char *pstrWindowsBinaryTitle = "二值图(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsOutLineTitle = "轮廓图(http://blog.csdn.net/MoreWindows)";
- CvSeq *g_pcvSeq = NULL;
- void on_trackbar(int pos)
- {
- // 转为二值图
- IplImage *pBinaryImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 1);
- cvThreshold(g_pGrayImage, pBinaryImage, pos, 255, CV_THRESH_BINARY);
- // 显示二值图
- cvShowImage(pstrWindowsBinaryTitle, pBinaryImage);
- CvMemStorage* cvMStorage = cvCreateMemStorage();
- // 检索轮廓并返回检测到的轮廓的个数
- cvFindContours(pBinaryImage,cvMStorage, &g_pcvSeq);
- IplImage *pOutlineImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 3);
- int _levels = 5;
- cvZero(pOutlineImage);
- cvDrawContours(pOutlineImage, g_pcvSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels);
- cvShowImage(pstrWindowsOutLineTitle, pOutlineImage);
- cvReleaseMemStorage(&cvMStorage);
- cvReleaseImage(&pBinaryImage);
- cvReleaseImage(&pOutlineImage);
- }
- int main( int argc, char** argv )
- {
- const char *pstrWindowsSrcTitle = "原图(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsToolBarName = "二值化";
- // 从文件中加载原图
- IplImage *pSrcImage = cvLoadImage("003.jpg", CV_LOAD_IMAGE_UNCHANGED);
- // 显示原图
- cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE);
- cvShowImage(pstrWindowsSrcTitle, pSrcImage);
- // 转为灰度图
- g_pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
- cvCvtColor(pSrcImage, g_pGrayImage, CV_BGR2GRAY);
- // 创建二值图和轮廓图窗口
- cvNamedWindow(pstrWindowsBinaryTitle, CV_WINDOW_AUTOSIZE);
- cvNamedWindow(pstrWindowsOutLineTitle, CV_WINDOW_AUTOSIZE);
- // 滑动条
- int nThreshold = 0;
- cvCreateTrackbar(pstrWindowsToolBarName, pstrWindowsBinaryTitle, &nThreshold, 254, on_trackbar);
- on_trackbar(1);
- cvWaitKey(0);
- cvDestroyWindow(pstrWindowsSrcTitle);
- cvDestroyWindow(pstrWindowsBinaryTitle);
- cvDestroyWindow(pstrWindowsOutLineTitle);
- cvReleaseImage(&pSrcImage);
- cvReleaseImage(&g_pGrayImage);
- return 0;
- }
程序运行结果如下所示:
可以明显看出,由于图像的二值化处理只是根据图像中单个像素的值来决定,因此这种最简单轮廓检测所产生的结果和人眼观测的实际感觉肯定是有比较大的区别的。
轮廓检测另外还有一个重要的函数cvApproxPoly,它的函数原型如下所示
CVAPI(CvSeq*) cvApproxPoly(
const void* src_seq,
int header_size,
CvMemStorage* storage,
int method,
double parameter,
int parameter2 CV_DEFAULT(0)
);
由于篇幅问题,对于cvApproxPoly函数就不细讲了,有兴趣的读者可以参考下面这一篇博客:http://blog.csdn.net/bruce_zeng/article/details/8074253
《OpenCV入门指南》系列文章地址:http://blog.csdn.net/morewindows/article/category/863841
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8253174
欢迎关注微博:http://weibo.com/MoreWindows