我有一个1080p网络摄像头(Logitech v-u0032),我正在创建一个程序,该程序每隔X秒钟就从它的提要中保存静止图像,但是图像两侧都有黑条,分辨率似乎比使用内置相机应用程序的Windows仍然低,两个输出文件均具有1920x1080像素,但实际分辨率的差异非常明显
我一直在寻找改变分辨率的方法,但OpenCV似乎总是可以升级到新分辨率
import cv2
import numpy as np
import datetime
import time
import os
cv2.namedWindow("Unregistered Hypercam")
vc = cv2.VideoCapture(0)
vc.set(3, 1920) # Set the horizontal resolution
vc.set(4, 1080) # Set the vertical resolution
vc.set(5, 10) # Set the framerate
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
lastSave = time.time()
def mfold(name):
try:
os.mkdir(name)
except OSError:
x = 0
else:
print ("Successfully created the directory %s " % fold)
while rval:
cv2.imshow("Unregistered Hypercam", frame)
rval, frame = vc.read()
now = datetime.datetime.now()
if time.time() > lastSave + 10:
lastSave = time.time()
fold = '\\snapshots\\' # This will create folder C:\snapshots
mfold(fold)
cv2.imwrite(fold + str(now.year) + '.' + str(now.month) + '.' + str(now.day) + ' ' + str(now.hour) + 'h_' + str(now.minute) + 'm_' + str(now.second) + 's.png', frame)
key = cv2.waitKey(20)
if key == 27:
break
cv2.destroyWindow("Unregistered Hypercam")
图像模糊不清且带有黑条,并且与Windows摄像头应用程序拍摄的图像完全不同
答案 0 :(得分:0)
这是一个示例小部件,用于将网络摄像头/视频流保存到视频文件或屏幕快照图像中。它保持流的原始分辨率。我使用了一个IP摄像机流而不是一个网络摄像机,但是它应该与网络摄像机相同。当前,它会打开一个流并将其另存为视频,但是您可以对其进行修改以获取定期的屏幕截图。另外,要修改分辨率,您可以使用此功能在保持宽高比的同时手动调整框架的大小(当前未在小部件中使用)。
在保持宽高比的同时调整帧的分辨率
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
您可以像这样使用它来调整宽度或高度
rval, frame = vc.read()
resize_width = maintain_aspect_ratio_resize(frame, width=500)
resize_height = maintain_aspect_ratio_resize(frame, height=500)
流并保存视频小部件
from threading import Thread
import cv2
class VideoWriterObject(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def show_frame(self):
# Display frames in main program
if self.status:
cv2.imshow('frame', self.frame)
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
if __name__ == '__main__':
video_stream_widget = VideoWriterObject(0)
while True:
try:
video_stream_widget.show_frame()
video_stream_widget.save_frame()
except AttributeError:
pass