You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

图像去歪斜代码报错TypeError: tuple() > int()问题求助

解决图像去歪斜处理中的TypeError问题

我瞅了下你的代码,问题根源很明确!cv2.threshold函数会返回两个值:第一个是计算出的阈值,第二个才是我们需要的二值化图像。你直接把整个返回的元组thresh拿去和0比较,自然会触发tuple() > int()的类型错误啦。

另外还有个隐藏小问题:cv2.minAreaRect(coords)返回的是包含矩形中心、尺寸和旋转角度的元组,你直接把整个元组赋值给angle,后面判断angle < -45也会出错,得取元组的第三个元素才是真正的旋转角度。

修正后的完整代码

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))
# 提取minAreaRect返回的第三个元素:旋转角度
angle = cv2.minAreaRect(coords)[2]

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)

这样修改后,代码就能正常运行,完成图像去歪斜的处理了。

内容的提问来源于stack exchange,提问作者Akhilesh Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:47:47