Unwanted 180 degree rotation in find homography matrix of chessboard pattern

Hi, I am testing python code for image perspective correction. I am using a chessboard pattern of 6 rows and 4 columns to obtain the homography matrix using cv2.findHomography which is intended for perspective correction (using cv2.warpPerspective) of a larger image. The two sets of points are obtained from the image of the chessboard and the actual .jpg file (created using Inkscape) using cv2.findChessboardCorners. Everything looks to work fine but in reality, the transformed chessboard image is rotated by 180 degrees. Please help to get unrotated transformation
The code being used is given below

def getChessCorners(imgPath,row,col):
    criteria = (cv2.TERM_CRITERIA_EPS + 
    cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    img = cv2.imread(imgPath)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (row, col), 
    None)
    # If found, add object points, image points (after refining them)
    if ret == True:
        # objpoints.append(objp)
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        return corners2

def Main()
    fileTemp="D:\PyCharm-BMI20OCT2021\images\chessSingle.jpg"
    fileImg="D:\PyCharm- 
    BMI20OCT2021\images\chessImageSingle.jpg"
    refChessPoints=getChessCorners("D:\PyCharm- 
    BMI20OCT2021\images\chessSingle.jpg",6,4)
    imgChessPoints=getChessCorners("D:\PyCharm- 
    BMI20OCT2021\images\chessImageSingle.jpg",6,4)
    points1=refChessPoints
    points2=imgChessPoints
    h, mask = cv2.findHomography(points2, points1, cv2.RANSAC,5.0)
    print(h)
    im2=cv2.imread(fileImg,0)
    im1=cv2.imread(fileTemp,0)
![chessSingle|353x500](upload://4ADFrVuwOztrcBd0a7pMOCsGs6t.jpeg)

    height,width=im1.shape
    print("height({}) Width({})".format(height,width))
    im2Reg = cv2.warpPerspective(im2, h, (width,height))
    cv2.imshow("transformed chessboard", im2Reg)
    cv2.waitKey(0)
cv2.imwrite("D:\PyCharm-BMI20OCT2021\images\chessImageTransformed.jpg",im2Reg)

@Nand_Kumar

I noticed you are using a symmetrical pattern, so there are two possible homography solutions, one being the 180º rotated version of the other.

1 Like

indeed, nice find !

to avoid this, one side of the chessboard should be odd, the other even,
e.g. [7x4] , [8x5] or [9x6]

2 Likes