Liris
Liris

Reputation: 1511

Python opencv detecting chessboard

I'm trying to get the corners of a chessboard pattern, and thought I could use openCV with cv2.findChessboardCorners.

However, I can't find the good arguments to pass to the function such that it succeeds in detecting the chessboard. I thought the image would be appropriated for this function. If it is not, I can't figure out what king of preprocessing I should do ..

Here is my code:

import numpy as np
import cv2
import glob
import sys
import os

nline = 4
ncol = 4 

img = cv2.imread(glob.glob('*.jpg')[0])

## termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

## processing
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nline, ncol), None)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

and this is the image :enter image description here

I have tried various pattern size to give him, because I thought that a 4*4 pattern would be easier to find but it wan not.

EDIT : Actually using the appropriated number of lines and columns its works : enter image description here

However, it doesn't for these ones which are not so different from the first one ... enter image description here 11 lines, for 8 or 9 columns, tried both.

Or this one : enter image description here which is 13*9.

How to deal with this kind of images ?

Upvotes: 4

Views: 14311

Answers (1)

zteffi
zteffi

Reputation: 670

Parameters nline and ncol must be exact, (number of rows -1, number colums -1)

nline = 11
ncol = 9

Upvotes: 3

Related Questions