您好,我想测试一下相机的分辨率。通常我手动设置,分辨率 (w,h) 320,480 ...
现在我希望在循环中设置列表分辨率 = [(...)] 或在 函数 中设置它会自动给我每个输出,所以我不必写每手的每个决议。我尝试了很多,但还是不明白。
希望有人能帮助我
谢谢!
代码:
import sys
sys.path.append("C:\\opencv\\build\\python\\2.7")
import cv2
import cv2.cv as cv
import time
capture = cv2.VideoCapture(0)
num_frame = 0
resolution = [(320,480),(640,480),(704,680),(960,680),(1280,720),(1440,720),(1920,1080)]
w = 320
h = 480
size = capture.get(cv.CV_CAP_PROP_FRAME_WIDTH), capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
size_new = capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, w),capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT,h)
print size
start = time.time()
while(True):
ret, frame = capture.read()
if num_frame < 60:
num_frame = num_frame + 1
else:
break
total_time = (time.time() - start)
fps = (num_frame / total_time)
print str(num_frame) + ' Frames ' + str(total_time) + ' Sekunden = ' + str(fps) + ' fps'
capture.release()
cv2.destroyAllWindows()
最佳答案
您可以通过将脚本封装在一个函数中并在迭代列表时在列表中的分辨率上调用它来简单地做到这一点。
实际上代码非常快,所以 total_time = (time.time() - start)
在某些情况下评估为零,因此给出错误:ZeroDivisionError: float division by
,因为 total_time 的计算结果为 0,所以我添加了 fps = (num_frame/total_time)
行上的零time.sleep(0.001)
来摆脱这个错误。
import cv2
import cv2.cv as cv
import time
resolution = [(320,480),(640,480),(704,680),(960,680),(1280,720),(1440,720),(1920,1080)]
def change_resolution(w, h):
capture = cv2.VideoCapture(0)
num_frame = 0
size = capture.get(cv.CV_CAP_PROP_FRAME_WIDTH), capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
size_new = capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, w),capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT,h)
print size
start = time.time()
while(True):
ret, frame = capture.read()
if num_frame < 60:
num_frame = num_frame + 1
time.sleep(0.001)
else:
break
total_time = (time.time() - start)
fps = (num_frame / total_time)
print str(num_frame) + ' Frames ' + str(total_time) + ' Sekunden = ' + str(fps) + ' fps' + ' for width: ' + str(w) + ' height: ' + str(h)
capture.release()
cv2.destroyAllWindows()
for reso in resolution:
change_resolution(reso[0], reso[1])
输出:
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 320 height: 480
(0.0, 0.0)
60 Frames 0.0639998912811 Sekunden = 937.501592564 fps for width: 640 height: 480
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 704 height: 680
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 960 height: 680
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1280 height: 720
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1440 height: 720
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1920 height: 1080
关于python - 分辨率列表(OpenCv、Python、相机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30577042/