OpenCV's Canny Edge Detection in C++
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) )
How to process the result of OpenCV:: Canny Edge Detection
edges
is array (Mat) of the same size as source picture, and it contains zero pixels and max value (255) pixels at edges that Canny function found
You can emphasize edges at source image (by white color), making bitwise_or
operation with edges (in my). Or transform edges to color image to draw color edges. Here is example of using edges as mask.
Edges is raster result. To get set of segments, use findContours function on edges
, then you can use drawContours
Note that this information is well-googlable.
Get angle from OpenCV Canny edge detector
canny
doesn't give you this directly.
However, you can calculate the angle from the Sobel transform, which is used internally in canny()
.
Pseudo code:
cv::Canny(image,contours,10,350);
cv::Sobel(image, dx, CV_64F, 1, 0, 3, 1, 0, cv::BORDER_REPLICATE);
cv::Sobel(image, dy, CV_64F, 0, 1, 3, 1, 0, cv::BORDER_REPLICATE);
cv::Mat angle(image.size(), CV_64F)
foreach (i,j) such that contours[i, j] > 0
{
angle[i, j] = atan2(dy[i,j], dx[i , j])
}
OpenCV's Canny Detection slow on more complicated image
That behavior is expected, indeed.
There are 3 ways to speed things up:
OpenCV can benefit from an Intel processor, if you have one you can install Intel IPP. You might have to compile OpenCV on your own to enable this feature.
Use OpenCV's GPU module. The method
gpu::Canny()
provides an implementation of Canny that runs on the GPU. OpenCV can run certain algorithms on the GPU if your video card supports CUDA or OpenCL. You might have to compile OpenCV on your own to enable this feature.Investigate a different approach: sometimes a different set of algorithms can achieve the same result in a smaller amount of time. I talked briefly about this on your other question.
Related Topics
Find Available Network Interfaces in C/C++
Clion C++ Can't Read/Open .Txt File in Project Directory
What Rules Does Compiler Have to Follow When Dealing with Volatile Memory Locations
Any Good Reason Why Assignment Operator Isn't a Sequence Point
C++ Sqrt Function Precision for Full Squares
C++ Delete Does Not Free All Memory (Windows)
Why Are Designated Initializers Not Implemented in G++
C++ Class Member Function Callback
Why Does Destructor Disable Generation of Implicit Move Methods
Eclipse Mingw C++ Cannot Find -Lpthread
How to Embed the Gnu Octave in C/C++ Program
How Are Objects Stored in Memory in C++
Priority Queue with Limited Space: Looking for a Good Algorithm