// 2値画像の膨張処理(4近傍)プログラム // (2値画像は,背景(黒):MIN_GRAY_LEVEL,図形(白):MAX_GRAY_LEVELのグレイスケール画像とする。) // プログラム名:dilate_4.cpp #include "eps-header.hh" // 元画像(src)を膨張処理(4近傍)して処理済画像に入れる関数 // 4近傍を示す●が一つでも図形(白)があると,注目画素☆を図形(白)にする。 // ┏━┳━┳━┓ // ┃ ┃●┃ ┃ // ┣━╋━╋━┫ // ┃●┃☆┃●┃ // ┣━╋━╋━┫ // ┃ ┃●┃ ┃ // ┗━┻━┻━┛ // void dilate_4n( class image& src, // 元画像クラス(入力) class image& dst, // 処理済画像クラス(出力) int count) // 膨張処理の回数 { if (src.p == NULL) { printf("元画像がありません。"); return; } printf("\n2値画像を4近傍で %d回 膨張処理します。\n", count); int nr_lines = src.get_nr_lines(); // 元画像のライン数 (縦の画素数) int nr_pixels = src.get_nr_pixels(); // 元画像のピクセル数 (横の画素数) dst.gmake(nr_lines, nr_pixels); // 処理済画像の配列を確保 for (int k = 0; k < count; k ++) { for (int iy = 0; iy < nr_lines; iy ++) { for (int ix = 0; ix < nr_pixels; ix ++) { dst.p[iy][ix] = src.p[iy][ix]; if (iy - 1 >= 0) { if (src.p[iy - 1][ix ] == MAX_GRAY_LEVEL) {dst.p[iy][ix] = MAX_GRAY_LEVEL; continue;} } if (ix - 1 >= 0) if (src.p[iy ][ix - 1] == MAX_GRAY_LEVEL) {dst.p[iy][ix] = MAX_GRAY_LEVEL; continue;} if (ix + 1 < nr_pixels) if (src.p[iy ][ix + 1] == MAX_GRAY_LEVEL) {dst.p[iy][ix] = MAX_GRAY_LEVEL; continue;} if (iy + 1 < nr_lines) { if (src.p[iy + 1][ix ] == MAX_GRAY_LEVEL) {dst.p[iy][ix] = MAX_GRAY_LEVEL; continue;} } } } src = dst; // 膨張処理した画像を元画像にする。 } } // プログラムの開始位置 int main() { class image src; // 元画像クラスを宣言 class image dst; // 処理済画像クラスを宣言 src.gload(); // 元画像をBMPファイルから読み込む // BMPファイルを指定したい場合は, // src.gload("パス付きBMPファイル名"); // とする。 // 元画像(src)を膨張処理(4近傍)して処理済画像に入れる dilate_4n( src, // 元画像クラス(入力) dst, // 処理済画像クラス(出力) 2); // 膨張処理の回数 dst.gsave(); // 処理済画像をBMPファイルに保存する // BMPファイルを指定したい場合は, // dst.gsave("パス付きBMPファイル名"); // とする。 }