Improving the image for 4-point contour approximation

Hi everyone,

I’m creating a YuGiOh card detector based exclusively on OpenCV and Tesseract OCR.
First I make a binary image and then find the contours that can be approximated with 4 points. The problem is that sometimes the edges of the borders are smoothed out as seen in the example below:

As you can see the edges on the card in the middle are quite smooth even though they are not in the original image which is why the 4-point approximation fails I presume.

This is the code for getting the binary image.

    #grayscale -> bluring -> canny thresholding -> dilation
    gray_img = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray_img, (11,11), 0)
    thresh = cv2.Canny(blur, 50, 100)
    dilated = cv2.dilate(thresh, np.ones((11,11), dtype=np.int8))

This is the code for 4-point contour approximation.

    tbd = list()
    for c in contours:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.05 * peri, True)
        if len(approx) == 4:
            tbd.append(approx)

If someone could point me in the right direction to fix this type of problem I would be very grateful. Thanks in advance! :slight_smile: