C++ 在 OpenCV 中查找轮廓?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8449378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Contours in OpenCV?
提问by fdh
When you retrieve contours from an image, you should get 2 contours per blob - one inner and one outer. Consider the circle below - since the circle is a line with a pixel width larger than one, you should be able to find two contours in the image - one from the inner part of the circle and one from the outer part.
从图像中检索轮廓时,每个 blob 应该得到 2 个轮廓 - 一个内部和一个外部。考虑下面的圆圈 - 由于圆圈是像素宽度大于 1 的线,您应该能够在图像中找到两个轮廓 - 一个来自圆圈的内部,一个来自外部。
Using OpenCV, I want to retrieve the INNER contours. However, when I use findContours (), I only seem to be getting the outer contours. How would I retrieve the inner contours of a blob using OpenCV?
使用 OpenCV,我想检索内部轮廓。但是,当我使用 findContours() 时,我似乎只获得了外部轮廓。如何使用 OpenCV 检索 blob 的内部轮廓?
I am using the C++ API, not C therefore only suggest functions that use the C++ API. (i.e. findContours () rather than cvFindContours ())
我使用的是 C++ API,而不是 C,因此只建议使用 C++ API 的函数。(即 findContours() 而不是 cvFindContours())
Thanks.
谢谢。
回答by SSteve
I ran this code on your image and it returned an inner and outer contour.
我在你的图像上运行了这段代码,它返回了一个内部和外部轮廓。
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
int main(int argc, const char * argv[]) {
cv::Mat image= cv::imread("../../so8449378.jpg");
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);
return 0;
}
Here are the contours it found:
这是它找到的轮廓:
回答by Kacem Boufelliga
I think what Farhad is asking is to crop the contour from the original image.
我认为 Farhad 要求的是从原始图像中裁剪轮廓。
To do that you will need to find the contour as explained above, then use a mask to get the inside from the original, and then crop the result into an image with the same size as the contour.
为此,您需要找到上述轮廓,然后使用蒙版从原始图像中获取内部,然后将结果裁剪为与轮廓大小相同的图像。
回答by Dangelo Guanipa
The findcontours function stores all the contours in different vectors, in the code given all contours are drawn, you just have draw the contour corresponding to the inner one, idx is the variable that states which contour is drawn.
findcontours 函数将所有轮廓存储在不同的向量中,在给出所有轮廓的代码中,您只需绘制与内部轮廓相对应的轮廓,idx 是说明绘制哪个轮廓的变量。