简体   繁体   English

get(cv2.CAP_PROP_FOURCC)使用openCV和python返回错误

[英]get(cv2.CAP_PROP_FOURCC) return errors with openCV and python

I have to read a local .avi files and make it shaw on a window.Here is my code: 我必须阅读一个本地.avi文件并使其在窗口中逸出。这是我的代码:

import os,cv2
user=os.path.expanduser('~')
capture=cv2.VideoCapture(str(user)+"/Downloads/vehicle/Sunny/april21.avi")
if(capture.isOpened()):
    print "Open"
else:
    print "Fail to open!"
fps=capture.get(cv2.CAP_PROP_FPS)
fourcc=capture.get(cv2.CAP_PROP_FOURCC)
print fourcc#why fourcc is return 0.0?
print "fps:%d"%fps
size=(int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))/2,
    (int)(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))/2
    )
isStop=1
while isStop:
    grabbed,frame=capture.read()#frame is None
    cv2.namedWindow("window")
    img=cv2.resize(frame,size,interpolation=cv2.INTER_CUBIC)
    cv2.imshow("window",img)
    c=0xFF&cv2.waitKey(1)
    if c ==27:
        isStop=False
        cv2.destroyAllWindows()

But now the result is : 但是现在的结果是:

Open
0.0
fps:29
OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /Users/tom/opencv-3.1.0/modules/imgproc/src/imgwarp.cpp, line 3229
Traceback (most recent call last):
  File "/Users/tom/Documents/readVideo.py", line 20, in <module>
    img=cv2.resize(frame,size,interpolation=cv2.INTER_CUBIC)
cv2.error: /Users/tom/opencv-3.1.0/modules/imgproc/src/imgwarp.cpp:3229: error: (-215) ssize.area() > 0 in function resize

The only thing I am sure is these codes are right,cause I have created a .avi files through cv2.VideoWriter and it can be read and shown on a window. 我唯一确定的是这些代码正确,因为我已经通过cv2.VideoWriter创建了一个.avi文件,并且可以在窗口中读取和显示它。 Thanks for answer. 感谢您的回答。

Much likely the data type being returned by OpenCV is a double (floating point) whose arbitrary content (the FOURCC code) is invalid (not IEEE compliant). OpenCV返回的数据类型很可能是双精度(浮点数),其任意内容(FOURCC代码)无效(不符合IEEE)。

I don't know Python but the following C/C++ snippet do the work: 我不了解Python,但以下C / C ++代码段可以完成工作:

union {
    char    c[5];
    int     i;
} myfourcc;
myfourcc.i = capture.get(CV_CAP_PROP_FOURCC);
myfourcc.c[4] = '\0';
std::cout << "4cc:" << myfourcc.c << std::endl;

I guess Python has a more idiomatic way that just the ctypes. 我猜想Python除了ctypes之外还有一种更惯用的方式。 Just my pennies. 只是我的便士。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM