opencv canny: Do minVal and maxVal matter if you&#

2019-08-12 00:34发布

问题:

Preface: This is a continuation of this question.

Consider the following code, taken from here:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
edges = cv2.Canny(img,100,200) # Would 100 and 200 matter if your original image was black and white?

plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()

My Question:

  • When using open cv's canny function, do your choices for minVal and maxVal matter if you're working with a black and white image?

Reason I ask:

  • I've experimented with many values and they don't seem to matter.

回答1:

The threshold values do matter.


Assuming a 3x3 Sobel filter (as in Canny), the possible values for dx and dy you can get for an input binary (0, 255) image are:

-1020, -765, -510, -255, 0, 255, 510, 765, 1020

And the possible magnitude values are:

  1. L1 (default) : 0, 510, 1020, 1530.
  2. L2 : 0, 360.63, 510, 721.25, 806.38, 1020, 1081.87, 1140.40

So, you'll get different output images from Canny if you use, for example (minVal, maxVal) as (200,400) or (400,600).

If you use thresholds that are in the same interval (the boundary of the intervals are the magnitude values shown above), then you'll get always the same result.