比较了两个图一个会产生2个轮廓,一个只产生一个轮廓
cpp
/**
* @function findContours_Demo.cpp
* @brief Demo code to find contours in an image
* @author OpenCV team
*/
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, "{@input | HappyFish.jpg | input image}");
Mat src = imread(samples::findFile(parser.get<String>("@input")));
Mat src_gray , binarized ;
cvtColor(src, src_gray, COLOR_BGR2GRAY);
threshold(src_gray, binarized, 90, 255, THRESH_BINARY); //THRESH_BINARY_INV);
//savedata(binarized);
vector<vector<Point>> contours;
findContours(binarized, contours, RETR_LIST, CHAIN_APPROX_NONE); // 查找轮廓
for (size_t i = 0; i < contours.size(); i++)
{
//contourArea(contours[i]);
double area = moments(contours[i]).m00;
cout << "轮廓" << i << " 面积:" << area << endl;
drawContours(src, contours, i, Scalar(0, 255, 0), 2);
}
string windowName = "Contours " + to_string(contours.size());
imshow(windowName, src);
waitKey();
return 0;
}

