opencv核心类型

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图像存放方式(连续)

[iimage

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<Vec3b>(row, col)[0] = 255;
mat.at<Vec3b>(row, col)[1] = 0;
mat.at<Vec3b>(row, col)[2] = 0;

at可以使用try{} catch(…/cv::Exception &ex){}捕获异常

通过迭代器遍历Mat
  • 可以不用管mat的行列
    auto it = mr.begin();
    auto it_end = mr.end();

ROI感兴趣区域

cv::Rect rect(100, 100, 300, 300);

像素格式和灰度图

RGB, YUV, GRAY

cvtColor(src, img, COLOR_BGR2GRAY);//源图像,目标凸显,转换方式,利用多线程等方式提高效率

自己实现转换:
Gray = (R30 + G59 + B*11 + 50)/100

二值化和阈值

  • THRESH_BINARY 二进制阈值化
  • THRESH_BINARY_INV 反二进制阈值化

改变图片的对比度和亮度

g(i,j) = a*f(i,j) + b
a 1.0~3.0(对比) b 0~100(亮度)
saturate_cast防止移除函数

图像尺寸调整

  • INTER_NEAREST 近邻算法(最快)
int sx, sy = 0;//原图对应的坐标
float fy = float(src.rows)/out.rows;
float fx = float(src.cols)/out.cols;
for(int y = 0; y< out.rows;y++){
  sy = fy*y + 0.5;//+0.5四舍五入
  for(int x = 0; x < out.cols;x++){
    sx = fx*x + 0.5;
    out.at<Vec3b>(y,x) = src.at<Vec3b>(sy, sx);
  }

}
  • CV_INTER_LINEAR 双线程差值(缺省使用)

滤波:
输入图像中像素的小领域来产生输出图像的方法,在信号处理中,这种方法称为滤波(filtering).其中,最常用的是线性滤波:输出像素是输入领域像素的加权和.

双线性内插值:
是由源图像位置在它附近的2*2区域4个邻近像素的值通过加权平均计算得出的.
低通滤波性质,使高频分量受损,图像轮廓可能会有一点模糊.

图像金字塔

高斯金字塔(Gaussian pyramid):用来向下采样

  • 获取G(i+1)将G(i)与高斯内核卷积
  • 将所有偶数行和列去除

拉普拉斯金字塔(Laplacian pyramid):用来从金字塔底层图像重建上层未采样图像

  • 用来从金字塔底层图像重建上层未采样图像
  • 首先,将图像扩大两杯,新增以0填充
  • 高斯内核(乘以4)与放大后的图像卷积

两幅图像混合(blending)

  • dst = src1*a + src2*(1-a) + gamma//gamma增益
  • a=[0~1]
  • 画面叠化(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
  • 15
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻口味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值