Python 将代码从 openCV 更新到 openCV2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31076403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating code from openCV to openCV2
提问by Will Jones
I'm trying to update some code from openCV to openCV2 in python. The original code is as follows:
我正在尝试将一些代码从 openCV 更新到 python 中的 openCV2。原代码如下:
self.capture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty( self.capture, cv.CV_CAP_PROP_FRAME_WIDTH, 160 );
cv.SetCaptureProperty( self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 120 );
The code that I wrote for openCV2 is this:
我为 openCV2 编写的代码是这样的:
self.capture = cv2.VideoCapture(0)
cv2.VideoCapture.set( CV_CAP_PROP_FRAME_WIDTH, 160 );
cv2.VideoCapture.set( CV_CAP_PROP_FRAME_HEIGHT, 120 );
However this does not work I'm getting an error that says:
但是,这不起作用,我收到一条错误消息:
cv2.VideoCapture.set( CV_CAP_PROP_FRAME_WIDTH, 160 ); AttributeError: 'builtin_function_or_method' object has no attribute 'set'
cv2.VideoCapture.set( CV_CAP_PROP_FRAME_WIDTH, 160 ); AttributeError: 'builtin_function_or_method' 对象没有属性 'set'
采纳答案by Rahul K P
Try this one. It will work.
试试这个。它会起作用。
self.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 160)
回答by deets
You need to work on the instancereturned by your initial call, not the class
您需要处理初始调用返回的实例,而不是类
self.capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 160)
回答by shruti iyyer
It seems that CV_CAP_PROP_POS_MSECand other such similar properties are deprecated in your installed version of Opencv, to solve the problem for example this property ,Change it to cv2.CAP_PROP_POS_MSEC
and similarly others too. Works good for me on Opencv 3.1
似乎CV_CAP_PROP_POS_MSEC和其他类似的属性在您安装的Opencv版本中已被弃用,以解决例如这个属性的问题,将其更改为cv2.CAP_PROP_POS_MSEC
类似的其他属性。在 Opencv 3.1 上对我有用
回答by ddintzner
i came across this same issue, was able to get it work with:
我遇到了同样的问题,能够让它与:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160);
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120);
回答by Ja9
This answer was constructed using a combination of proposed answers/ tutorials
这个答案是使用建议的答案/教程的组合构建的
Using cv2 - change the Frame resolution from Video captured via webcam
使用 cv2 - 从通过网络摄像头捕获的视频更改帧分辨率
import cv2
import time
cam = cv2.VideoCapture(0)
print "Frame default resolution: (" + str(cam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "; " + str(cam.get(cv2.CAP_PROP_FRAME_HEIGHT)) + ")"
#set Frame resolution to 1280; 720
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
print "Frame resolution set to: (" + str(cam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "; " + str(cam.get(cv2.CAP_PROP_FRAME_HEIGHT)) + ")"
r, frame = cam.read()
print('Resolution: ' + str(frame.shape[0]) + ' x ' + str(frame.shape[1]))
回答by Shitanshu
I removed .cv.CV_
, and it worked.
我删除了.cv.CV_
,它起作用了。
Change:
改变:
cv2.cv.CV_CAP_PROP_FRAME_HEIGHT
to:
到:
cv2.CAP_PROP_FRAME_HEIGHT