03-矩阵的掩膜操作
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main() {
Mat src, dst;
src = imread("D:\\heroRcData\\opencvProject\\arrowImg\\01\\01.jpg");
if (!src.data) {
cout << "could not load image..." << endl;
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
int cols = src.cols * src.channels();
int rows = src.rows;
int channels = src.channels();
dst = Mat::zeros(src.size(), src.type());
for (int i = 1; i < rows - 1; i++) {
const uchar* current = src.ptr<uchar>(i);
const uchar* previous = src.ptr<uchar>(i - 1);
const uchar* next = src.ptr<uchar>(i + 1);
uchar* output = dst.ptr<uchar>(i);
for (int j = 1; j < cols - 1; j++) {
output[j] = saturate_cast<uchar>(5 * current[j] - (previous[j] + next[j] + current[j - channels] + current[j + channels]));
}
}
namedWindow("output image", CV_WINDOW_AUTOSIZE);
imshow("output image", dst);
double t = getTickCount();
Mat dst1;
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst1, src.depth(), kernel);
namedWindow("output1 image", CV_WINDOW_AUTOSIZE);
imshow("output1 image", dst1);
double timeConsume = (getTickCount() - t) / getTickFrequency();
cout << "time consume " << timeConsume << endl;
waitKey(0);
return 0;
}