Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV's Canny Edge Detection in C++

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);
}
like image 833
Og Namdik Avatar asked Aug 16 '12 12:08

Og Namdik


1 Answers

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) )

like image 129
Sam Avatar answered Sep 30 '22 18:09

Sam