The Triangle method worked pretty well here! This is no guarantee that it'll work well in other cases as well. Triangle_thresh = get_range(triangle_thresh) Triangle_thresh, _ = cv2.threshold(img_blur, 0, 255, cv2.THRESH_TRIANGLE)ĭef get_range( threshold, sigma= 0.33): return ( 1-sigma) * threshold, ( 1+sigma) * threshold Let's try both of them out, as well as taking a simple median of the pixel values as the third option: otsu_thresh, _ = cv2.threshold(img_blur, 0, 255, cv2.THRESH_OTSU) OpenCV provides us with Otsu's method (works great for bi-modal images) and the Triangle method. Though, finding the threshold is what's more difficult. When sigma, is say, 0.33 - the bounds will be 0.66*threshold and 1.33*threshold, allowing a ~1/3 range around it. You can make your own calculation for some good value, and then adjust the range with a sigma around that threshold: lower_bound = ( 1-sigma)*threshold Can we try to automate this? Automated Thresholding for cv2.Canny()?Ĭan you find an optimal set of threshold values? Yes, but it doesn't always work. The values of 20 and 30 here aren't arbitrary - I've tested the method on various parameters, and chose a set that seemed to produce a decent result. Img_blur = cv2.GaussianBlur(img, ( 3, 3), 0) Img = cv2.imread( 'finger.jpg', cv2.IMREAD_GRAYSCALE) Let's load an image in and grayscale it (Canny, just as Sobel/Scharr requires images to be gray-scaled): import cv2 To find good threshold values, you'll generally experiment with different lower and upper bounds for the thresholds, or employ an automated method such as Otsu's method or the Triangle method. That's hysteresis thresholding! In effect, it helps clean up the final output and remove false edges, depending on what you classify as a false edge. If they're not connected, they're likely artifacts of a miscalculated edge. If any edge in-between the thresholds is connected to a definitive edge (ones above the threshold) - they're also considered edges. Everything in-between the lower bound and upper bound is in the "gray zone". Thresholding works in much the same way as usual - if the gradient is below a lower threshold, remove it (zero it out), and if it's above a given top threshold, keep it. You can threshold gradients, and only include the stronger ones, assuming that "real" edges are more intense than "fake" edges. Because of the various reasons these miscalculations occur - it's hard to make an automated evaluation of what an edge certainly is and isn't. Many non-edges can and likely will be evaluated as edges, due to lighting conditions, the materials in the image, etc. The concept is applicable to more tasks than this, but let's bind it to this context for now. This is known as Non-Max Suppression! The non-max pixels (ones smaller than the one we're comparing them to in a small local field, such as a 3x3 kernel) get suppressed. However, we can find the common line in the edges, and suppress the rest of the pixels around it, yielding a clean, thin separation line instead. The edges usually aren't so clear cut in images, as light diffuses gradually. It's not like someone took a pencil and drew a line to create a line art of the image. A noticeable issue with the Sobel filter is that edges aren't really clear.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |