画像処理入門 > 初級編 > 二値化

二値化


二値化
閾値未満を黒、閾値以上を白に二値化するプログラムです。
サンプルは8bitのみ対応しています。

サンプルプログラム
プログラムはこちら void binary(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, int th){ int i, j; int index; int widthStep; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4- widthStep%4; } for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ index = i * widthStep + j; // 閾値以上は255、閾値未満は0にする if(in[index] >= th){ out[index] = 255; } else { out[index] = 0; } } } }