Is there any way that I can straighten this image using OpenCV with Python? I was figuring it out using the different transformations but I cant get it.
Here is my code:
rows, cols, h = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
And then I apply Affine Transformation.
dst = cv2.warpAffine(roi, M, (cols, rows))
Still I cant get the desired output of the image to be straighten. Scratching my head for almost an hour now. Anyone can help me please?
Do you remember my previous post? This answer is based on that.
So I obtained the 4 corner points of the bounding box around the book and fed it into the homography function.
Code:
#---- 4 corner points of the bounding box
pts_src = np.array([[17.0,0.0], [77.0,5.0], [0.0, 552.0],[53.0, 552.0]])
#---- 4 corner points of the black image you want to impose it on
pts_dst = np.array([[0.0,0.0],[77.0, 0.0],[ 0.0,552.0],[77.0, 552.0]])
#---- forming the black image of specific size
im_dst = np.zeros((552, 77, 3), np.uint8)
#---- Framing the homography matrix
h, status = cv2.findHomography(pts_src, pts_dst)
#---- transforming the image bound in the rectangle to straighten
im_out = cv2.warpPerspective(im, h, (im_dst.shape[1],im_dst.shape[0]))
cv2.imwrite("im_out.jpg", im_out)
Since you have the contour bounding box around the book; you have to feed those 4 points into the array pts_src
.