cv2.threshold()错误(-210)

Iva*_*rew 6 python opencv image-processing computer-vision

我是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)
Run Code Online (Sandbox Code Playgroud)

但是这段代码失败了:

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

为什么会导致"-210"错误?

for*_*ord 18

可以查找OpenCV 错误代码types_c.h.

错误代码-210定义为:

CV_StsUnsupportedFormat= -210, /**< the data format/type is not supported by the function*/
Run Code Online (Sandbox Code Playgroud)

因此,在将图像uint8传递给数据之前,您需要将其强制转换为数据类型cv2.threshold.这可以使用astype方法numpy完成:

afterFourier = afterFourier.astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)

这会将所有浮点值截断afterFourier为8位值,因此您可能需要在执行此操作之前对数组进行一些缩放/舍入,具体取决于您的应用程序.