(5)opencv的基础操作和矩阵的掩模操作

不懂的,可以简单,看看这个网址:https://blog.csdn.net/xiongwen_li/article/details/78503491

图片放到了桌面,所以,图片的路径就是桌面了,剩余的代码如下

 1 #include<iostream>
 2 #include<opencv.hpp>
 3 
 4 using namespace std;
 5 using namespace cv;
 6 
 7 int main()
 8 {
 9     //定义两个位图的类
10     Mat sour, dest;
11     //将你要弄的图片读进来
12     sour=imread("C:\\Users\\32829\\Desktop\\aa.jpg");
13     if (!sour.data)//这里还可以用sour.empty()来检验图片读入是否正确
14     {
15         cout << "图片读入失败" << endl;
16         return -1;
17     }
18     //创建一个要展示图片的窗口
19     namedWindow("原图片展示", 1);
20     imshow("原图片展示", sour);//进行图片展示
21 
22     //创建一个空的和原图片大小一样的图,并把它赋值给dest
23     dest = Mat::zeros(sour.size(), sour.type());
24     //图片的宽度,注意是原图片的列数减了1,为啥减一,暂时理解为是不考虑图片的第一列,还得乘以他的管道数
25     int cols = (sour.cols - 1)*sour.channels();
26     //因为你不考虑他的第一列,所以就设置了一个偏移量,这个变量
27     int offsets = sour.channels();
28     //图片的宽度
29     int rows = sour.rows;
30     
31     for (int row = 1; row < (rows-1); row++)
32     {
33         const uchar* current = sour.ptr<uchar>(row);//获取当前图片当前行的指针
34         const uchar*privious = sour.ptr<uchar>(row - 1);//获取图片上一行行的指针
35         const uchar* next = sour.ptr<uchar>(row + 1);//获取当前图片下一行行的指针
36         uchar* output = dest.ptr<uchar>(row);//获取目标图片当前行的指针
37         for (int col = offsets; col < cols; col++)
38         {
39             //目的图片的当前像素点的计算。 saturate_cast<uchar>,这个就是保证你的RGB不溢出,范围都控制在0-255
40             output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsets] + current[col + offsets] + next[col] + privious[col]));
41         }
42     }
43     namedWindow("新的图片展示");
44     imshow("新的图片展示",dest);
45 
46 
47 
48 
49 
50     waitKey(0);
51     return 1;
52 }

还有一个简单的代码,用opencv自己带的一个函数实现掩膜操作

 1 #include<iostream>
 2 #include<opencv.hpp>
 3 
 4 using namespace std;
 5 using namespace cv;
 6 
 7 int main()
 8 {
 9     //定义两个位图的类
10     Mat sour, dest;
11     //将你要弄的图片读进来
12     sour=imread("C:\\Users\\32829\\Desktop\\aa.jpg");
13     if (!sour.data)//这里还可以用sour.empty()来检验图片读入是否正确
14     {
15         cout << "图片读入失败" << endl;
16         return -1;
17     }
18     //创建一个要展示图片的窗口
19     namedWindow("原图片展示", 1);
20     imshow("原图片展示", sour);//进行图片展示
21 
22 
23 
24 
25     // 使用Filter2D函数
26     Mat result;
27     Mat kern = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
28     filter2D(sour, dest, sour.depth(), kern);
29 
30 
31     namedWindow("新的图片展示");
32     imshow("新的图片展示", dest);
33 
34     waitKey(0);
35     return 1;
36 }

猜你喜欢

转载自www.cnblogs.com/xiaoyoucai/p/10178533.html