目录
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;
}
c++打开摄像头简化版
# include<opencv2/opencv.hpp>
# include<cstdio>
using namespace cv;
using namespace std;
int main(int argc, char *argv[]){
// 打开摄像头
VideoCapture cam(0);
if (!cam.isOpened()){
cout << "cam open failed!" << endl;
getchar();
return -1;
}
cout << "cam open success!" << endl;
namedWindow("cam");
Mat img;
for(;;){
cam.read(img); // 读帧
if (img.empty()) break;
imshow("cam", img); // 显示每一帧
if (waitKey(5) == 'q') break; // 键入q停止
}
return 0;
}
rgb2bgr 画圆,矩形框
cv::Mat im_bgr;
cv::cvtColor(rgb, im_bgr, cv::COLOR_RGB2BGR);
for (int ii = 0; ii < 5; ii++) {
//printf(objects[i].pts[ii]);
cv::circle(objects[i].trans_image, objects[i].pts[ii], 2, cv::Scalar(0, 255, 255), -1);
cv::circle(im_bgr, objects[i].pts[ii], 2, cv::Scalar(0, 255, 255), -1);
}
cv::rectangle(im_bgr, objects[i].rect, cv::Scalar(0x27, 0xC1, 0x36), 2);
cv::imshow("im_bgr", im_bgr);
//cv::imshow("image", objects[i].trans_image);
//cv::imwrite("image_det.png", cv_mat);
//cv::waitKey(0);