【发布时间】:2020-09-05 15:24:28
【问题描述】:
我正在尝试使用以下代码来校正图像,但收到错误消息:
TypeError: unorderable types: tuple() > int()
错误在以下行:
坐标 = np.column_stack(np.where(thresh > 0))
完整代码是:
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)
【问题讨论】:
-
请显示minimum working example。
thresh似乎是一个元组,您可能期望得到不同的结果 -
@rpoleski 我已经更新了代码。
-
我刚刚用谷歌搜索了它,found
cv2.threshold()返回一个元组。您可能应该丢弃元组的第一个元素。 -
@rpoleski 你能告诉我当我这样做时问题是如何传播到角度变量的吗?
-
不要盲目地尝试不同的东西以希望找到有用的东西,而是查看问题变量
thresh。它是什么?显然是tuple。但是什么元组?阅读文档。这是调试的基础——在你理解它之前,你无法解决问题。