findcontours 发现太多轮廓
findcontours finds too many contours
find contours 在明明只有 3 个轮廓的情况下找到这么多轮廓正常吗?
原图和绘制后找到的轮廓如下图所示:
密码是:
Mat image;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
image = imread(argv[1], 0); // Read the file
findContours(image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
cout << contours.size();
RNG rng(12345);
Mat drawing = Mat::zeros(image.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());
}
/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image); // Show our image inside it.
我认为您正在阅读 Jpeg 或您的实际输入图像的任何其他压缩版本,这些图像会增加这些噪音。查找轮廓需要二值图像。如果您想处理此图像,请在找到轮廓之前简单地应用阈值!请注意,您的源图像对我来说工作正常!
find contours 在明明只有 3 个轮廓的情况下找到这么多轮廓正常吗?
原图和绘制后找到的轮廓如下图所示:
密码是:
Mat image;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
image = imread(argv[1], 0); // Read the file
findContours(image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
cout << contours.size();
RNG rng(12345);
Mat drawing = Mat::zeros(image.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());
}
/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image); // Show our image inside it.
我认为您正在阅读 Jpeg 或您的实际输入图像的任何其他压缩版本,这些图像会增加这些噪音。查找轮廓需要二值图像。如果您想处理此图像,请在找到轮廓之前简单地应用阈值!请注意,您的源图像对我来说工作正常!