OpenCV's Canny Edge Detection in C++

天大地大妈咪最大 提交于 2019-11-26 16:18:48

问题


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 image generated 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);
}

回答1:


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.

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.

Edit

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

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

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

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



来源:https://stackoverflow.com/questions/11987483/opencvs-canny-edge-detection-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!