项目地址:https://github.com/Sherryhaha/Laplace
"laplace.h"文件
// Created by sunguoyan on 16/4/5.
//
#ifndef LAPLACE_LAPLACE_H
#define LAPLACE_LAPLACE_H
#include<iostream>
#include<opencv2/highgui.hpp>
#include<cv.h>
using namespacestd;
using namespacecv;
typedef uchar ty;
class Laplace{
public:
intX_image,Y_image;
intTempltExcute(Mat &src,int *tmplt,inttw,int x,inty);
voidSharpLaplace(Mat &src,Mat&dst);
};
#endif //LAPLACE_LAPLACE_H
laplace.cpp文件:
//
// Created by sunguoyan on 16/4/5.
//
#include "laplace.h"
/**
* 功能:使用模板对灰度图像邻域进行处理
* 参数:src为目标图像,tmplt为模板,tw为邻域大小,x,y为要取得像素的坐标
*/
int Laplace::TempltExcute(Mat &src,int*tmplt,int tw,intx,int y){
inti,j,py,px;
intsum = 0;
for(i=0;i<tw;i++){
for(j=0;j<tw;j++){
py = y-tw/2+i;
px = x-tw/2+j;
sum+=src.at<ty>(py,px)*tmplt[i*tw+j];
}
}
returnsum;
}
/*
* 功能:灰度图像的拉普拉斯锐化处理
* 参数:src为原始图像,dst为锐化结果
*/
void Laplace::SharpLaplace(Mat&src,Mat&dst){
inttmplt[9] = {-1,-1,-1,-1,8,-1,-1,-1,-1};
inth,w;
intscale;
inttmp;
scale =3;
for(h= 1;h < Y_image-1;h++){
for(w= 1;w < X_image - 1;w++){
tmp = TempltExcute(src,tmplt,3,w,h);
tmp = tmp/scale;
tmp = tmp +src.at<ty>(h,w);
tmp = tmp > 255?255:tmp;
tmp = tmp < 0?0:tmp;
dst.at<ty>(h,w)=tmp;
}
}
}
int main(){
Matsrc,dst;
stringfilename = "/Users/sunguoyan/Downloads/picture/heben.jpg";
src =imread(filename,CV_LOAD_IMAGE_GRAYSCALE);
dst.create(src.size(),src.type());
Laplacep;
p.X_image= src.cols;
p.Y_image= src.rows;
p.SharpLaplace(src,dst);
namedWindow("origin");
namedWindow("result");
imshow("origin",src);
imshow("result",dst);
waitKey(0);
return0;
}