Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bounding rectangle to get rotation angle not working (OpenCV/Python)

I'm trying to straighten this letter but I'm not sure how to use the bounding rectangle to do so.

Here's what I have so far:

import cv2
import numpy as np

img = cv2.imread('rtes.jpg',0)

ret,thresh = cv2.threshold(img,127,255,0)
ret,contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#cnt = contours[2]
#cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(255,0,0),2)
cv2.imshow('img',img)

k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()

and here's the result: enter image description here

I've looked into OpenCV docs to get it working but wasn't able to get it working. I'm trying to eventually write a loop that'll try 4 angles to see if it's straight.

like image 283
222 Avatar asked Mar 29 '16 19:03

222


People also ask

How do you rotate a picture with a bounding box?

To rotate the image, use the orientation provided in the Exif metadata in your code. Return bounding box coordinates already oriented to 0 degrees. To show the bounding box in the correct position, use the coordinates that were returned. You do not need to translate them.

How do you rotate Yolo bounding boxes?

How to Rotate the Yolo_mark Format Bounding Box? To rotate a point (x,y) by θ, we need to multiply it by the rotation matrix. A point (x,y) will be rotated counterclockwise by angle θ.

How do I get bounding box in OpenCV?

To draw a bounding box around an object in the given image, we make use of a function called selectROI() function in OpenCV. The image on which the bounding box is to be drawn using selectROI() function is read using imread() function.

How to find the rotation angle of a rectangle?

Allocate the destination image with the size of the rectangle. Where x0,y0 is the coordinate of the upper-left corner of the rectangle and theta is the rotation angle. I haven't tested this, so there may be an error in this definition but it should be a good start.

What is the return type of every function in OpenCV?

Every function in the OpenCV is bound to return some numeric data or lists of data. In some cases where you have operations on the images, you might get ‘None’ as a return. CV2 Boundingrect returns 4 numeric values when the contour is passed as an argument.

How to get the angle 45 degrees in Python?

The arctan (rise/run) function returns the angle in radians. Multiplying that radians by 180 divided by pi gives you the angle in degrees. That is, if the rise over run equals 1, then the angle returned is 45 degrees. is there a Python solution for this?

How do I get the value of an object in OpenCV?

There are two methods in OpenCV to derive a bounding rectangle and extract the relevant values of an object or series of objects in an image. For both methods, it’s first necessary to obtain a binary image of the object of interest from an image.


1 Answers

cv2.minAreaRect(cnt)

returns a Box2D object, which already stores rotation information.

(x,y),(width,height),theta

where theta is the angle enclosed by the horizontal and the first side of the Box2D object, in your example the shorter side of the inner contour.

It depends on what you're trying to achieve as an end result, but if you're only trying to rotate this particular example, you could do it this way:

rows, cols = img.shape
rect = cv2.minAreaRect(cnt)
center = rect[0]
angle = rect[2]
rot = cv2.getRotationMatrix2D(center, angle-90, 1)
print(rot)
img = cv2.warpAffine(img, rot, (rows,cols))
like image 128
s1hofmann Avatar answered Sep 29 '22 11:09

s1hofmann