//引用自忘记哪里的程序了
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "math.h"
#include <iostream>
using std::cout;
using std::endl;
using std::sort;
#define B(image,x,y)((uchar*)(image->imageData+image->widthStep*(y)))[(x*3)]//B
#define G(image,x,y)((uchar*)(image->imageData+image->widthStep*(y)))[(x*3)+1]//G
#define R(image,x,y)((uchar*)(image->imageData+image->widthStep*(y)))[(x*3)+2]//R
#define S(image,x,y)((uchar*)(image->imageData+image->widthStep*(y)))[(x)] //S
void cvSharp(IplImage* &src)
{
IplImage *dst=NULL;
IplImage *dst1=NULL;
dst=cvCloneImage(src);
int m,m1,m2;
int i,j;
dst1=cvCloneImage(dst);
int templt[9]={-1,-1,-1,-1,8,-1,-1,-1,-1};
int x,y;
int scale;
int size=3;
scale = 3;//设定衰减因子
//依次对源图像的每个像素进行处理
for(j=size/2;j<dst1->height-size/2;j++)
for(i=size/2;i<dst1->width-size/2;i++)
{
m=0;
m1=0;
m2=0;
for(x=0;x<size;x++)
for(y=0;y<size;y++)
{
m+=B(dst1,(i-size/2+x),(j-size/2+y))*templt[x*size+y];
m1+=G(dst1,(i-size/2+x),(j-size/2+y))*templt[x*size+y];
m2+=R(dst1,(i-size/2+x),(j-size/2+y))*templt[x*size+y];
}
m/= scale;
//对中心像素进行增强
m=B(dst,i,j)+m;
//过限处理
m = m>255?255:m;
m = m<0?0:m;
m1/= scale;
//对中心像素进行增强
m1=G(dst,i,j)+m1;
//过限处理
m1 = m1>255?255:m1;
m1 = m1<0?0:m1;
m2/= scale;
//对中心像素进行增强
m2=R(dst,i,j)+m2;
//过限处理
m2 = m2>255?255:m2;
m2 = m2<0?0:m2;
B(dst1,i,j)=m;
G(dst1,i,j)=m1;
R(dst1,i,j)=m2;
}
}