views:

393

answers:

1

I am working through the book "Learning OpenCV" from the O'Reilly series and am trying to perform a canny edge detection sample.

Any grayscale image I choose seems to come up as having 3 channels, and to the best of my knowledge, canny only works with single channel images, so this always fails. I am even using the 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 might this always give me 3-channel images? How can I solve this?

+2  A: 

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 parameter is load image with color. What you have to do is to load it with grayscale

Here is an example

cvLoadImage("yourimage.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Here is the detail explanation for that method. You can look at here for more details: Open CV 2.0 References

scolor – Specific color type of the loaded image: if $ > 0 $, the loaded image is forced to be a 3-channel color image; if 0, the loaded image is forced to be grayscale; if $ < 0 $, the loaded image will be loaded as is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB).

vodkhang
awesome, thanks! i did not realize that parameter was being defaulted.
cvcentral
that page is a good page for OpenCV. I always used it when I worked with openCV before
vodkhang