#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat img = imread("123.jpg",-1);
int cols = (img.cols - 1)*img.channels();
//列,乘以 img.channels() 是因为有RGB三个通道,减一是因为mask 矩阵为3*3 上下左右各有一列(行)触及不到。
int rows = img.rows;//行。
int offset = img.channels();//RGB img.channels() = 3;
Mat out1,out2;
out1 = Mat::zeros(img.size(),img.type());
out2 = Mat::zeros(img.size(),img.type());
for (int row = 1;row<(rows - 1);row++)
{
const uchar* previous = img.ptr<uchar>(row-1);
const uchar* current = img.ptr<uchar>(row);
const uchar* next = img.ptr<uchar>(row+1);
uchar* output1 = out1.ptr<uchar>(row);
uchar* output2 = out2.ptr<uchar>(row);
for (int col = offset; col<cols;col++)
{
output1[col] = 5*current[col] - current[col-offset] - current[col+offset] - previous[col] - next[col];
output2[col] = saturate_cast<uchar>(5*current[col] - current[col-offset] - current[col+offset] - previous[col] - next[col]) ;
//output2[col] = saturate_cast<uchar>(output1[col]) ; //这么写哪里有问题?
}
}
imshow("1",out1);
imshow("2",out2);
imwrite("t1.jpg",out1);
imwrite("t2.jpg",out2);
waitKey();
return 1;
}
之前的
增加防止溢出saturate_cast