// グレイスケール画像のヒストグラムを平滑化した画像を作成するプログラム // プログラム名:smooth_hist.cpp #include "eps-header.hh" // 元画像(src)のヒストグラムを平滑化した処理済画像(dst)に入れる関数 void smooth_hist( class image& src, // 元画像クラス(入力) class image& dst, // 処理済画像クラス(出力) int smooth_level) // 平滑化するヒストグラムの階調の割合 [%](入力) // 25[%]ぐらいを使う { if (src.p == NULL) { printf("元画像がありません。"); return; } printf("\n画像のヒストグラムを平滑化します。\n"); int nr_lines = src.get_nr_lines(); // 元画像のライン数 (縦の画素数) int nr_pixels = src.get_nr_pixels(); // 元画像のピクセル数 (横の画素数) dst.gmake(nr_lines, nr_pixels); // 処理済画像の配列を確保 long int src_hist[NR_GRAY_LEVEL]; // 元画像のヒストグラムの頻度 // 元画像のヒストグラムの頻度を計数する。 for (int ia = 0; ia < NR_GRAY_LEVEL; ia ++) { src_hist[ia] = 0; } for (int iy = 0; iy < nr_lines; iy ++) { for (int ix = 0; ix < nr_pixels; ix ++) { src_hist[ src.p[iy][ix] ] ++; } } if (smooth_level < 0) smooth_level = 0; if (smooth_level > 100) smooth_level = 100; int smooth_gray_level = (NR_GRAY_LEVEL * smooth_level) / 100; long int smooth_hist_count = ((long int)nr_lines * nr_pixels) / smooth_gray_level; printf("ヒストグラム平滑化の階調: %8d\n", smooth_gray_level); printf("ヒストグラム平滑化後の平均頻度: %8d\n", smooth_hist_count); // 階調変換表を作成 int smooth_conv[NR_GRAY_LEVEL]; int level = 0; int hist_stack = 0; for (int ia = 0; ia < NR_GRAY_LEVEL; ia ++) { if ( hist_stack > smooth_hist_count || hist_stack + (src_hist[ia] / 2) > smooth_hist_count ) { level ++; if (level >= smooth_gray_level) { level = smooth_gray_level - 1; } hist_stack = 0; } hist_stack += src_hist[ia]; smooth_conv[ia] = level * NR_GRAY_LEVEL / smooth_gray_level; } // 階調変換表を使って元画像の輝度値を変換 for (int iy = 0; iy < nr_lines; iy ++) { for (int ix = 0; ix < nr_pixels; ix ++) { dst.p[iy][ix] = smooth_conv[ src.p[iy][ix] ]; } } } // プログラムの開始位置 int main() { class image src; // 元画像クラスを宣言 class image dst; // 処理済画像クラスを宣言 src.gload(); // 元画像をBMPファイルから読み込む // BMPファイルを指定したい場合は, // src.gload("パス付きBMPファイル名"); // とする。 // 元画像(src)のヒストグラムを平滑化した処理済画像(dst)に入れる smooth_hist( src, // 元画像クラス(入力) dst, // 処理済画像クラス(出力) 25); // 平滑化するヒストグラムの階調の割合 [%](入力) // 25[%]ぐらいを使う dst.gsave(); // 処理済画像をBMPファイルに保存する // BMPファイルを指定したい場合は, // dst.gsave("パス付きBMPファイル名"); // とする。 }