拉普拉斯模板对该图像滤波后的结果。由图可以看出,将原始图像通过拉普拉斯变换后增强了图像中灰度突变处的对比度,使图像中小的细节部分得到增强并保留了图像的背景色调,使图像的细节比原始图像更加清晰。基于拉普拉斯变换的图像增强已成为图像锐化处理的基本工具。
#include <opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//-----------------------------------【命名空间声明部分】---------------------------------------
// 描述:包含程序所使用的命名空间
//-----------------------------------------------------------------------------------------------
using namespace cv;
using namespace std;
//手动实现拉普拉斯算子图像锐化
void sharpenImage1(const Mat &image, Mat &result)
{
result.create(image.size(), image.type());//为输出图像分配内容
/*拉普拉斯滤波核3*3
0 -1 0
-1 5 -1
0 -1 0 */
//处理除最外围一圈外的所有像素值
for (int i = 1; i<image.rows - 1; i++)
{
const uchar * pre = image.ptr<const uchar>(i - 1);//前一行
const uchar * cur = image.ptr<const uchar>(i);//当前行,第i行
const uchar * next = image.ptr<const uchar>(i + 1);//下一行
uchar * output = result.ptr<uchar>(i);//输出图像的第i行
int ch = image.channels();//通道个数
int startCol = ch;//每一行的开始处理点
int endCol = (image.cols - 1)* ch;//每一行的处理结束点
for (int j = startCol; j < endCol; j++)
{
//输出图像的遍历指针与当前行的指针同步递增, 以每行的每一个像素点的每一个通道值为一个递增量, 因为要考虑到图像的通道数
//saturate_cast<uchar>保证结果在uchar范围内
*output++ = saturate_cast<uchar>(5 * cur[j] - pre[j] - next[j] - cur[j - ch] - cur[j + ch]);
}
}
//将最外围一圈的像素值设为0
result.row(0).setTo(Scalar(0));
result.row(result.rows - 1).setTo(Scalar(0));
result.col(0).setTo(Scalar(0));
result.col(result.cols - 1).setTo(Scalar(0));
/*/或者也可以尝试将最外围一圈设置为原图的像素值
image.row(0).copyTo(result.row(0));
image.row(image.rows-1).copyTo(result.row(result.rows-1));
image.col(0).copyTo(result.col(0));
image.col(image.cols-1).copyTo(result.col(result.cols-1));*/
}
//调用OpenCV函数实现拉普拉斯算子图像锐化
void sharpenImage2(const Mat &image, Mat &result)
{
Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(image, result, image.depth(), kernel);
}
//main函数
void main() {
Mat mat = imread("1.jpg");
Mat result1;
Mat result2;
sharpenImage1(mat, result1);
sharpenImage2(mat, result2);
namedWindow("src");
namedWindow("result1");
namedWindow("result2");
imshow("src", mat);
imshow("result1", result1);
imshow("result2", result2);
}
ref
http://www.cnblogs.com/xfzhang/archive/2011/01/19/1939020.html
https://blog.csdn.net/dcrmg/article/details/53677739