gpt4 book ai didi

c++ - OpenCV cv::findcontours StackOverflow 异常

转载 作者:行者123 更新时间:2023-11-28 00:28:35 25 4
gpt4 key购买 nike

我有一个 3 channel cv::Mat(RGB 480 高 x 640 宽),我想找到这张图片的轮廓。我尝试了不同的例子: This one This one但是每次我在行中遇到 stackoverflow 错误时:

cv::findContours(contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);

cv::findContours(image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

我注意了所涉及的 cv::Mat 的大小以及它们的 channel 数。我不知道还能做什么。经过几次操作,我的代码是这个,我总是在 findcontours 中得到堆栈溢出。

                cv::Mat src_gray;
cv::Mat dst;
cv::Mat canny_output;
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;

cvtColor(src, src_gray, CV_BGR2GRAY);

int threshold_value = 0;
int threshold_type = 3;
int max_BINARY_value = 4;
threshold(src_gray, dst, threshold_value, max_BINARY_value, threshold_type);


/// Detect edges using canny
Canny(dst, canny_output, 100, 100 * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

我得到的是:xxx.exe 中发生类型为“System.StackOverflowException”的未处理异常

最佳答案

findContours() 的输入必须是 channel 图像。您不能将 3 channel BGR 图像传递给它。

解决方案是将您的输入图像转换为灰度图像。例如。 cvtColor(src, src_gray, CV_BGR2GRAY);

当然它不一定是灰度图像,您可以使用 split() 从图像中提取和使用任何单个 channel 。例如。色相、饱和度、明度、B、G、R……等

此外,findContours() 最适合二进制 输入图像,因此您可能希望在将单 channel 图像传递给 findContours() 之前对其进行阈值处理

关于c++ - OpenCV cv::findcontours StackOverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23855257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com