zoukankan      html  css  js  c++  java
  • 最大连通域(指针)

      1 #include <cv.h>   
      2 
      3 #include <highgui.h>   
      4 
      5 #include <vector>   
      6 
      7 #include <algorithm>   
      8 
      9 using namespace std;  
     10 
     11 
     12 
     13 int main()  
     14 
     15 {  
     16 
     17     IplImage *src = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_COLOR);  
     18 
     19     cvNamedWindow("原始图像");  
     20 
     21     cvShowImage("原始图像", src);  
     22 
     23   
     24 
     25     IplImage* dst=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);   
     26 
     27     cvCvtColor(src,dst,CV_BGR2GRAY);  
     28 
     29     cvNamedWindow("灰度图像");  
     30 
     31     cvShowImage("灰度图像", src);  
     32 
     33   
     34 
     35     cvThreshold(dst, dst, 0.0, 255.0, CV_THRESH_BINARY | CV_THRESH_OTSU);//OTSU二值化   
     36 
     37     IplConvKernel *element = cvCreateStructuringElementEx(5, 5, 0, 0, CV_SHAPE_ELLIPSE);  
     38 
     39     cvMorphologyEx(dst, dst, NULL, element, CV_MOP_OPEN);//开运算,去除比结构元素小的点   
     40 
     41     cvReleaseStructuringElement(&element);  
     42 
     43     cvNamedWindow("二值图像");  
     44 
     45     cvShowImage("二值图像", dst);  
     46 
     47       
     48 
     49     int w,h;  
     50 
     51     CvSize sz = cvGetSize(dst);  
     52 
     53       
     54 
     55     int color = 254;//不对0计数,不可能为255,所以254   
     56 
     57     for (w=0; w<sz.width; w++)  
     58 
     59     {  
     60 
     61         for (h=0; h<sz.height; h++)  
     62 
     63         {  
     64 
     65             if (color > 0)  
     66 
     67             {  
     68 
     69                 if (CV_IMAGE_ELEM(dst, unsigned char, h, w) == 255)  
     70 
     71                 {  
     72 
     73                     //把连通域标记上颜色   
     74 
     75                     cvFloodFill(dst, cvPoint(w,h), CV_RGB( color,color,color));  
     76 
     77                     color--;  
     78 
     79                 }  
     80 
     81             }  
     82 
     83         }  
     84 
     85     }  
     86 
     87     cvNamedWindow("标记颜色后的图像");  
     88 
     89     cvShowImage("标记颜色后的图像", dst);  
     90 
     91       
     92 
     93     int colorsum[255] = {0};  
     94 
     95     for (w=0; w<sz.width; w++)  
     96 
     97     {  
     98 
     99         for (h=0; h<sz.height; h++)  
    100 
    101         {  
    102 
    103             if (CV_IMAGE_ELEM(dst, unsigned char, h, w) > 0)  
    104 
    105             {  
    106 
    107                 colorsum[CV_IMAGE_ELEM(dst, unsigned char, h, w)]++;//统计每种颜色的数量   
    108 
    109             }  
    110 
    111         }  
    112 
    113     }  
    114 
    115     vector<int> v1(colorsum, colorsum+255);//用数组初始化vector   
    116 
    117     //求出最多数量的染色,注意max_element的使用方法   
    118 
    119     int maxcolorsum = max_element(v1.begin(), v1.end()) - v1.begin();  
    120 
    121     printf("%d
    ",maxcolorsum);  
    122 
    123   
    124 
    125     for (w=0; w<sz.width; w++)  
    126 
    127     {  
    128 
    129         for (h=0; h<sz.height; h++)  
    130 
    131         {  
    132 
    133             if (CV_IMAGE_ELEM(dst, unsigned char, h, w) == maxcolorsum)  
    134 
    135             {  
    136 
    137                 CV_IMAGE_ELEM(dst, unsigned char, h, w) = 255;  
    138 
    139             }  
    140 
    141             else  
    142 
    143             {  
    144 
    145                 CV_IMAGE_ELEM(dst, unsigned char, h, w) = 0;  
    146 
    147             }  
    148 
    149         }  
    150 
    151     }  
    152 
    153     cvNamedWindow("最大连通域图");  
    154 
    155     cvShowImage("最大连通域图", dst);  
    156 
    157       
    158 
    159     cvWaitKey(0);   
    160 
    161     cvDestroyAllWindows();  
    162 
    163     cvReleaseImage(&src);  
    164 
    165     cvReleaseImage(&dst);  
    166 
    167       
    168 
    169     return 0;  
    170 
    171 }  
  • 相关阅读:
    json 拼二维json数组
    lnmp 环境搭建
    lnmp git ruby sass 安装
    制定clone的用户名
    git branch 重命名
    sql命令创建数据库
    layerui
    弹出确认框,打开新窗口
    Ajax请求小结
    .net core注入服务
  • 原文地址:https://www.cnblogs.com/hsy1941/p/7806544.html
Copyright © 2011-2022 走看看