mat
Mat替代lplImage
创建和清理mat空间
- Mat mat(3000, 4000, CV_8UC3);//3000行,4000列数组,数组里存放3个unsigned char类型的数据
- mat.create(rows, cols, CV_8UC1);//行数,列数,如果mat已经有空间,create时会自动清理已有空间
- release或者析构:引用计数为1时释放
处理类型一定要用unsigned char而不是char
3*3RGB图像存放方式(连续)
i[image](http://)
###### isContinuous
* 判断存储空间是否连续
* 通过step记录
##### 直接地址访问连续空间
int size = mat.rows*mat.cols*mat.elemSize();
for(int i = 0; i< size; i+3)//3是因为RGB
{
mat.data[i] = 0; //B
mat.data[i+1] = 0; //G
mat.data[i+2] = 0; //R
}
//优化编码后效率高13ms (4000\*3000)
##### 直接地址访问不连续空间
for(int i = 0; i < mat.row; i++)
{
for(int j = 0; j < mat.cols; j++)
{
(&mat.data[i*mat.step])[j*3] = 255;//B
(&mat.data[i*mat.step])[j*3 + 1] = 255;//G
(&mat.data[i*mat.step])[j*3 + 2] = 1;//R
}
}
##### 通过ptr接口遍历Mat(模板函数)
* 性能基本等同与地址访问
* mat.ptr(row);//返回的指针
* mat.ptr(row, col);
##### 通过at接口遍历Mat(模板函数)
* 接口最简单的遍历方法
mat.at
- 画面叠化(cross-dissolve)效果
addWeighted(src1, a, src2, 1-a, 0.0, dst);
//两幅图像大小需一致
图像旋转和镜像
-
cv::rotate(src, dst, type);
- ROTATE_180
- ROTATE_90_CLOCKWISE
- ROTATE_90_COUNTERCLOCKWISE
-
cv::flip(src,dst, type);//镜像type 0(x), 1(y), -1
###通过ROI图像合并
打开摄像头接口说明和源码分析
- VideoCapture
- bool open(int index)
- VideoCapture cap(index)
- open(int cameraNum, int apiPrefrence)
打开视频流文件
-
bool open(const String &filename)
-
VideoCapture cap(const String &file)
-
bool open(const String &filename, int apiPrefrence)
-
关闭和空间释放
-
~VideoCapture
-
release
读取一帧视频
read(OutputArray image);
- bool grab() 读取并解码
- virtual bool retrieve(OUtputArray , intflag= 0):图像色彩转换
- vc>>mat
获取视频,相机属性
- CAP_PROP_FPS帧率
- CAP_PROP_FRAME_COUNT 总帧数
- CAP_PROP_POS_FRAMES 播放帧的位置
- CAP_PROP_FRAME_WIDTH HEIGHT
VideoWriter
- open(const String &filename, int fourcc, //VideoWrite::fourcc('H', '2', '6', '4')
double fps, Size frameSize,bool isColor=true)
release
- void write(const Mat&)
- cvVideoWriter_FFMPEG::writeFrame