#include #include #include #include #pragma comment(lib,"cv.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"highgui.lib") void laplacian(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel); int main(){ IplImage *in, *out; in = cvLoadImage("./image.bmp", 0); out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels); laplacian((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels); cvNamedWindow("in", 1); cvShowImage("in", in); cvNamedWindow("out", 1); cvShowImage("out", out); cvWaitKey(-1); cvDestroyWindow("in"); cvDestroyWindow("out"); cvReleaseImage( &in ); cvReleaseImage( &out ); return 0; } void laplacian(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel){ int i, j, k, l; int weight[9] = {2, 0, 2, 0, -8, 0, 2, 0, 2}; int *imgTmp; int weightSize = 3; int start = -1; int end = start + weightSize; double dataTmp; int widthStep; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4 - widthStep % 4; } for(i = 1; i < (inHeight - 1); ++i){ for(j = 1; j < (inWidth - 1); ++j){ dataTmp = 0; for(k = start; k < end; ++k){ for(l = start; l < end; ++l){ dataTmp += weight[(k - start) * weightSize + (l - start)] * (unsigned char)in[(i + k) * widthStep + (j + l)]; } } // 255を超えた値は255、0未満の値は絶対値にする if(dataTmp > 255){ dataTmp = 255; } else if(dataTmp < 0){ dataTmp = -dataTmp; } out[i * widthStep + j] = dataTmp; } } }