2018年4月23日10:50:50
总结:
1. 再将图像的未处理点进行置零操作的时候,忘记 -1 了,原来图像的行也是按照数组的排列来算的啊
2.
注意了:在进行循环的时候,我们要注意
j和i
是从
1
开始的,也就是说,我们不处理图像的边界
#include "mainwindow.h"
#include <QApplication>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
void sharpen(const Mat & image, Mat & result)
{
result.create(image.size(), image.type());
int i = image.rows;
int j = image.cols * image.channels();
for(int x=1; x<i-1; x++)//第一行,和最后一行无需处理
{
const uchar * previous = image.ptr<uchar>(x-1);
const uchar * current = image.ptr<uchar>(x);
const uchar * next = image.ptr<uchar>(x+1);
uchar * data_out = result.ptr<uchar>(x);
for(int y=3; y<j-3; y++)
{
data_out[y] = static_cast<uchar>(5 * current[y] - previous[y] - next[y] - current[y-3] - current[y+3]);
}
}
//将未处理过的像素设为零
result.row(0).setTo(Scalar(0,0,0));
result.row(result.rows-1).setTo(Scalar(0,0,0));
result.col(0).setTo(Scalar(0,0,0));
result.col(result.cols-1).setTo(Scalar(0,0,0));
}
int main(int argc, char *argv[])
{
Mat image = imread("a.jpg");
Mat result;
sharpen(image, result);
namedWindow("原图像");
imshow("原图像", image);
namedWindow("锐化后图像");
imshow("锐化后图像", result);
waitKey();
return 0;
}