Home > OS >  Detecting squares in a chessboard OpenCV
Detecting squares in a chessboard OpenCV

Time:04-15

I detected a chessboard using OpenCV in python using:

  • Computing the edge of the image
  • Computing the Hough transform
  • Finding the local maxima of Hough transform
  • Extracting the image lines

Then I used findContours and drawContours functions:

  im_gray = cv2.imread('redLines.png', cv2.IMREAD_GRAYSCALE)
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))  
  morphed = cv2.dilate(im_gray, kernel, iterations=1)
  (ret, thresh) = cv2.threshold(morphed, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cv2.drawContours(thresh, contours, -1, (255, 255, 255), 3)

And it worked well, the last imshow looks something like this:

enter image description here

Now, I'm trying to detect each square in the grid and save its points in a unique index in a vector.

I know I can do it using the contours array. But when I print the length of the contours it keeps changing rapidly, from size 2 up to 112..

So I guess it's not recognizing the grids well.

Any help would be appreciated.

CodePudding user response:

An approach is to use enter image description here

Isolated squares

enter image description here

import cv2
import numpy as np

# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("1.png")
mask = np.zeros(image.shape, dtype=np.uint8)
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY   cv2.THRESH_OTSU)[1]

# Remove noise with morph operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening

# Find contours and find squares with contour area filtering   shape approximation
cnts = cv2.findContours(invert, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4 and area > 100 and area < 10000:
        x,y,w,h = cv2.boundingRect(c)
        cv2.drawContours(original, [c], -1, (36,255,12), 2)
        cv2.drawContours(mask, [c], -1, (255,255,255), -1)

cv2.imshow("original", original)
cv2.imshow("mask", mask)
cv2.waitKey()
  • Related