【OpenCV入门学习笔记1】:Mat对象的指针操作和掩膜操作
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【OpenCV入门学习笔记1】:Mat对象的指针操作和掩膜操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                b站:https://www.bilibili.com/video/BV1uW411d7Wf?p=5
 下面是我在b站上看視頻學習的筆記和操作的示例代碼
 
實例代碼
#include<opencv2/opencv.hpp> #include<iostream> #include<math.h>using namespace cv;int main(int argc, char** argv) {Mat src, dst;src = imread("E:/picture/pic_cv/pic.jpg");if (!src.data) {printf("Could not load image...\n");return -1;}namedWindow("input image", WINDOW_AUTOSIZE);imshow("input image", src);//-------------------------------------------------------------------------//----------------------【自定義掩膜操作過程】-----------------------------//------------------------------------------------------------------------int cols = src.cols * src.channels();//RGB圖像是三通道圖像int offsetx = src.channels();//左右漂移多少是通道數決定的int rows = src.rows;dst = Mat::zeros(src.size(), src.type());//對dst圖像矩陣初始化為src的大小和類型 //一定要注意初始化!!for (int row = 1; row < (rows - 1); row++) {const uchar* previous = src.ptr<uchar>(row - 1);uchar* current = src.ptr<uchar>(row);const uchar* next = src.ptr<uchar>(row + 1);uchar* output = dst.ptr<uchar>(row);//創建指針指向dst的row行for (int col = offsetx; col < cols - offsetx; col++){//方法一:這樣出來的圖像跟麻臉一樣,因為有的數據超過了255,或小于0,造成溢出//output[col] = 5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]);//方法二:用saturate_cast(飽和函數)函數 處理溢出數據output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));}}//-------------------------------------------------------------------------//----------------------【end1】-----------------------------//------------------------------------------------------------------------namedWindow("contrast image demo 1", WINDOW_AUTOSIZE);imshow("contrast image demo 1", dst);//-------------------------------------------------------------------------//----------------------【調用filter2D函數操作】-----------------------------//------------------------------------------------------------------------double t = getTickCount();Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,-1, 5, -1,0, -1, 0);filter2D(src, dst, src.depth(), kernel);double timeconsume = (getTickCount() - t) / getTickFrequency();printf("time consume :%.2lf", timeconsume);//-------------------------------------------------------------------------//----------------------【end2】-----------------------------//------------------------------------------------------------------------namedWindow("contrast image demo 2", WINDOW_AUTOSIZE);imshow("contrast image demo 2", dst);waitKey(0);return 0; }總結
以上是生活随笔為你收集整理的【OpenCV入门学习笔记1】:Mat对象的指针操作和掩膜操作的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 转载:掩膜矩阵操作数学解释(权重表,锐化
- 下一篇: 【OpenCV入门学习笔记2】:Mat对
