分享

《学习openCV》例程解析 ex_8_2 (轮廓)

 instl 2013-12-29
例:根据滑动条参数检测轮廓,在滑动条变化时候重新检测

效果图:

  1. /** 
  2.   Our Example 8-2 is drawn from the OpenCV package. Here we create a window with an  
  3. image in it. A trackbar sets a simple threshold, and the contours in the thresholded im- 
  4. age are drawn. The image is updated whenever the trackbar is adjusted. 
  5. Example 8-2. Finding contours based on a trackbar’s location; the contours are updated whenever  
  6. the trackbar is moved                                                                      
  7. */  
  8.   
  9.   
  10. #include "stdafx.h"   
  11. #include "cv.h"   
  12. #include "highgui.h"   
  13.   
  14. IplImage* g_img = NULL;  
  15. IplImage* g_gray = NULL;  
  16. int g_thresh = 100;  
  17. CvMemStorage* g_storage = NULL;         //内存存储器是一个可用来存储诸如序列,轮廓,图形,子划分等   
  18.                                         //动态增长数据结构的底层结构。   
  19.   
  20. void on_trackbar(int pos)  
  21. {  
  22.     if (g_storage == NULL)  
  23.     {  
  24.         g_gray = cvCreateImage(cvGetSize(g_img), 8, 1);  
  25.         g_storage = cvCreateMemStorage();//创建内存块默认值为64K   
  26.     }  
  27.     else  
  28.     {  
  29.         cvClearMemStorage(g_storage);   //清除储存块内容,并不释放内存   
  30.     }  
  31.       
  32.     cvCvtColor(g_img, g_gray, CV_BGR2GRAY);  
  33.     //色彩空间转换, BGR到GRAY   
  34.     cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY);  
  35.     //对数组元素进行固定阈值操作   
  36.     //该函数的典型应用是对灰度图像进行阈值操作得到二值图像。   
  37.   
  38.     CvSeq* contours = 0;  
  39.     //可动态增长元素序列   
  40.     cvFindContours(                     //在二值图像中寻找轮廓    
  41.         g_gray,                         //输入二值图像   
  42.         g_storage,                      //得到的轮廓的存储容器    
  43.         &contours                       //指向第一个轮廓的指针   
  44.         );  
  45.     //coutours序列指针指向储存在g_storage 内存块中的轮廓   
  46.   
  47.     cvZero(g_gray);                     //等于cvSetZero,清空图像   
  48.     if (contours)                       //如果序列非空   
  49.     {  
  50.         cvDrawContours(                 //在图像中绘制外部和内部的轮廓   
  51.             g_gray,  
  52.             contours,                   //指针指向第一个轮廓   
  53.             cvScalarAll(255),           //g_gray 为单通道颜色,只有一种颜色   
  54.             cvScalarAll(255),           //赋为白色   
  55.             100                         //绘制轮廓的最大等级,如果为0 单独绘制轮廓   
  56.             );  
  57.         cvShowImage("Contours", g_gray);  
  58.     }  
  59. }  
  60.   
  61. int main()  
  62. {     
  63.     cvNamedWindow("g_img", CV_WINDOW_AUTOSIZE);  
  64.     g_img = cvLoadImage("lena.jpg");  
  65.     cvShowImage("g_img", g_img);  
  66.   
  67.     cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);  
  68.   
  69.     cvCreateTrackbar(  
  70.                  "Threshold",       //滑块名字   
  71.                  "Contours",        //滑块所在窗口名字   
  72.                  &g_thresh,         //指定创建时的滑块位置   
  73.                  255,               //滑块位置的最大值   
  74.                  on_trackbar        //每次滑块位置被改变的时候,被调用函数的指针。   
  75.                                     //这个函数应该被声明为void Foo(int);    
  76.                                     //如果没有回调函数,这个值可以设为NULL。    
  77.                  );  
  78.   
  79.     on_trackbar(0);                 //初始先调用一次,否者滑块变动时才显示   
  80.     cvWaitKey();  
  81.   
  82.     cvDestroyAllWindows();  
  83.     cvReleaseImage(&g_img);  
  84.     cvReleaseImage(&g_gray);  
  85.     return 0;  
  86. }  

(2) 效果图


改进单独加颜色绘制轮廓

  1. /** 
  2.     扩展8_2使每个轮廓都随机颜色                                                                
  3. */  
  4.   
  5.   
  6. #include "stdafx.h"   
  7. #include "cv.h"   
  8. #include "highgui.h"   
  9.   
  10. IplImage* g_img = NULL;  
  11. IplImage* g_gray = NULL;  
  12. int g_thresh = 100;  
  13. CvMemStorage* g_storage = NULL;         //内存存储器是一个可用来存储诸如序列,轮廓,图形,子划分等   
  14.                                         //动态增长数据结构的底层结构。   
  15.   
  16. void on_trackbar(int pos)  
  17. {  
  18.     if (g_storage == NULL)  
  19.     {  
  20.         g_gray = cvCreateImage(cvGetSize(g_img), 8, 1);  
  21.         g_storage = cvCreateMemStorage();//创建内存块默认值为64K   
  22.     }  
  23.     else  
  24.     {  
  25.         cvClearMemStorage(g_storage);   //清除储存块内容,并不释放内存   
  26.     }  
  27.       
  28.     cvCvtColor(g_img, g_gray, CV_BGR2GRAY);  
  29.     //色彩空间转换, BGR到GRAY   
  30.     cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY);  
  31.     //对数组元素进行固定阈值操作   
  32.     //该函数的典型应用是对灰度图像进行阈值操作得到二值图像。   
  33.   
  34.     CvSeq* contours = 0;  
  35.     //可动态增长元素序列   
  36.     cvFindContours(                     //在二值图像中寻找轮廓    
  37.         g_gray,                         //输入二值图像   
  38.         g_storage,                      //得到的轮廓的存储容器    
  39.         &contours                       //指向第一个轮廓的指针   
  40.         );  
  41.     //coutours序列指针指向储存在g_storage 内存块中的轮廓   
  42.   
  43.     cvZero(g_gray);                     //等于cvSetZero,清空图像   
  44.   
  45.     IplImage* g_temp = cvCreateImage(cvGetSize(g_gray), 8, 3);  
  46.     //创建一个3通道图像,显示有颜色的轮廓   
  47.     cvZero(g_temp);                       
  48.   
  49.     for(; contours != 0; contours = contours->h_next)  
  50.     {  
  51.         //因为要单独绘制轮廓,所以得用循环   
  52.         CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255);  
  53.         //随机取色   
  54.         cvDrawContours(                 //在图像中绘制外部和内部的轮廓   
  55.             g_temp,  
  56.             contours,                   //指针指向第一个轮廓   
  57.             color,  
  58.             color,  
  59.             0                           //单独绘制轮廓   
  60.             );  
  61.     }  
  62.     cvShowImage("Contours", g_temp);  
  63. }  
  64.   
  65. int main()  
  66. {     
  67.     cvNamedWindow("g_img", CV_WINDOW_AUTOSIZE);  
  68.     g_img = cvLoadImage("lena.jpg");  
  69.     cvShowImage("g_img", g_img);  
  70.   
  71.     cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);  
  72.   
  73.     cvCreateTrackbar(  
  74.                  "Threshold",       //滑块名字   
  75.                  "Contours",        //滑块所在窗口名字   
  76.                  &g_thresh,         //指定创建时的滑块位置   
  77.                  255,               //滑块位置的最大值   
  78.                  on_trackbar        //每次滑块位置被改变的时候,被调用函数的指针。   
  79.                                     //这个函数应该被声明为void Foo(int);    
  80.                                     //如果没有回调函数,这个值可以设为NULL。    
  81.                  );  
  82.   
  83.     on_trackbar(0);                 //初始先调用一次,否者滑块变动时才显示   
  84.     cvWaitKey();  
  85.   
  86.     cvDestroyAllWindows();  
  87.     cvReleaseImage(&g_img);  
  88.     cvReleaseImage(&g_gray);  
  89.     return 0;  
  90. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多