OpenCV 在 C++ 中的 Canny 边缘检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11987483/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:43:53  来源:igfitidea点击:

OpenCV's Canny Edge Detection in C++

c++opencvcontouredge-detection

提问by Og Namdik

I want to extract the edges of hand but I get the following result. I've tried adjusting the low and high threshold but I still can't get the desired output. I have included below the code and its output. What seems to be the problem?

我想提取手的边缘,但得到以下结果。我已经尝试调整低阈值和高阈值,但仍然无法获得所需的输出。我在代码及其输出下面包含了。似乎是什么问题?

This is the output imagegenerated by the code below.

这是由下面的代码生成的输出图像

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(){

    cv::Mat image= cv::imread("open_1a.jpg");
    cv::Mat contours;
    cv::Mat gray_image;

    cvtColor( image, gray_image, CV_RGB2GRAY );

    cv::Canny(image,contours,10,350);

    cv::namedWindow("Image");
    cv::imshow("Image",image);

    cv::namedWindow("Gray");
    cv::imshow("Gray",gray_image);

    cv::namedWindow("Canny");
    cv::imshow("Canny",contours);
    cv::waitKey(0);
}

回答by Sam

Change this line

改变这一行

cvtColor( image, gray_image, CV_RGB2GRAY );

to

std::vector<cv::Mat> channels;
cv::Mat hsv;
cv::cvtColor( image, hsv, CV_RGB2HSV );
cv::split(hsv, channels);
gray_image = channels[0];

The problem seems to be that your hand in gray scale is very close to the gray background. I have applied Canny on the hue (color) because the skin color should be sufficiently different.

问题似乎是您的灰度手与灰色背景非常接近。我在色调(颜色)上应用了 Canny,因为肤色应该足够不同。

Also, the Canny thresholds look a bit crazy. The accepted norm is that the higher one should be 2x to 3x the lower. 350 is a bit too much and it doesn't help solve the main problem.

此外,Canny 阈值看起来有点疯狂。公认的规范是较高的应该是较低的 2 到 3 倍。350有点太多了,无助于解决主要问题。

Edit

编辑

with these thresholds I was able to extract quite a good contour

通过这些阈值,我能够提取出相当不错的轮廓

cv::Canny(image,contours,35,90);

cv::Canny(图像,轮廓,35,90);

Reading a bit of theory about the algorithm will help you understand what happens and what you should do to improve. wiki cannyon google

阅读有关该算法的一些理论将帮助您了解会发生什么以及您应该如何改进。wiki canny在谷歌上

However, the improvement above will give you much better results (provided you use better thresholds than 10, 350. Try (40, 120) )

但是,上述改进将为您提供更好的结果(假设您使用比 10, 350 更好的阈值。尝试 (40, 120) )