guy blau
guy blau

Reputation: 9

Detecting rectangular corners with python cv2

I'm trying to implement an optical mark-recognition with python opencv. As part of the program, I used a pre-"filter" to transform the original image into a binary one and then use contour detection. At first glance, the binary images looks great, but when applying the contour detection on some images it works and on others it doesn't (even though they are really similar).

first this is the python code for the preprocessing "filters":

def PreProcessing(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    ret, imgThresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    return imgThresh

Second, the function for finding the corners:

def findCorners(canny):
    contourCounter = 0
    points1 = []
    points2 = []
    contours, _ = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 400000 and area < 2500000:
            epsilon = 0.001 * cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, epsilon, True)
            for point in approx:
                x, y = point[0]
                if contourCounter == 0:
                    points1.append((x, y))
                elif contourCounter == 1:
                    points2.append((x, y))
            contourCounter += 1
    if contourCounter != 2:
        raise Exception(f"the number of big contours detacted is {contourCounter}")
    return points1 , points2

Here are two images by way of example:

One that works:

original original

result result

and one that doesn't:

original original

result result

Upvotes: 1

Views: 61

Answers (0)

Related Questions