-1

I am trying perspective transformation example from the documentation but I am getting different result than the example.

import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('sudoku.png')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

The result according to the example should look like (ignore the green lines) enter image description here

whereas what I got looks like

Any ideas what's going on here? I am on macOS 10.13, openCV 3.3.1 and using python 3.6

Kinght 金
  • 17,681
  • 4
  • 60
  • 74
Coddy
  • 549
  • 4
  • 18
  • What's the size of the input image? How did you selected the pts1 points? – Miki Nov 06 '17 at 06:44
  • It was all given in the documentation example. – Coddy Nov 07 '17 at 05:52
  • You can clearly see from the x,y axis that you image is bigger than the one you show with the green cross... Come on, a little effort... – Miki Nov 07 '17 at 07:52

1 Answers1

1

You have the wrong coords.

## sudo.png (563, 558, 3)

## tl->tr->br->bl
pts1 = np.float32([[76,85],[490,70],[520,520],[35,512]])
pts2 = np.float32([[0,0],[300,0],[300,300], [0,300]])

enter image description here

Kinght 金
  • 17,681
  • 4
  • 60
  • 74
  • Are you using matplotlib to plot? I am still not getting the result. – Coddy Nov 07 '17 at 05:54
  • the correct order that worked for me is tl -> tr -> bl -> br – Coddy Nov 12 '17 at 05:51
  • And are you still trapped in this? Here is an interesting post for more advanced topic, I'm trying to solve it. You can have a look. https://stackoverflow.com/questions/10196198/how-to-remove-convexity-defects-in-a-sudoku-square/11366549 – Kinght 金 Nov 12 '17 at 05:56