We often take pictures of menus, receipts, and message boards etc. They are often not aligned properly. In this post, I will show you how to select the pertinet parts and allign them as correctly as possible.
Let’s get started.
1. Find the anchor points
We will first load the image of a newspaper clipping containing the sudoku puzzle. Next, we will find out the cordinates of the four corners of the puzzle in the original image.
image = "sudoku_newspaper_small.jpg" original_img = cv2.imread(image) cv2.imshow("menu orig", original_img) cv2.waitKey(0)
Once the image loads you can find the cordinates of the four corners by hovering the mouse pointer around it. Once you have noted them down, store the four corners of the sudoku puzzle in a numpy array as top_left, top_right, bottom_left, and bottom_right respectively.
pts1 = np.float32([[22,95],[290,102],[0,383],[318,370]])
2. Select and transform
Now, that we have the bounding points selected, we can apply the transformation
using the function
cv2.getPerspectiveTransform() by passing it the selected points
pts1 and target cordinates
pts2 for the size of the transformnation. I decided to eyeball the length and width of the sudoku puzzle to be of the size 250X250
pts2 = np.float32([[0,0],[250,0],[0,250],[250,250]]) M = cv2.getPerspectiveTransform(pts1,pts2)
Next, we can apply the transformation to the original image and extract the relevant section using
target_img = cv2.warpPerspective(original_img,M,(252,252)) cv2.imwrite("sudoku.jpg", target_img)
Feel free to play with other images (menus, receipts, signs etc) you have in your cameral roll :)
Stay tuned for more posts on OpenCV.