Canny edge detection - Grayscale images always appear as 3-channel unusable?
I am working on the OpenCV tutorial from the O'Reilly series and am trying to do the canny edge detection sample.
Any grayscale image I choose seems to have 3 channels, and as far as I know canny only works with single channel images, so this always fails. I even use images provided by OpenCV.
Here is my code ..
IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
if(in->nChannels != 1)
return(0); //canny only handles gray scale images
IplImage* out = cvCreateImage(cvSize(in->width, in->height), IPL_DEPTH_8U, 1);
cvCanny(in, out, lowThresh, highThresh, aperture);
return(out);
};
IplImage* img = cvLoadImage("someGrayscaleImage.jpg");
IplImage* out = doCanny(img, 10, 100, 3);
Why does it always give me 3-channel images? How can I solve this?
a source to share
You can use this method with another parameter
IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)
#define CV_LOAD_IMAGE_COLOR 1
#define CV_LOAD_IMAGE_GRAYSCALE 0
#define CV_LOAD_IMAGE_UNCHANGED -1
The default setting is the boot image with color. What you need to do is load it in grayscale
Here's an example
cvLoadImage("yourimage.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Here is a detailed explanation of this method. You can look here for more details: Open CV 2.0 Links
scolor - The specific color type of the uploaded image: if $> 0 $, the uploaded image must be a three-band color image; if 0, the loaded image should be grayscale; if $ <0 $ the loaded image will be loaded as is (note that in the current implementation the alpha channel, if any, is removed from the output image, for example a 4-channel RGBA image will be loaded as RGB).
a source to share