画像処理入門 > 初級編 > 平滑化

平滑化


上下反転
周辺9個のデータを使って平滑化するプログラムです。
サンプルは8bitのみ対応しています。

サンプルプログラム
プログラムはこちら 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; } } }