TypeError: Expected Ptr cv::UMat for argument '%s'

错误:

TypeError: Expected Ptr for argument '%s'

详细错误如下:

Traceback (most recent call last):
  File "D:/gitProject/ocr_screenRecognition/antiMissing/OperationTicket.py", line 119, in 
    longest_h_l = ot.line_detect(ot_image)
  File "D:/gitProject/ocr_screenRecognition/antiMissing/OperationTicket.py", line 22, in line_detect
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
TypeError: Expected Ptr for argument '%s'

其实就是传入的参数不对。原本传入到cv2中的image需要是numpy.array类型,但是从上面的信息可以看出,传进去的是字符串类型,所以报错。我之所以出现这样的错是我copy我原来写的函数到类中,作为成员函数,如下:

原本是如下所示:

def line_detect(image, model=None):
    """
    检测图片中的线
    :param image:
    :param model:
    :return:
    """
    if model != "v":
        model = "horizontal"
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # ...

copy到类中:

class OperationTicket(object):
    def line_detect(image, model=None):
        """
        检测图片中的线
        :param image:
        :param model:
        :return:
        """
        if model != "v":
            model = "horizontal"
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # ...

        python有自己的语言特性,上面类中的image其实被当成self了。改成如下就不会出错了。

class OperationTicket(object):
    def line_detect(self, image, model=None):
        """
        检测图片中的线
        :param image:
        :param model:
        :return:
        """
        if model != "v":
            model = "horizontal"
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # ...

 

你可能感兴趣的:(Python)