6

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.

Image

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?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
rbo13
  • 365
  • 3
  • 5
  • 14

1 Answers1

10

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)

enter image description here

Since you have the contour bounding box around the book; you have to feed those 4 points into the array pts_src.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • ahhh okay... Never thought that I could apply it in this scenario. Thanks dude. Just one question, how did you get this value? `pts_src = np.array([[17.0,0.0], [77.0,5.0], [0.0, 552.0],[53.0, 552.0]])` `pts_dst = np.array([[0.0,0.0],[77.0, 0.0],[ 0.0,552.0],[77.0, 552.0]])` `im_dst = np.zeros((552, 77, 3), np.uint8)` – rbo13 Feb 02 '17 at 10:02
  • I manually drew circles at the corners of the bounding box. All you have to do is obtain these points from the contour you have got – Jeru Luke Feb 02 '17 at 10:41
  • How do I obtain the points from my contour? Because when I print it, it returns a collection. And I dont know where to start from that. Hope you can give me suggestion. Much appreciated. – rbo13 Feb 02 '17 at 12:26
  • how did you find and draw contours? After finding the contours apply the following code for the contour: `rect = cv2.minAreaRect(cnt)`; `box = cv2.boxPoints(rect)`; `box = np.int0(box)`. The four corners will be inside `box`. See [THIS DOC](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html#contour-features) for more info – Jeru Luke Feb 02 '17 at 12:38
  • Hi @JeruLuke. I will feed the `box` at `cv2.findHomography()` ? Here's my progress so far in this [link](http://pastebin.com/qpTz7Sk7) – rbo13 Feb 03 '17 at 03:29
  • @whaangbuu Yes the corners of the box you want the image in must be fed to `pts_dst` of `cv2.findHomography()` – Jeru Luke Feb 03 '17 at 04:16
  • @whaangbuu I saw your code. You have not used the **bounding box** property. Visit bounding rectangle on [THIS PAGE](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html#contour-features). It creates a rectangle around your contour. That rectangle contains the points of the contour. You can insert these points into `pts_dst` in the code :D – Jeru Luke Feb 03 '17 at 14:49