[英]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.