图像锐化
代码展示
#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;
}
效果展示


该博客介绍了如何使用OpenCV库实现图像锐化。通过展示C++代码,作者演示了应用3x3模板算子(如拉普拉斯算子)进行图像处理,以增强图像细节的过程。代码实现了从读取图像到显示锐化结果的完整流程。
1万+

被折叠的 条评论
为什么被折叠?



