title: Can Canny in OpenCV deal with both grayscale and color images?
body: <p>I have some questions about the <strong>Canny edge detector</strong> in <strong>OpenCV</strong>.</p>
  
  <p>Here is the code I tried. </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>then,</p>
  
  <pre><code>##### 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)
  </code></pre>
  
  <p>In this situation, I got an image like this:
  <img src="https://i.stack.imgur.com/xlloJ.png" alt="enter image description here"></p>
  
  <pre><code>##### second situation #####
  img = cv2.imread('mango.jpg')
  auto = auto_canny(img)
  cv2.imwrite('mango_color_edge.jpg', auto)
  </code></pre>
  
  <p>in this situation, I got an image like this:
  <img src="https://i.stack.imgur.com/ZF5nz.png" alt="enter image description here"></p>
  
  <p>and this is the original image:
  <img src="https://i.stack.imgur.com/45TAU.jpg" alt=""></p>
  
  <p>The different between the first situation and the second situation is whether I convert the color image to grayscale. <strong>But</strong>, I am confused about this. Because, </p>
  
  <p>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. </p>
  
  <p>And the edge is much better when I didn’t convert it to grayscale, just like in second situation.</p>
  
  <p>So my question is, </p>
  
  <ol>
  <li><p>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 <code>cv2.Canny()</code>?</p></li>
  <li><p>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? </p></li>
  <li><p>How does Canny actually work? Could Canny deal with color image directly or must the color image be converted to grayscale first? </p></li>
  <li><p>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?</p></li>
  </ol>