第一課
掩模處理:
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對角矩陣