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;
}