Python OpenCV视频分辨率

7
我尝试以以下方式更改视频分辨率(使用mp4!)(更改为800x600):但是它不起作用,当我使用cap.get(3)和(4)时,它每次都会返回默认的1280x720!
import cv2
cap = cv2.VideoCapture('file')
while(cap.isOpened()):
    cv2.waitKey(10)

    ret, frame = cap.read()
    cap.set(3, 800)
    cap.set(4, 600)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) 
    print cap.get(3) # return default 1280       

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

What I'm doing wrong?

I tried -

cv2.resizeWindow("ssss", 300, 300), 

并且
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)

没有影响!


尝试使用cv2.VideoCapture(0)打开您的网络摄像头,它会给您800而不是1280。 - Kallz
1
使用网络摄像头可以工作,更改为文件!- mp4 - TheRutubeify
1
如果你在 Stack OverFlow 上找到了答案,你应该遵循这些规则:https://stackoverflow.com/help/someone-answers。如果你知道问题的答案,应该在回答面板中给出答案,而不是在问题面板中。 - Raksha Saini
3个回答

10
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    cv2.waitKey(10)

    ret, frame = cap.read()
    cap.set(3, 800)
    cap.set(4, 600)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) 
    print cap.get(3) # return default 1280       

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

这段代码适用于网络摄像头,而不是文件。

对于视频文件,您可以调整窗口大小

cv2.resizeWindow(winname, width, height) 

首先定义一个具有名称的窗口并调整其大小。

示例

  cv2.namedWindow("frame", 0);
  cv2.resizeWindow("frame", 800,600);

了解详细的窗口大小调整请点击这里


如何使用cv2.resizeWindow("cap", 300, 300)在循环中?我尝试了这种方式,但没有变化。 - TheRutubeify
它只是打开一个新的“空白”窗口,标题为“Final”。 - TheRutubeify
1
@TheRutubeify cv2.imshow('frame',gray) 这一行改为 cv2.imshow('Final',gray)。 - Kallz
是的,这很有帮助,因为我在文档和互联网上都找不到关于这个简单功能的解决方案! - TheRutubeify
请@TheRutubeify接受答案,这样其他用户也可以得到正确的答案。 - Kallz
这个解决方案救了我!! - carl

3
我认为你的代码中可能有几个需要注意的地方。
  1. As described in the OpenCV documentation for VideoCapture, if you want to access your default WebCam, you'd need to initialise the class as follows:

    cap = cv2.VideoCapture('file')
    

    If you are trying to then change the resolution of the camera, I'd suggest to move the two set lines right below the initialisation of cap and only perform it once - not each time you read in the frame. You can also use constants to access the right attributes:

    cap = cv2.VideoCapture('file')
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    
    # Your while loop and the rest of the code...
    
  2. If you are trying to read the frame from a file and want to change it's resolution, you'd probably want to use the resize method as described here. This would need to be done inside the loop, right after you read in the frame. It could be something like:

    resize(ret, ret, Size(800, 600), 0, 0, INTER_CUBIC); 
    
我希望这可以帮助您。

1
我尝试了 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800) 和 cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600),但没有帮助! - TheRutubeify
1
就像我之前写的一样,cap.set(3, 800)和cap.set(4, 600)是相同的! - TheRutubeify

1

cap.set()在分辨率低于640,480时没有效果(至少对我的MacBook Pro来说) 您可以增加分辨率,但例如将其设置为300,300没有效果。 根据我的经验,在read()帧后需要调用resize()


我在 MacBook Pro High Sierra 上也是如此。无论我选择的宽度低于 640,都会打印出“宽度 640”。 - KeitelDOG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接