opencv2.4.9 findContours并求面积

来源:互联网 发布:淮南电信网络宽带 编辑:程序博客网 时间:2024/05/29 21:18

代码

#include<opencv2\highgui\highgui.hpp>#include<opencv2\imgproc\imgproc.hpp>#include<iostream>using namespace std;using namespace cv;int main(){    Mat src = imread( "D:/MyDesktop/1.bmp" );    Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);    Mat canny_output;    vector<vector<Point>> contours;    vector<Vec4i> hierarchy;    RNG rng;    vector<double> area;    /// Detect edges using canny    Canny(src, canny_output, 100, 200);    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );    /// Draw contours    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);    for (int i = 0; i< contours.size(); i++)    {        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());        area.push_back( contourArea(contours[i]) );    }    cout << area.size() << endl;    imshow("111", drawing);    waitKey();    return 0;}

图像

原图

这里写图片描述

结果图

这里写图片描述

0 0
原创粉丝点击