# Unwanted 180 degree rotation in find homography matrix of chessboard pattern

**URL:** https://forum.opencv.org/t/unwanted-180-degree-rotation-in-find-homography-matrix-of-chessboard-pattern/7286
**Category:** Python
**Tags:** calib3d, homography
**Created:** [January 26, 2022, 6:40am UTC](https://forum.opencv.org/t/unwanted-180-degree-rotation-in-find-homography-matrix-of-chessboard-pattern/7286 "2022-01-26T06:40:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Nand\_Kumar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nand_kumar/32/4332_2.png) [@Nand\_Kumar](https://forum.opencv.org/u/Nand_Kumar)
#### Post date: [January 26, 2022, 6:40am UTC](https://forum.opencv.org/t/unwanted-180-degree-rotation-in-find-homography-matrix-of-chessboard-pattern/7286/1 "2022-01-26T06:40:51Z")

</div>

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

```auto
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)
```

---

<div class="post-metadata">

### Author: ![Alejandro\_Silvestri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alejandro_silvestri/32/357_2.png) [@Alejandro\_Silvestri](https://forum.opencv.org/u/Alejandro_Silvestri)
#### Post date: [January 26, 2022, 11:27am UTC](https://forum.opencv.org/t/unwanted-180-degree-rotation-in-find-homography-matrix-of-chessboard-pattern/7286/2 "2022-01-26T11:27:04Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [January 27, 2022, 11:21am UTC](https://forum.opencv.org/t/unwanted-180-degree-rotation-in-find-homography-matrix-of-chessboard-pattern/7286/3 "2022-01-27T11:21:00Z")

</div>

> [@Alejandro\_Silvestri](#):
>
> so there are two possible homography solutions, one being the 180º rotated version of the other.

indeed, nice find !

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