zoukankan      html  css  js  c++  java
  • Opencv2系列学习笔记10(提取连通区域轮廓) 另一个

    http://blog.csdn.net/lu597203933/article/details/17362457

    连通区域指的是二值图像中相连像素组成的形状。而内、外轮廓的概念及opencv1中如何提取二值图像的轮廓见我的这篇博客:http://blog.csdn.net/lu597203933/article/details/14489225

     轮廓的简单提取算法如下:

           系统性地扫描图像直到遇到连通区域的一个点,以它为起始点,跟踪它的轮廓,标记边界上的像素。当轮廓完整闭合,扫描回到上一个位置,直到再次发现新的成分。

    代码:

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. #include <opencv2corecore.hpp>  
    3. #include <opencv2highguihighgui.hpp>  
    4. #include <opencv2imgprocimgproc.hpp>  
    5.   
    6. using namespace std;  
    7. using namespace cv;  
    8.   
    9. // 移除过小或过大的轮廓  
    10. void getSizeContours(vector<vector<Point>> &contours)  
    11. {  
    12.     int cmin = 100;   // 最小轮廓长度  
    13.     int cmax = 1000;   // 最大轮廓长度  
    14.     vector<vector<Point>>::const_iterator itc = contours.begin();  
    15.     while(itc != contours.end())  
    16.     {  
    17.         if((itc->size()) < cmin || (itc->size()) > cmax)  
    18.         {  
    19.             itc = contours.erase(itc);  
    20.         }  
    21.         else ++ itc;  
    22.     }  
    23. }  
    24.   
    25. // 计算连通区域的轮廓,即二值图像中相连像素的形状  
    26.   
    27. int main()  
    28. {  
    29.     Mat image = imread("E:\opencv2cv\lesson7\Debug\55.png",0);  
    30.     if(!image.data)  
    31.     {  
    32.         cout << "Fail to load image" << endl;  
    33.         return 0;  
    34.     }  
    35.     Mat imageShold;  
    36.     threshold(image, imageShold, 100, 255, THRESH_BINARY);   // 必须进行二值化  
    37.     vector<vector<Point>> contours;  
    38.     //CV_CHAIN_APPROX_NONE  获取每个轮廓每个像素点  
    39.     findContours(imageShold, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));  
    40.     getSizeContours(contours);  
    41.     cout << contours.size() << endl;  
    42.     Mat result(image.size(), CV_8U, Scalar(255));  
    43.     drawContours(result, contours, -1, Scalar(0), 2);   // -1 表示所有轮廓  
    44.     namedWindow("result");  
    45.     imshow("result", result);  
    46.     namedWindow("image");  
    47.     imshow("image", image);  
    48.     waitKey(0);  
    49.     return 0;  
    50. }  

    结果:

    未移除过大多小的轮廓前:

    移除后:

  • 相关阅读:
    dapper中使用类似whereif语句
    xamarin开发常见错误总结Sqlite本地数据库使用了保留字段导致语法错误
    vs2019C#代码规范设置命名规范
    xamarin在visual studio中遇到的包还原问题
    基础知识除数vs被除数
    xaf代码注册module
    xaf手动注册module之WebForm
    xamarin开发常见错误总结the operation was canceled
    JDK源码分析实战系列ThreadLocal
    公众号文章汇总
  • 原文地址:https://www.cnblogs.com/donaldlee2008/p/5230032.html
Copyright © 2011-2022 走看看