图像锐化
代码展示
#include<opencv2\opencv.hpp>
using namespace cv;
Mat& imgSharpen(const Mat& img, char* arith) //arith为3*3模板算子
{
int rows = img.rows; //原图的行
int cols = img.cols * img.channels(); //原图的列
int offsetx = img.channels(); //像素点的偏移量
static Mat dst = Mat::ones(img.rows - 2, img.cols - 2, img.type());
for (int i = 1; i < rows - 1; i++)
{
const uchar* previous = img.ptr<uchar>(i - 1);
const uchar* current = img.ptr<uchar>(i);
const uchar* next = img.ptr<uchar>(i + 1);
uchar* output = dst.ptr<uchar>(i - 1);
for (int j = offsetx; j < cols - offsetx; j++)
{
output[j - offsetx] =
saturate_cast<uchar>(previous[j - offsetx] * arith[0] + previous[j] * arith[1] + previous[j + offsetx] * arith[2] +
current[j - offsetx] * arith[3] + current[j] * arith[4] + current[j + offsetx] * arith[5] +
next[j - offsetx] * arith[6] + next[j] * arith[7] + next[j - offsetx] * arith[8]);
}
}
return dst;
}
int main()
{
Mat img = imread("D:\\Images\\test2.png");
namedWindow("原图像", WINDOW_NORMAL);
imshow("原图像", img);
char arith[9] = { 0, -1, 0, -1, 5, -1, 0, -1, 0 }; //使用拉普拉斯算子
Mat dst1 = imgSharpen(img, arith);
namedWindow("锐化图像", WINDOW_NORMAL);
imshow("锐化图像", dst1);
waitKey(0);
return 0;
}
效果展示