opencv摄像头用法
【摘要】 python:
设置视频起始帧:
cap.set(cv2.CAP_PROP_POS_FRAMES, keys_frame) # keys_frame为关键帧的序号
import cv2import numpy as numpycap=cv2.VideoCapture(0)#设置显示分辨率和FPS ,不设置的话会非常卡cap.set(cv2.CAP_PRO...
python:
设置视频起始帧:
cap.set(cv2.CAP_PROP_POS_FRAMES, keys_frame) # keys_frame为关键帧的序号
-
import cv2
-
import numpy as numpy
-
cap=cv2.VideoCapture(0)
-
#设置显示分辨率和FPS ,不设置的话会非常卡
-
cap.set(cv2.CAP_PROP_FRAME_WIDTH,800)
-
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,600)
-
cap.set (cv2.CAP_PROP_FPS,20)
-
while cap.isOpened():
-
ret,frame=cap.read()
-
# cv2.flip(frame,frame,1)
-
# frame1=None
-
# cv2.flip(frame,frame1,1)
-
#图像水平翻转
-
frame=cv2.flip(frame,1)
-
# gray=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
-
-
cv2.imshow('camare',frame[0:100,1:200])
-
#按Q键退出
-
if cv2.waitKey(1) & 0xFF==ord('q'):
-
break
-
#释放摄像头和卸载窗口
-
cap.release()
-
cv2.destroyAllWindows()
c++打开摄像头:
-
#include<opencv2/core/core.hpp>
-
#include<opencv2/highgui/highgui.hpp>
-
#include<opencv2/imgproc/imgproc.hpp>
-
#include<iostream>
-
using namespace std;
-
using namespace cv;
-
-
int main()
-
{
-
//定义VideoCapture对象选择摄像头
-
VideoCapture capture(0);
-
//判断是否出错
-
if (!capture.isOpened())
-
{
-
cout << "some thing wrong" << endl;
-
system("pause");
-
return -1;
-
}
-
//获取视频相关信息---分辨率(宽、高)
-
int frameHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
-
int frameWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
-
cout << "this video is :" << frameWidth << "*" << frameHeight << endl;
-
//定义writer对象
-
VideoWriter outputVideo;
-
outputVideo.open("save.avi", -1, 25.0, Size(frameWidth, frameHeight), true);
-
//判断open writer对象是否出错
-
if (!outputVideo.isOpened()){
-
cout << "fail to open the videowriter" << endl;
-
system("pause");
-
return -1;
-
}
-
-
//循环读取一帧
-
Mat frameImg;
-
long nCount = 1;
-
while (1){
-
//输出当前帧数
-
cout << "Current frame" << nCount << endl;
-
capture >> frameImg;
-
//判断是否读完
-
if (!frameImg.empty()){
-
imshow("frame", frameImg);
-
}
-
else{
-
break;
-
}
-
//按Q退出
-
if (char(waitKey(40) == 'q')){
-
break;
-
}
-
outputVideo << frameImg;//将该帧写入文件
-
nCount++;
-
}
-
//释放摄像头
-
capture.release();
-
return 0;
-
}
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/54377981
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)