TypeError: unorderable types: tuple() > int()

Viewed 171

I am trying the following code for deskewing an image but I am recieving error :

TypeError: unorderable types: tuple() > int()

The bug is in the following line :

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

The full code is :

import numpy as np
import argparse
import cv2

image= cv2.imread('das.jpg',0)

gray = cv2.bitwise_not(image)

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

coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)

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('a.jpg',rotated)

2 Answers

Try:

coords = np.column_stack(np.where(thresh[1] > 0))

Well I finally made it work cv2.threshold returns a tuple of 2 elements the first one is the optimum threshold value and the second is an array of the output image. So changing the bugged line to :

coords = np.column_stack(np.where(thresh[1] > 0))

After this the same problem occurs with the angle variable which is also a tuple of 3 elements. The first element provides the centre of the rectangle, the second element provides height and width and the third element provides us with the rotation angle in a clockwise direction which we need. To get the angle into desired format we need to convert it to a list so it can be formatted. Therefore the code needs to be changed to :

    angle = cv2.minAreaRect(coords)
    new_list=list(angle)

    if new_list[2] < -45:
      new_list[2] = -(90 + new_list[2])
    else:
      new_list[2]=-new_list[2]
    (h, w) = image.shape[:2]
     center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, new_list[2], 1.0)

The rest of the code remains the same.

Related