opencv 打开摄像头 mat

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(opencv_example_project)

find_package(OpenCV REQUIRED)

message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

add_executable(opencv_example example.cpp)

target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})

example.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main(int argc,char *argv)
{
	Mat image = imread("1.png");
	namedWindow("img");
	imshow("img",image);
	waitKey(0);
	return 0;
}

打开了摄像头接口说明和源码分析
1.VideoCapture
2.bool open(int index)
3.VideoCapture cap(index)
4.open(int cameraNum,int apiPreference)

创建和清理mat空间
1.Mat mat(3000,4000,CV_8UC3);
2.mat.create(rows,cols,CV_8UC1);
3.release和析构
-引用计数为1释放

isContinuous
1.判断存储空间是否连续
2.通过step记录

直接地址访问连续空间


int size = mat.rows*mat.cols * mat.elemSize();
for(int i = 0;i < size; i+=3)
{
	mat.data[i] = 0; //B
	mat.data[i+1] = 0; //G
	mat.data[i+2] = 0; //R
}
//优化编码后效率高 13ms(4000*3000)

example.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main(int argc,char *argv)
{
	Mat mat(3000,4000,CV_8UC3);
//	mat.create(3000,4000,CV_8UC3);
	int es = mat.elemSize();
	int size = mat.rows*mat.cols*mat.elemSize();

	for(int i = 0;i < size;i += es)
	{
		mat.data[i] = 255; //B
		mat.data[i + 1] = 0; //G
		mat.data[i + 2] = 0; //R
	}
	namedWindow("mat");
	imshow("mat",mat);
	waitKey(0);
	return 0;
}

读取一帧视频
1.read(OutputArray image)
2.bool grab();
-读取并解码
3.virtual bool retrieve(OutputArray image, int flag = 0);
-图像色彩转换->调用
img_convert_ctx = sws_getCachedContext(
img_convert_ctx,
buffer_width,buffer_height,
video_st->codec->pix_fmt,
buffer_width,buffer_height,
AV_PIX_FMT_BGR24,
SWS_BICUBIC,
NULL,NULL,NULL);
4.vc>>mat

example.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;

int main(int argc,char *argv)
{
	VideoCapture cam;
	string url = "rtsp://test:test@192.168.1.4";
	namedWindow("video");
	//if(cam.open(url))
	if(cam.open(0))
	{
		cout<<"open cam success"<<endl;
	}
	else
	{
		cout<<"open cam failed !"<<endl;
		waitKey(0);
		return -1;
	}
	Mat frame;
	for(;;)
	{
		cam.read(frame);
		imshow("video",frame);
		waitKey(1);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值