cv2.threshold()错误(-210)

2024-04-28 11:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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”错误?


Tags: importsrcimgthreshold定义asnperror
1条回答
网友
1楼 · 发布于 2024-04-28 11:57:26

OpenCV error codes可以在^{}中查找。

错误代码-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位值,因此您可能需要在执行此操作之前对数组进行一些缩放/舍入操作,具体取决于您的应用程序。

相关问题 更多 >