#include #include "cv.h" #include "cxcore.h" #include "highgui.h" #pragma comment(lib,"cv.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"highgui.lib") void smoothing(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, double **filterData, int filterHeight, int filterWidth); int main(){ IplImage *in, *out; int i,j; double filter[3][3] = { {1.0/9.0, 1.0/9.0, 1.0/9.0}, {1.0/9.0, 1.0/9.0, 1.0/9.0}, {1.0/9.0, 1.0/9.0, 1.0/9.0} }; double **filterData; filterData = (double**)malloc(sizeof(double*)*3); for(i = 0; i <3; ++i){ filterData[i] = (double*)malloc(sizeof(double)*3); } in = cvLoadImage("./image640.bmp", 0); out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels); for(i = 0; i < 3; ++i){ for(j = 0; j < 3; ++j){ filterData[i][j] = filter[i][j]; } } smoothing((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels, filterData, 3, 3); 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 smoothing(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, double **filterData, int filterHeight, int filterWidth){ int i, j, k, l; int index; int widthStep; double tmp; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4- widthStep%4; } for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ // 配列を超える場合は平滑化しない if( (i-(filterHeight/2) < 0) || (i+(filterHeight/2) > inHeight) || (j-(filterWidth/2) < 0) || (j+(filterWidth/2) > inWidth) ){ out[i * widthStep + j] = in[i * widthStep + j]; continue; } tmp = 0.0; for(k = 0; k < filterHeight; ++k){ for(l = 0; l < filterWidth; ++l){ tmp += (double)in[(i-(int)(filterHeight/2-k))*widthStep + j-(int)(filterWidth/2-l)] * filterData[k][l]; } } out[i * widthStep + j] = (unsigned char)tmp; } } }