OpenCV 错误:getRectSubPix 中不支持的格式或格式组合(不支持的输入和输出格式组合)

hal*_*ato 0 python opencv numpy

运行cv2.getRectSubPix(img, (5,5), (0,0))抛出错误:

OpenCV Error: Unsupported format or combination of formats (Unsupported combination of input and output formats) in getRectSubPix.
Run Code Online (Sandbox Code Playgroud)

所述dtypeimgfloat64,将其用确定img.dtype

hal*_*ato 6

查看源代码显示,只有 getRectSubPix 的输入组合是:

depth == CV_8U && ddepth == CV_8U

depth == CV_8U && ddepth == CV_32F

depth == CV_32F && ddepth == CV_32F
Run Code Online (Sandbox Code Playgroud)

这意味着输入数组需要转换为int8或float32才能传入,可以通过以下方式完成:

np.int8(img)
Run Code Online (Sandbox Code Playgroud)

或者

np.float32(img)
Run Code Online (Sandbox Code Playgroud)