Rotate an image with text

asked 2018-05-15 07:31:50 -0500

I have an image as shown image description I need to convert this image to correct orientation. How can achieve this? This is the code i wrote,

image = cv2.imread("borderRemoved.png")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.bitwise_not(gray)

thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

coords = np.column_stack(np.where(thresh > 0))

angle = cv2.minAreaRect(coords)[-1]

if angle < -45:

angle = -(90 + angle)

else:

angle = -angle

(h, w) = image.shape[:2]

center = (w // 2, h // 2)

M = cv2.getRotationMatrix2D(center, angle, 1.0)

rotated = cv2.warpAffine(image, M, (w, h),flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

cv2.imwrite("Rotated.png",rotated)

Thanks in advance.

edit retag flag offensive close merge delete

Comments

how will you find out, which way to rotate it ?

berak gravatar imageberak ( 2018-05-15 07:55:02 -0500 )edit

That's the solution i needed.

NIDHEESH gravatar imageNIDHEESH ( 2018-05-15 07:57:13 -0500 )edit

maybe a simple cv2.rotate() ?

berak gravatar imageberak ( 2018-05-16 02:51:50 -0500 )edit

No its not possible. Which angle should i take to rotate this image? I need a general solution for rotating an image

NIDHEESH gravatar imageNIDHEESH ( 2018-05-16 03:08:29 -0500 )edit

yea, that was my question, before.

berak gravatar imageberak ( 2018-05-16 04:26:49 -0500 )edit