简体   繁体   English

收到错误 TypeError: Expected Ptr<cv::umat> 处理图像时的参数“src”</cv::umat>

[英]Getting the error TypeError: Expected Ptr<cv::UMat> for argument 'src' while processing images

I am getting an error in this code我在这段代码中遇到错误

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)

I get this error:我收到此错误:

Traceback (most recent call last): File "C:/Users/PycharmProjects/test.py", line 27, in img = cv2.resize(img,(32,32)) TypeError: Expected Ptr<cv::UMat> for argument 'src'回溯(最后一次调用):文件“C:/Users/PycharmProjects/test.py”,第 27 行,在 img = cv2.resize(img,(32,32)) 中 TypeError: Expected Ptr<cv::UMat>对于参数“src”

i dont know whats wrong with this coding我不知道这个编码有什么问题

  • You need to analyze your code step-by-step您需要逐步分析您的代码

  • The first line of your code:您的代码的第一行:

    •  while True
    • I would suggest use cap.isOpened() where cap variable is your VideoCapture class object.我建议使用cap.isOpened()其中cap变量是您的VideoCapture class object。

    • If your video won't open, then your code won't crash.如果您的视频无法打开,那么您的代码不会崩溃。 Otherwise your code will throw error.否则你的代码会抛出错误。

  • The second line of your code:您的代码的第二行:

    •  success, imgOriginal = cap.read()
    • Use success variable to check whether the current frame is returned.使用success变量检查当前帧是否返回。

    •  if success: . .
  • The third line of your code:您的代码的第三行:

    •  img = np.asarray(img_original)
    • Use copy when you want to keep the original value.当您想保留原始值时,请使用copy

    •  img = img_original.copy()
  • The fourth line of your code:代码的第四行:

    •  img = pre_processing(img)
    • You are applying histogram equalization and then normalizing the image.您正在应用直方图均衡化,然后对图像进行归一化。

    • If you normalize the image by img = img / 255 , cv2.imshow will throw an error.如果通过img = img / 255对图像进行归一化, cv2.imshow将引发错误。 Therefore you need to remove the img = img / 255 line.因此,您需要删除img = img / 255行。

  • The fifth line of your code:代码的第五行:

    •  cv2.imshow("Processsed Image", img)
    • You are trying to display the image, therefore you also need to use waitKey .您正在尝试显示图像,因此您还需要使用waitKey The waitKey method takes delay as an integer. waitKey方法将延迟视为 integer。 So you could do:所以你可以这样做:

    •  cv2.imshow("Processsed Image", img) cv2.waitKey(1)
    • But if you want to close during the display you could do:但是,如果您想在显示期间关闭,您可以这样做:

      •  key = cv2.waitKey(1) & 0xFF if key == ord("q"): break
      • If you press q the code will terminate.如果按q ,代码将终止。

  • The last line of your code:代码的最后一行:

    •  img = img.reshape(1, 32, 32, 1)
    • I assume you are trying to design your code for dataset, but you should reshape after the display.我假设您正在尝试为数据集设计代码,但您应该在显示后重新调整。

  • Always release the VideoCapture after exit.退出后始终release VideoCapture


Code:代码:


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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:预期的 Ptr<cv::umat> 对于参数'src'?</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'src'? 错误:我无法将我的图像从 bgr 转换为 rgb:TypeError: Expected Ptr<cv::umat> 对于参数“src”</cv::umat> - error: im not able to convert my images from bgr to rgb: TypeError: Expected Ptr<cv::UMat> for argument 'src' Python - tkinter; 类型错误:预期的 Ptr<cv::umat> 对于参数“src”</cv::umat> - Python - tkinter; TypeError: Expected Ptr<cv::UMat> for argument 'src' 创建人脸识别代码时出错。 错误是“ TypeError: Expected Ptr<cv::UMat> 对于参数 &#39;src&#39; ” 什么是解决方案 - I got Error while creating face recognition code. The Error is “ TypeError: Expected Ptr<cv::UMat> for argument 'src' ” what is solution 预期 Ptr<cv::UMat> 对于参数&#39;src&#39; - Expected Ptr<cv::UMat> for argument 'src' 类型错误:预期的 Ptr<cv::umat> 对于参数 'm'</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'm' 错误“TypeError:预期的 Ptr<cv::umat> 尝试显示数组中的图像时用于参数“垫子”</cv::umat> - Error “TypeError: Expected Ptr<cv::UMat> for argument 'mat'” when trying to display images from an Array 类型错误:预期的 Ptr<cv::umat> 对于参数“%s”</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument '%s' 类型错误:预期的 Ptr<cv::umat> 对于参数“img”</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'img' 在将bgr转换为rgb时,获取TypeError:参数&#39;src&#39;的预期cv :: UMat - Getting TypeError: Expected cv::UMat for argument 'src' while casting bgr to rgb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM