opencv05-掩膜
参考:
https://blog.csdn.net/shuiyixin/article/details/88825549
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <array>
#include <algorithm>using namespace std;
using namespace cv;int main() {string filename1 = R"(D:\workspace\cpp_workspace\my-cv\data\img\1.png)";string filename2 = R"(D:\workspace\cpp_workspace\my-cv\data\img\2.png)";Mat src, dst, mask;src = imread(filename1);imshow("image", src);//1. 获取图像的像素指针// Mat.ptr<uchar>(int i=0) //获取像素矩阵的指针,索引i表示第几行,从0开始计行数。const uchar *curr = src.ptr<uchar>(0); //获得第一行指针cout << curr[1] << endl; // p(row, col) =current[col] //获取当前像素点P(row, col)的像素值。cout << saturate_cast<uchar>(-10) << endl; //返回 0cout << saturate_cast<uchar>(288) << endl; //返回255cout << saturate_cast<uchar>(100) << endl;//返回100int 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++) {// saturate_cast<uchar> 这个函数的功能是确保RGB值得范围在0~255之间output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));}}imshow("dst", dst);waitKey();return 0;
}
使用filter2D来实现:
int main() {string filename1 = R"(D:\workspace\cpp_workspace\my-cv\data\img\1.png)";string filename2 = R"(D:\workspace\cpp_workspace\my-cv\data\img\2.png)";Mat src, dst, mask;src = imread(filename1);/** void filter2D(InputArray src,OutputArray dst,int ddepth,InputArray kernel,Point anchor = Point(-1,-1),double delta = 0,int borderType = BORDER_DEFAULT);(1).InputArray类型的src ,输入图像。(2).OutputArray类型的dst ,输出图像,图像的大小、通道数和输入图像相同。(3).int类型的ddepth,目标图像的所需深度。(4).InputArray类型的kernel,卷积核(或者更确切地说是相关核).是一种单通道浮点矩阵;如果要将不同的核应用于不同的通道,请使用split将图像分割成不同的颜色平面,并分别对其进行处理。。(5).Point类型的anchor,表示锚点(即被平滑的那个点).,注意他有默认值Point(-1,-1)。如果这个点坐标是负值的话,就表示取核的中心为锚点,所以默认值Point(-1,-1)表示这个锚点在核的中心。。(6).double类型的delta,在将筛选的像素存储到dst中之前添加到这些像素的可选值。说的有点专业了其实就是给所选的像素值添加一个值delta。(7).int类型的borderType,用于推断图像外部像素的某种边界模式。有默认值BORDER_DEFAULT。*/Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);cv::filter2D(src, dst, src.depth(), kernel);imshow("input image", src);imshow("contrast image demo", dst);waitKey();return 0;
}