【问题标题】:OpenCV Error: Unsupported format or combination of formats (Unsupported combination of input and output formats) in getRectSubPixOpenCV 错误:getRectSubPix 中不支持的格式或格式组合(输入和输出格式不支持的组合)
【发布时间】:2024-04-19 17:50:01
【问题描述】:

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

OpenCV Error: Unsupported format or combination of formats (Unsupported combination of input and output formats) in getRectSubPix.

imgdtypefloat64,由img.dtype 确定。

【问题讨论】:

    标签: python numpy opencv


    【解决方案1】:

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

    depth == CV_8U && ddepth == CV_8U
    
    depth == CV_8U && ddepth == CV_32F
    
    depth == CV_32F && ddepth == CV_32F
    

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

    np.int8(img)
    

    np.float32(img)
    

    【讨论】:

      【解决方案2】:

      您也可以使用.astype():

      img.astype('int8')
      

      【讨论】: