画像処理入門 > フィルタ > Prewittフィルタ

Prewittフィルタ


Prewittフィルタ
Prewittフィルタのプログラムです。
画像の端は処理していません。

フィルタに使う行列の値が違うだけで、中身はSobelフィルタと同じです。

サンプルプログラム
プログラムはこちら void Prewitt(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel){ int i, j, k, l; int weightH[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int weightV[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int *imgTmp; int weightSize = 3; int start = -1; int end = start + weightSize; double dataTmpH; double dataTmpV; double dataTmpSum; 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){ dataTmpH = 0; dataTmpV = 0; for(k = start; k < end; ++k){ for(l = start; l < end; ++l){ //水平方向 dataTmpH += weightH[(k - start) * weightSize + (l - start)] * (unsigned char)in[(i + k) * widthStep + (j + l)]; //垂直方向 dataTmpV += weightV[(k - start) * weightSize + (l - start)] * (unsigned char)in[(i + k) * widthStep + (j + l)]; } } dataTmpSum = sqrt(dataTmpH * dataTmpH + dataTmpV * dataTmpV); //255を超えた値は255、0未満の値は絶対値 if(dataTmpSum > 255){ dataTmpSum = 255; } else if(dataTmpSum < 0){ dataTmpSum = -dataTmpSum; } out[i * widthStep + j] = dataTmpSum; } } }