#include<cv.h>
#include<highgui.h>
using?namespace?std;
#define?MGETP1(x,y,z,ch,im,type)?(((type*)(im->imageData+(y)*im->widthStep))[ch*(x)+z])
int?main(){string?s;
s=/*圖的路徑,妳自己填吧*/;
IplImage*?sim=cvLoadImage(s.c_tr());
IplImage*?resx=cvCreateImage(cvGetSize(sim),IPL_DEPTH_32F,3);
IplImage*?resy=cvCreateImage(cvGetSize(sim),IPL_DEPTH_32F,3);
int?x,y;
for(x=2;x<sim->width-2;x++)
{
for(y=2;y<sim->height-2;y++)
{
int?x0,x1,x2,y0,y1,y2;
x0=MGETP1(x-1,y,0,3,sim,uchar);
x1=MGETP1(x,y,0,3,sim,uchar);
x2=MGETP1(x+1,y,0,3,sim,uchar);
MGETP1(x,y,0,3,resx,float)=x0+x2-2*x1;
x0=MGETP1(x,y-1,0,3,sim,uchar);
x1=MGETP1(x,y,0,3,sim,uchar);
x2=MGETP1(x,y+1,0,3,sim,uchar);
MGETP1(x,y,0,3,resy,float)=y0+y2-2*y1;
x0=MGETP1(x-1,y,0,3,sim,uchar);
x1=MGETP1(x,y,0,3,sim,uchar);
x2=MGETP1(x+1,y,0,3,sim,uchar);
MGETP1(x,y,0,3,resx,float)=x0+x2-2*x1;
x0=MGETP1(x,y-1,1,3,sim,uchar);
x1=MGETP1(x,y,1,3,sim,uchar);
x2=MGETP1(x,y+1,1,3,sim,uchar);
MGETP1(x,y,1,3,resy,float)=y0+y2-2*y1;
x0=MGETP1(x-1,y,2,3,sim,uchar);
x1=MGETP1(x,y,2,3,sim,uchar);
x2=MGETP1(x+1,y,2,3,sim,uchar);
MGETP1(x,y,2,3,resx,float)=x0+x2-2*x1;
x0=MGETP1(x,y-1,2,3,sim,uchar);
x1=MGETP1(x,y,2,3,sim,uchar);
x2=MGETP1(x,y+1,2,3,sim,uchar);
MGETP1(x,y,2,3,resy,float)=y0+y2-2*y1;
}
}
cvShowImage("x2Diff",?resx);
cvShowImage("y2Diff",?resy);
cvWaitKey();
cvReleaseImage(&resx);
cvReleaseImage(&resy);
cvReleaseImage(&sim);
return?0;
}
出來的二階差分妳要自己規格化成0-255之間就可以食用了~