当前位置:网站首页>opencv视频处理入门
opencv视频处理入门
2022-06-09 00:34:00 【51CTO】
文章目录
从相机捕捉视频
需要创建一个VideoCapture对象。它的参数可以是设备索引或者一个视频文件名。
import
cv2
as
cv
cap
=
cv.
VideoCapture(
0)
#0代表一个摄像头
print(
cap.
isOpened())
fourcc
=
cv.
VideoWriter_fourcc(
*
'XVID')
out
=
cv.
VideoWriter(
'output.mp4',
fourcc,
20.0, (
640,
480))
while(
cap.
isOpened()):
ret,
frame
=
cap.
read()
if
ret
==
True:
out.
write(
frame)
cv.
imshow(
'frame',
frame)
if
cv.
waitKey(
0)
&
0xFF
==
ord(
'q'):
break
else:
break
cap.
release()
out.
release()
cv.
destroyAllWindows()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
运行后就会自动保存每一帧合成视频。
cv.VideoCapture(0) 参数0代表一个摄像头,1则两个摄像头,笔记本只有一个摄像头。这里也可以传参视频,不仅仅是摄像头。
cap.read() 返回一个 bool 值(True/False)。如果加载成功,它会返回True。ret: 布尔值;
frame: 图像的三维矩阵;
这样 ret 存储布尔值, frame 存储图像。
cap.isOpened() 检查是否初始化。
从相机捕捉图片
按q退出,s保存图片为test.jpg
import
cv2
cap
=
cv2.
VideoCapture(
0)
#摄像头开启,也可以传入mp4
print(
cap.
isOpened())
""
"
返回两个值
先返回一个布尔值,
如果视频读取正确,
则为
True,
如果错误,
则为
False;
也可用来判断是否到视频末尾;
再返回一个值,
为每一帧的图像,
该值是一个三维矩阵;
通用接收方法为:
ret,
frame
=
cap.
read();
ret:
布尔值;
frame:
图像的三维矩阵;
这样
ret
存储布尔值,
frame
存储图像;
若使用一个变量来接收两个值,
如:
frame
=
cap.
read()
则
frame
为一个元组,
原来使用
frame
处需更改为
frame[
1]
""
"
while
cap.
isOpened():
ret_flag,
img_camera
=
cap.
read()
cv2.
imshow(
"camera",
img_camera)
k
=
cv2.
waitKey(
1)
if
k
==
ord(
's'):
cv2.
imwrite(
"test.jpg",
img_camera)
if
k
==
ord(
'q'):
break
cap.
release()
cv2.
destroyAllWindows()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
边栏推荐
- 【直播回顾】战码先锋第四期:轻松入门,成为媒体子系统贡献者
- 如何选择正确的消息队列驱动
- Cloud native technology - Construction of highly available etcd database cluster
- Redis持久化
- Reading notes - Reflections on the greatest psychological experiment of the 20th century 1
- How to make 100+ orders a day without any supply of free fish? Share the complete strategy!!
- Final review of introduction to information security at Harbin Institute of Technology
- AGV电池介绍
- [tcp/ip] triple handshake process and causes
- Structured analysis
猜你喜欢
![[tcp/ip] triple handshake process and causes](/img/5c/5dfada817031b4b3ded8b10722b7f7.png)
[tcp/ip] triple handshake process and causes

扁平化登录form页面

A life and death journey of 2000+ orders on the same day when there is no supply of idle fish!

0.96OLED 4针IIC STM32-HAL库版本(附源码)

. Podspec dependency problem handling

Parameter ‘XXX‘ not found. Available parameters are [arg1, arg0, para

GPU 渲染管线和硬件架构浅谈

直播预告|FeatureStore Meetup V3 重磅来袭!

Centos7 completely uninstalls mysql5.7 and reinstalls 8.0

Robotics (II): dynamic parameter identification
随机推荐
Good programming style (habit)
What are cache avalanche, cache breakdown, and cache penetration?
[the problem has been solved]-golang Sarama reported an error. Tried to send a message to a replica that is not the leader
JS add to collection
哪一天不写博客了,粉丝会不会好奇我去哪里了
TCP four wave and its causes
Fastlane build version number increases automatically
js加入收藏
Fastdds awesome!!!
JZ77:按之字形顺序打印二叉树
0.96OLED 4针IIC STM32-HAL库版本(附源码)
[tcp/ip] triple handshake process and causes
Notes on international spot gold conversion
enumeration
ffmepg.spec 文件 —— 筑梦之路
Enumerate matching opponents of the problem
RealBasicVSR源码解析
Treatment of data file damage caused by magnetic array failure in a project site
Detailed explanation of MySQL MySQL index
鼠标移过,左右按钮显示