gpt4 book ai didi

python ,OpenCV : Capture Images from WebCam

转载 作者:太空狗 更新时间:2023-10-29 22:25:52 24 4
gpt4 key购买 nike

我有一个 Logitech C920 挂接到我的 PC 上,我正在尝试用它来点击使用 OpenCV 的图片。

我知道我可以使用以下方式捕获图像:

cam = cv2.VideoCapture(1)
s, im = cam.read() # captures image
cv2.imshow("Test Picture", im) # displays captured image
cv2.imwrite("test.bmp",im) # writes image test.bmp to disk

但它让我获得了我的相机能够拍摄的 15MP 静态照片。如果我单击 图片,我从上面得到的结果远不如我所期望的。

那么,有没有办法拍照(就像在官方的网络摄像头软件中一样)?

最佳答案

用这个 previous question 测试后作为指南,我编写了以下程序来检查我的相机设置(罗技 S7500 USB 网络摄像头)。我在 Windows 7 上使用 Python 2.7 和 OpenCV 2.4.6。

希望您可以使用此代码作为指南来修改相机上的设置以获得所需的分辨率(或接近它)(使用 OpenCV 中的 set() 函数。)

如果每个相机设置的值都是 0.0,那么您可能无法调整相机设置。我在笔记本电脑的内置网络摄像头上运行此程序时遇到过这种情况。

from __future__ import print_function
import sys
import cv2


def main(argv):
#capture from camera at location 0
cap = cv2.VideoCapture(0)
# Change the camera setting using the set() function
# cap.set(cv2.cv.CV_CAP_PROP_EXPOSURE, -6.0)
# cap.set(cv2.cv.CV_CAP_PROP_GAIN, 4.0)
# cap.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS, 144.0)
# cap.set(cv2.cv.CV_CAP_PROP_CONTRAST, 27.0)
# cap.set(cv2.cv.CV_CAP_PROP_HUE, 13.0) # 13.0
# cap.set(cv2.cv.CV_CAP_PROP_SATURATION, 28.0)
# Read the current setting from the camera
test = cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC)
ratio = cap.get(cv2.cv.CV_CAP_PROP_POS_AVI_RATIO)
frame_rate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
brightness = cap.get(cv2.cv.CV_CAP_PROP_BRIGHTNESS)
contrast = cap.get(cv2.cv.CV_CAP_PROP_CONTRAST)
saturation = cap.get(cv2.cv.CV_CAP_PROP_SATURATION)
hue = cap.get(cv2.cv.CV_CAP_PROP_HUE)
gain = cap.get(cv2.cv.CV_CAP_PROP_GAIN)
exposure = cap.get(cv2.cv.CV_CAP_PROP_EXPOSURE)
print("Test: ", test)
print("Ratio: ", ratio)
print("Frame Rate: ", frame_rate)
print("Height: ", height)
print("Width: ", width)
print("Brightness: ", brightness)
print("Contrast: ", contrast)
print("Saturation: ", saturation)
print("Hue: ", hue)
print("Gain: ", gain)
print("Exposure: ", exposure)
while True:
ret, img = cap.read()
cv2.imshow("input", img)

key = cv2.waitKey(10)
if key == 27:
break

cv2.destroyAllWindows()
cv2.VideoCapture(0).release()


# 0 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
# 1 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
# 2 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
# 3 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
# 4 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
# 5 CV_CAP_PROP_FPS Frame rate.
# 6 CV_CAP_PROP_FOURCC 4-character code of codec.
# 7 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
# 8 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
# 9 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
# 10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
# 11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
# 12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
# 13 CV_CAP_PROP_HUE Hue of the image (only for cameras).
# 14 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
# 15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
# 16 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
# 17 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
# 18 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

if __name__ == '__main__':
main(sys.argv)

Here是关于这个主题和 OpenCV Documentation 的 OpenCV 问题的链接。

关于 python ,OpenCV : Capture Images from WebCam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com