第一课
掩模处理:
1.原理方式
int cols = (src.cols-1) * src.channels();
int offsetx = src.channels();
int rows = src.rows;
dst = Mat::zeros(src.size(), src.type());
for (int row = 1; row < (rows - 1); row++) {
const uchar* previous = src.ptr<uchar>(row - 1);
const uchar* current = src.ptr<uchar>(row);
const uchar* next = src.ptr<uchar>(row + 1);
uchar* output = dst.ptr<uchar>(row);
for (int col = offsetx; col < cols; col++) {
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col- offsetx] + current[col+ offsetx] + previous[col] + next[col]));
2.API方式:
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, src.depth(), kernel);//原图像,输出图像,位图深度
位图:位图图像(bitmap),亦称为点阵图像或栅格图像,是由称作像素(图片元素)的单个点组成的。
saturate_cast<uchar>函数可以使值保持在0~255之间,不发生截断错误
getTickcount():获取执行时钟数
getTickFrequency();获取时钟频率
第二课 Mat对象
Mat对象IplImage对象
1.Mat对象:2.0之后引进的图像数据结构,自动分配内存,不存在内存泄露
面向对象的数据结构,分为头部与数据部分
2.IplImage对象:容易内存泄漏
Mat对象使用
输出图像的内存是自动分配的
使用OpenCV的C++接口,不需要考虑内存分配的问题
赋值操作和拷贝构造函数只赋值头部分(指针指向同样的数据)
使用clone和copyTo两个函数则头部和数据完全赋值
arc为原图像
Mat M(100, 100, CV_8UC1, Scalar(127));//100×100,像素值为127单通道
m1.create(arc.size(), arc.type());//与原图大小相同
Mat csrc;//3×3掩模处理原图
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(arcrc, csrc, -1, kernel);
Mat m2 = Mat::zeros(arc.size(), arc.type());//与原图相同规格零矩阵,黑色
Mat m2 = Mat::zeros(2,2,CV_8UC1);//2×2零矩阵
Mat m2 = Mat::eye(2,2,CV_8UC1);//2×2对角矩阵