• 最大连通域


      1 // liantongyu.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include "stdafx.h"
      5 #include <iostream>
      6 #include<numeric>
      7 #include<opencv2opencv.hpp>
      8 #include <opencv2/core/core.hpp>
      9 #include <opencv2/imgproc/imgproc.hpp>
     10 #include <opencv2/highgui/highgui.hpp>
     11 #include <opencv2/video/video.hpp>  
     12 
     13 using namespace cv;
     14 using namespace std;
     15 
     16 void contourDelete(Mat &bwImg, Mat &bwImg1, Rect &rect)
     17 {
     18     for (int y = rect.y; y < rect.y + rect.height; y++)
     19     {
     20         for (int x = rect.x; x < rect.x + rect.width; x++)
     21         {
     22             const uchar *inData = bwImg.ptr<uchar>(y)+x;
     23             uchar *outData = bwImg1.ptr<uchar>(y) + x;
     24             if (inData[0]==255)
     25             {
     26                 outData[0] = 0;
     27             }
     28         }
     29     }
     30 
     31 }
     32 
     33 int main()
     34 {
     35     Mat bwImg,srcImg, bwImg1;
     36     srcImg = imread("14.jpg");
     37     vector<vector<Point>> contours;
     38 
     39     imshow("original image", srcImg);
     40 
     41     //灰度转换+阈值+形态操作
     42     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
     43     threshold(srcImg, bwImg, 0.0, 255.0, CV_THRESH_BINARY | CV_THRESH_OTSU);
     44     Mat element = getStructuringElement(CV_SHAPE_ELLIPSE, Size(5, 5));
     45     morphologyEx(bwImg, bwImg, MORPH_OPEN, element);
     46     
     47     //复制得到bwImage1
     48     bwImg1 = bwImg.clone();
     49 
     50     imshow("binary image", bwImg);
     51     imshow("bwImg1", bwImg1);
     52 
     53     // 查找轮廓,对应连通域
     54     findContours(bwImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
     55 
     56     // 寻找最大连通域
     57     double maxArea = 0;
     58     vector<Point> maxContour;
     59     for (size_t i = 0; i < contours.size(); i++)
     60     {
     61         double area = contourArea(contours[i]);
     62         if (area > maxArea)
     63         {
     64             maxArea = area;
     65             maxContour = contours[i];
     66         }
     67     }
     68 
     69     //输出最大连通区域面积
     70     cout << "最大的连通区域面积是:" << maxArea;
     71 
     72     //删除除最大连通域外的所有小连通域
     73     for (size_t i = 0; i < contours.size(); i++)
     74     {
     75         double area = contourArea(contours[i]);
     76         Rect rect = boundingRect(contours[i]);
     77         if (area < maxArea)
     78         {
     79             contourDelete(bwImg,bwImg1,rect);
     80         }
     81     }
     82 
     83     // 将轮廓转为矩形框
     84     Rect maxRect =boundingRect(maxContour);
     85 
     86     // 显示连通域
     87     Mat result1, result2;
     88     bwImg.copyTo(result1);
     89     bwImg1.copyTo(result2);
     90 
     91     //显示所有轮廓矩形框
     92     for (size_t i = 0; i < contours.size(); i++)
     93     {
     94         Rect r = boundingRect(contours[i]);
     95         rectangle(result1, r,Scalar(255));
     96     }
     97     imshow("all regions", result1);
     98 
     99     //显示最大轮廓矩形框
    100     rectangle(result2, maxRect,Scalar(255));
    101     imshow("largest region", result2);
    102     imshow("最终结果",bwImg1);
    103     waitKey();
    104     return 0;
    105 }
  • 相关阅读:
    instanceof操作符判断对象类型
    继承
    题解 P3943 星空
    NOIP 模拟 10 考试总结
    题解 P3942 将军令
    题解 P3941 入阵曲
    题解 P3191 [HNOI2007]紧急疏散EVACUATE
    NOIP 模拟 9 考试总结
    NOIP 模拟 9 分组
    NOIP 模拟 9 数颜色
  • 原文地址:https://www.cnblogs.com/hsy1941/p/7806525.html
Copyright © 2020-2023  润新知