This is my test photo
I am trying to find the edges of the card. However, as you can see, the edges are somewhat blurry.
To find the edges i first enhance the contrast of the image so hopefully blurry edges will be less blurry and much more easier to find:
Then i used Gaussian Blur to smooth it a little (I tried removing Gaussian blur, but the the edge detector found to many details in the background + in the card).
Then i used canny with "dynamic thresholds" and got the following result:
As you can see, i barley found any edges of the card (except the left one, which was easy because of the dark background). Is there a robust (i don't want to "over fit" on this image) method to find straight blurry edges?
Found some suggestions here: Blurry edge detection How to find accurate corner positions of a distorted rectangle from blurry image in python?, but none resulted in satisfying edges.
The full code:
def auto_canny(image, sigma=0.5):
v = np.median(image)
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
return cv2.Canny(image, lower, upper)
def add_contrast(img, contrast_level=8):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(contrast_level, contrast_level))
cl = clahe.apply(l)
limg = cv2.merge((cl, a, b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return final
# ------------------------------------------ #
# FIND EDGES
# ------------------------------------------ #
img = add_contrast(img=img, contrast_level=8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
edges = auto_canny(image=blur_gray)
# Show images for testing
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()