16

I have some questions about the Canny edge detector in OpenCV.

Here is the code I tried.

def auto_canny(image, sigma=0.33):
    v = np.median(image)
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    edged = cv2.Canny(image, lower, upper)

then,

##### first situation #####
img = cv2.imread('mango.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
auto = auto_canny(gray)
cv2.imwrite('mango_gray_edge.jpg', auto)

In this situation, I got an image like this: enter image description here

##### second situation #####
img = cv2.imread('mango.jpg')
auto = auto_canny(img)
cv2.imwrite('mango_color_edge.jpg', auto)

in this situation, I got an image like this: enter image description here

and this is the original image:

The different between the first situation and the second situation is whether I convert the color image to grayscale. But, I am confused about this. Because,

In the first situation, I convert the image to grayscale, so the image has a single channel. And in the second situation, the image still has three channels.

And the edge is much better when I didn’t convert it to grayscale, just like in second situation.

So my question is,

  1. Does the Canny function in OpenCV include method to convert image to grayscale? I mean, do I need to convert the image to grayscale before I use cv2.Canny()?

  2. How does Canny deal with both single channel (first situation convert to grayscale) and three channels (original image like second situation) image at the same time?

  3. How does Canny actually work? Could Canny deal with color image directly or must the color image be converted to grayscale first?

  4. From the steps of Canny, when we calculate gradient, I think it should be single channel, then it can be calculated. If the image is color (three channels), how can we calculate the gradient? Do we calculate three channels separately? How does it work?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
XV15
  • 161
  • 1
  • 1
  • 3
  • **All color channels matter for edge detection.** “[_Color edge detection_](http://ai.stanford.edu/~ruzon/compass/color.html)” is a thing. I’d like to remind you that there’s many different sets of colors that could be grayscaled into the _same_ (“pseudoisochromatic”) grays. – Константин Ван Nov 17 '22 at 10:34

1 Answers1

11

Yes, OpenCV's Canny implementation can handle multiple channels.


Remember that OpenCV is open-source, so you can just check the implementation to find this kind of information.

Basically, what Canny does before non-maxima suppression is to compute the magnitude (norm) of the gradient for each pixel, according to Sobel derivatives (dx and dy). If you have more than one channel, as you can see in the source code, the magnitude of the gradient for a given pixel is the highest among all the ch channels.

Miki
  • 40,887
  • 13
  • 123
  • 202
  • It can handle color images? When did they commit that? My book says that Canny only handles greyscale. – Rich Apr 28 '17 at 05:40
  • So, 3 channel image is prefered over 1 channel image to perfom Canny edge detection? I'm just curious. – Scott Apr 09 '20 at 08:42
  • @Sherzod usually you'll use a 1ch image. OpenCV probably handles also the 3ch case just for easy of use, not necessarily for better results. – Miki Apr 09 '20 at 09:06
  • @Miki - But results are different and the result with 3 channel image looks better. Or, is it different case by case? If so, do we have to check both methods and pick better one every time? Thanks for you answer :) – Scott Apr 09 '20 at 09:22
  • @Miki LTTP, but I would also like an answer to Sherzod's question if you don't mind. – viracocha Nov 13 '20 at 14:43
  • @viracocha define _"better"_ first ;) – Miki Nov 13 '20 at 14:49
  • @Miki As you wish. I define the “_better_” as “_closer to how non-colorblind humans perceive edges_.” **Supplying the `::Canny()` method channels as many as possible _is_ necessary for better results.** Imagine how the results would vary when you feed it two versions of [pseudoisochromatic images](https://en.wikipedia.org/wiki/Color_vision_test#Pseudoisochromatic_plates), original and grayscaled. Those who are colorblind and those not recognize edges differently _because of the color inputs and their colorful contrasts_. (By “colorblind,” I mean “achromatopsia.”) – Константин Ван Nov 17 '22 at 09:52
  • [The link to the source code in your answer](https://github.com/opencv/opencv/blob/master/modules/imgproc/src/canny.cpp#L1037) is broken. Here’s [the latest permalink](https://github.com/opencv/opencv/blob/08906ddd4b57f6e93143b0f0bf861b782f5d7877/modules/imgproc/src/canny.cpp#L481-L498). – Константин Ван Nov 17 '22 at 10:49