// グレイスケール画像に一様乱数を使って点ノイズを加えるプログラム // プログラム名:dot_noise.c #include "eps-header.hh" #include // 元画像(src)に点ノイズを加えて処理済画像(dst)に入れる関数 void add_dot_noise( class image& src, // 元画像クラス(入力) class image& dst, // 処理済画像クラス(出力) int nr_dots, // 点ノイズの数(入力) unsigned int rnd_seed) // 乱数の種(入力) { if (src.p == NULL) { printf("元画像がありません。"); return; } printf("\nグレイスケール画像に一様乱数を使って点ノイズを加えます。\n"); printf("点の数は,%dです。\n", nr_dots); printf("乱数の種は,%dです。\n", rnd_seed); srand(rnd_seed); // 乱数の種をまく int nr_lines = src.get_nr_lines(); // 元画像のライン数 (縦の画素数) int nr_pixels = src.get_nr_pixels(); // 元画像のピクセル数 (横の画素数) dst = src; // 処理済画像の配列を確保し,元画像を入れる for (int i = 0; i < nr_dots; i ++) { int iy = rand () % nr_lines; int ix = rand () % nr_pixels; dst.p[iy][ix] = MAX_GRAY_LEVEL; } } // プログラムの開始位置 int main() { class image src; // 元画像クラスを宣言 class image dst; // 処理済画像クラスを宣言 src.gload(); // 元画像をBMPファイルから読み込む // BMPファイルを指定したい場合は, // src.gload("パス付きBMPファイル名"); // とする。 unsigned int seed = (unsigned int)time(0); // 元画像(src)に点ノイズを加えて処理済画像(dst)に入れる。 add_dot_noise( src, // 元画像クラス(入力) dst, // 処理済画像クラス(出力) 1000, // 点ノイズの数(入力) seed); // 乱数の種(入力) dst.gsave(); // 処理済画像をBMPファイルに保存する // BMPファイルを指定したい場合は, // dst.gsave("パス付きBMPファイル名"); // とする。 }