【问题标题】:Getting the error TypeError: Expected Ptr<cv::UMat> for argument 'src' while processing images处理图像时出现错误 TypeError: Expected Ptr<cv::UMat> for argument 'src'
【发布时间】:2021-05-29 14:07:38
【问题描述】:

我在这段代码中遇到错误

def pre_processing(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.equalizeHist(img)
    img = img / 255
    return img
    
while True:
    success, img_original = cap.read()
    img = np.asarray(img_original)
    img = cv2.resize(img, (32, 32))
    img = pre_processing(img)
    cv2.imshow("Processsed Image", img)
    img = img.reshape(1, 32, 32, 1)

我收到此错误:

Traceback(最近一次调用最后一次): 文件“C:/Users/PycharmProjects/test.py”,第 27 行,在 img = cv2.resize(img,(32,32)) TypeError: Expected Ptr<:umat> for argument 'src'

【问题讨论】:

标签: python tensorflow opencv pycharm ocr


【解决方案1】:

我不知道这个编码有什么问题

  • 您需要逐步分析您的代码

  • 代码的第一行:

    • while True
      
    • 我建议使用cap.isOpened(),其中cap 变量是您的VideoCapture 类对象。

    • 如果您的视频无法打开,那么您的代码不会崩溃。否则你的代码会报错。

  • 你的代码的第二行:

    • success, imgOriginal = cap.read()
      
    • 使用success变量检查当前帧是否返回。

    • if success:
          .
          .
      
  • 你的代码的第三行:

    • img = np.asarray(img_original)
      
    • 如果您想保留原始值,请使用copy

    • img = img_original.copy()
      
  • 代码的第四行:

    • img = pre_processing(img)  
      
    • 您正在应用直方图均衡化,然后对图像进行归一化。

    • 如果您通过img = img / 255 标准化图像,cv2.imshow 将抛出错误。因此,您需要删除 img = img / 255 行。

  • 代码的第五行:

    • cv2.imshow("Processsed Image", img)
      
    • 您正在尝试显示图像,因此您还需要使用waitKeywaitKey 方法将延迟作为整数。所以你可以这样做:

    • cv2.imshow("Processsed Image", img)
      cv2.waitKey(1)
      
    • 但如果你想在显示期间关闭,你可以这样做:

      • key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
        
      • 如果您按q,代码将终止。

  • 代码的最后一行:

    • img = img.reshape(1, 32, 32, 1)
      
    • 我假设您正在尝试为数据集设计代码,但您应该在显示后重新调整。

  • 退出后总是releaseVideoCapture


代码:


import cv2


def pre_processing(img_):
    img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
    img_ = cv2.equalizeHist(img_)
    # img_ = img_ / 255
    return img_


if __name__ == '__main__':
    # Initialize VideoCature object for Webcam
    cap = cv2.VideoCapture(0)

    # While webcam opens
    while cap.isOpened():

        # Get the current frame
        success, imgOriginal = cap.read()

        # If the current frame returns successfully
        if success:

            # Copy the original-image
            img = imgOriginal.copy()

            # Resize for 32 x 32
            img = cv2.resize(img, (32, 32))

            # Convert to gray-scale and apply histogram equalization
            img = pre_processing(img)

            # Display the frame
            cv2.imshow("Processsed Image", img)
            key = cv2.waitKey(1) & 0xFF

            # If `q` is pressed then exit
            if key == ord("q"):
                break
            
    # Reshape
    img = img.reshape(1, 32, 32, 1)

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 2021-01-29
    • 2019-12-13
    • 2021-04-15
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多