如何在Opencv中,设置视频类的属性宽和高,请举例
时间: 2023-02-08 14:02:10 浏览: 53
在 OpenCV 中,可以使用 VideoWriter 类来设置视频的宽和高。
示例代码:
```
import cv2
# 设置视频宽为 640,高为 480
width = 640
height = 480
# 创建 VideoWriter 对象
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width, height))
# 使用该对象写入视频帧
for i in range(100):
frame = cv2.imread('frame%d.jpg' % i)
out.write(frame)
# 释放该对象
out.release()
```
其中,`fourcc` 是视频编码格式的代码,`20.0` 是视频的帧率,`(width, height)` 是视频的宽和高。
在这个例子中,我们使用了 'MJPG' 编码,帧率为20帧每秒,宽为640像素,高为480像素。
相关问题
如何在Opencv中,获取视频类的属性宽和高,请举例
在 OpenCV 中,可以使用 cv2.VideoCapture 类读取视频文件,然后使用其 get() 方法获取视频的宽和高。
例如:
```python
import cv2
# 读取视频文件
video = cv2.VideoCapture("video.mp4")
# 获取视频宽度
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
# 获取视频高度
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("Video resolution: {}x{}".format(width, height))
```
在上面的例子中,使用 cv2.CAP_PROP_FRAME_WIDTH 和 cv2.CAP_PROP_FRAME_HEIGHT 获取视频的宽度和高度,并将它们转换为 int 类型。
另外一种方法就是使用 get(propId) 函数, propId 为CAP_PROP_FRAME_WIDTH,CAP_PROP_FRAME_HEIGHT来获取视频的宽度和高度。
```python
import cv2
# 读取视频文件
video = cv2.VideoCapture("video.mp4")
# 获取视频宽度
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
# 获取视频高度
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("Video resolution: {}x{}".format(width, height))
```
C++opencv打开并读取视频文件的宽和高
要打开并读取视频文件的宽和高,可以使用OpenCV库中的VideoCapture类。下面是一个简单的示例代码:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
VideoCapture cap("example.mp4");
if(!cap.isOpened()) {
printf("Error opening video file.\n");
return -1;
}
int width = cap.get(CAP_PROP_FRAME_WIDTH);
int height = cap.get(CAP_PROP_FRAME_HEIGHT);
printf("Video width: %d\n", width);
printf("Video height: %d\n", height);
return 0;
}
```
在这个示例中,我们首先创建了一个VideoCapture对象来打开视频文件并检查其是否成功打开。然后,我们使用CAP_PROP_FRAME_WIDTH和CAP_PROP_FRAME_HEIGHT属性来获取视频的宽度和高度。最后,我们将这些值输出到控制台。