cv2.threshold()错误 (-210)

7 投票
1 回答
14192 浏览
提问于 2025-04-18 06:02

我刚开始学习Python。

我想通过傅里叶变换来实现文本的旋转。

import cv2
import numpy as np
import matplotlib.pyplot as plot

img = cv2.imread ('Text_rot.bmp', cv2.CV_LOAD_IMAGE_GRAYSCALE)
afterFourier =  np.log (np.abs(np.fft.fft2 (img)))
ret1, th1 = cv2.threshold (afterFourier, 127, 255, cv2.THRESH_BINARY)

但是这段代码出错了,显示:

ret1, th1 = cv2.threshold (afterFourier, 127, 255, cv2.THRESH_BINARY)
error: ..\..\..\src\opencv\modules\imgproc\src\thresh.cpp:783: error: (-210) 

为什么会出现“-210”的错误呢?

1 个回答

19

OpenCV的错误代码可以在types_c.h中查找。

错误代码-210的定义是:

CV_StsUnsupportedFormat= -210, /**< the data format/type is not supported by the function*/

所以,在把你的图像传给cv2.threshold之前,你需要把它转换成uint8这种数据类型。这可以通过numpy的astype方法来实现:

afterFourier = afterFourier.astype(np.uint8)

这样会把afterFourier中的所有浮点值都截断为8位值,因此根据你的应用需求,你可能需要在执行这个操作之前对数组进行一些缩放或四舍五入。

撰写回答