如何解决网络摄像头图像在python中变形
import cv2
import sys
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
img_counter = 0
while True:
# Capture frame-by-frame
ret,frame = video_capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
k = cv2.waitKey(1)
faces = faceCascade.detectMultiScale(
gray,scaleFactor=1.5,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
# Display the resulting frame
cv2.imshow('FaceDetection',frame)
if k%256 == 27: #ESC Pressed
break
elif k%256 == 32:
# SPACE pressed
img_name = "facedetect_webcam_{}.png".format(img_counter)
cv2.imwrite(img_name,frame)
print("{} written!".format(img_name))
img_counter += 1
# When everything is done,release the capture
video_capture.release()
cv2.destroyAllWindows()
after running the code in python the image of my webcam turn into this
解决方法
您的代码对我来说没有任何问题。
输出图像看起来像输入图像的数据类型有问题。
这里讨论了类似的事情:
https://answers.opencv.org/question/174027/c-ycbcr422-to-rgb-convert-from-raw-data-file/
您还可以发布输入框灰度图像吗?
您正在使用什么相机?
尝试在video_capture.read()之后添加此代码并发布输出。
print(frame.dtype)
print(frame.shape)
我的是:dtype-uint8,形状-(480,640,3)。
您可能会遇到通道数量(例如RGBA)和矩形功能的问题。
这些只是我对可能出问题的想法。但是对我来说,您的代码可以正常工作。
br约瑟夫
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。