/*
http://i...content-available-to-author-only...e.com/fskVl3
http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1349527750/951
> [1] 授業単元:2値化
> [2] 問題文(含コード&リンク):
> http://i...content-available-to-author-only...e.com/fskVl3
> 画像読み込み→2値化→2値化した画像を表示するプログラムを作成せよ
> 画像読み込みまでは出来ましたが、2値化がよくわかりませんでした。
> フルカラー前提でRGBの全チャンネルが同値なら濃淡画像に変換され、その濃淡画像を閾値処理によって2値にするらしいですがその辺がよくわかりません
> [3] 環境
> [3.1] OS:Windows7
> [3.2] コンパイラ名とバージョン:VS2010
> [3.3] 言語:C
> [4] 期限:12/1
> [5] その他の制限:出来るだけ標準ライブラリでお願いします
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define WIDTH (640)
#define HEIGHT (480)
#define BMP_HEADER_SIZE (54)
#define INPUT_IMAGE_FILE "image.bmp"
#define OUT_IMAGE_FILE "image2.bmp"
#define OUT_IMAGE_COMMAND "start" // image 出力コマンド
//#define OUT_IMAGE_COMMAND "eog" // Linux Gnome の「Eye of GNOME」を使う場合
typedef struct color {
unsigned char r; // 入力画像 赤
unsigned char g; // 緑
unsigned char b; // 青
int v; // 出力画像 2値 (0 or 1)
} COLOR;
int main()
{
int i, j;
int mode = 2; // 2値変換モード (1:要求通り、2:RGBの平均値から計算)
unsigned char sikiichi = 0x80; // しきい値(これ以上なら白、未満なら黒)
int ave; // RGB3値平均計算用
unsigned char head[BMP_HEADER_SIZE]; // BMPヘッダ
static COLOR data[WIDTH][HEIGHT], *pColor; // データ
FILE *fp; // ファイル
// init
memset(data
, 0x00, sizeof(data
));
// read
if ((fp
= fopen(INPUT_IMAGE_FILE
, "rb")) == NULL
) { fprintf(stderr
, "file open error\n"); }
fread(head
, BMP_HEADER_SIZE
, 1, fp
); for (j = HEIGHT - 1; j >= 0; j--) {
for (i = 0; i < WIDTH; i++) {
pColor = &data[i][j];
}
}
// 2値計算
for (j = HEIGHT - 1; j >= 0; j--) {
for (i = 0; i < WIDTH; i++) {
pColor = &data[i][j];
switch (mode) {
case 1:
// ※ RGB値が同値でないドットは「黒」とする
if ((pColor->b == pColor->g) && (pColor->g == pColor->r) && (sikiichi <= pColor->r)) {
data[i][j].v = 1;
}
break;
case 2:
ave = (int) pColor->b + (int) pColor->g + (int) pColor->r;
ave /= 3;
if (sikiichi <= ave) {
data[i][j].v = 1;
}
break;
}
}
}
// 画像出力
fp
= fopen(OUT_IMAGE_FILE
, "wb"); fwrite(head
, BMP_HEADER_SIZE
, 1, fp
); for (j = HEIGHT - 1; j >= 0; j--) {
for (i = 0; i < WIDTH; i++) {
if (data[i][j].v) {
} else {
}
}
}
system(OUT_IMAGE_COMMAND
" " OUT_IMAGE_FILE
);
// end
return 0;
}
LyoKICAgIGh0dHA6Ly9pLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5lLmNvbS9mc2tWbDMKIAogICAgaHR0cDovL3QuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xMzQ5NTI3NzUwLzk1MQogICAgID4gWzFdIOaOiOalreWNmOWFg++8mjLlgKTljJYKICAgICA+IFsyXSDllY/poYzmloco5ZCr44Kz44O844OJJuODquODs+OCrynvvJoKICAgICA+IGh0dHA6Ly9pLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5lLmNvbS9mc2tWbDMKICAgICA+IOeUu+WDj+iqreOBv+i+vOOBv+KGkjLlgKTljJbihpIy5YCk5YyW44GX44Gf55S75YOP44KS6KGo56S644GZ44KL44OX44Ot44Kw44Op44Og44KS5L2c5oiQ44Gb44KICiAgICAgPiDnlLvlg4/oqq3jgb/ovrzjgb/jgb7jgafjga/lh7rmnaXjgb7jgZfjgZ/jgYzjgIEy5YCk5YyW44GM44KI44GP44KP44GL44KK44G+44Gb44KT44Gn44GX44Gf44CCCiAgICAgPiDjg5Xjg6vjgqvjg6njg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xvciB7CiAgICB1bnNpZ25lZCBjaGFyIHI7ICAgICAgICAgICAgLy8g5YWl5Yqb55S75YOPIOi1pAogICAgdW5zaWduZWQgY2hhciBnOyAgICAgICAgICAgIC8vICAgICAgICAgIOe3kQogICAgdW5zaWduZWQgY2hhciBiOyAgICAgICAgICAgIC8vICAgICAgICAgIOmdkgogICAgaW50IHY7ICAgICAgICAgICAgICAgICAgICAgIC8vIOWHuuWKm+eUu+WDjyAy5YCkICgwIG9yIDEpCn0gQ09MT1I7CgppbnQgbWFpbigpCnsKICAgIGludCBpLCBqOwogICAgaW50IG1vZGUgPSAyOyAgICAgICAgICAgICAgIC8vIDLlgKTlpInmj5vjg6Ljg7zjg4kgKDE66KaB5rGC6YCa44KK44CBMjpSR0Ljga7lubPlnYflgKTjgYvjgonoqIjnrpcpCiAgICB1bnNpZ25lZCBjaGFyIHNpa2lpY2hpID0gMHg4MDsgIC8vIOOBl+OBjeOBhOWApCjjgZPjgozku6XkuIrjgarjgonnmb3jgIHmnKrmuoDjgarjgonpu5IpCiAgICBpbnQgYXZlOyAgICAgICAgICAgICAgICAgICAgLy8gUkdCM+WApOW5s+Wdh+ioiOeul+eUqAogICAgdW5zaWduZWQgY2hhciBoZWFkW0JNUF9IRUFERVJfU0laRV07ICAgIC8vIEJNUOODmOODg+ODgAogICAgc3RhdGljIENPTE9SIGRhdGFbV0lEVEhdW0hFSUdIVF0sICpwQ29sb3I7ICAvLyDjg4fjg7zjgr8KICAgIEZJTEUgKmZwOyAgICAgICAgICAgICAgICAgICAvLyDjg5XjgqHjgqTjg6sKCiAgICAvLyBpbml0CiAgICBtZW1zZXQoZGF0YSwgMHgwMCwgc2l6ZW9mKGRhdGEpKTsKCiAgICAvLyByZWFkCiAgICBpZiAoKGZwID0gZm9wZW4oSU5QVVRfSU1BR0VfRklMRSwgInJiIikpID09IE5VTEwpIHsKICAgICAgICBmcHJpbnRmKHN0ZGVyciwgImZpbGUgb3BlbiBlcnJvclxuIik7CiAgICAgICAgZXhpdCgxKTsKICAgIH0KICAgIHJld2luZChmcCk7CiAgICBmcmVhZChoZWFkLCBCTVBfSEVBREVSX1NJWkUsIDEsIGZwKTsKICAgIGZvciAoaiA9IEhFSUdIVCAtIDE7IGogPj0gMDsgai0tKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IFdJRFRIOyBpKyspIHsKICAgICAgICAgICAgcENvbG9yID0gJmRhdGFbaV1bal07CiAgICAgICAgICAgIHBDb2xvci0+YiA9IGdldGMoZnApOwogICAgICAgICAgICBwQ29sb3ItPmcgPSBnZXRjKGZwKTsKICAgICAgICAgICAgcENvbG9yLT5yID0gZ2V0YyhmcCk7CiAgICAgICAgfQogICAgfQogICAgZmNsb3NlKGZwKTsKCiAgICAvLyAy5YCk6KiI566XCiAgICBmb3IgKGogPSBIRUlHSFQgLSAxOyBqID49IDA7IGotLSkgewogICAgICAgIGZvciAoaSA9IDA7IGkgPCBXSURUSDsgaSsrKSB7CiAgICAgICAgICAgIHBDb2xvciA9ICZkYXRhW2ldW2pdOwogICAgICAgICAgICBzd2l0Y2ggKG1vZGUpIHsKICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgICAgLy8g4oC7IFJHQuWApOOBjOWQjOWApOOBp+OBquOBhOODieODg+ODiOOBr+OAjOm7kuOAjeOBqOOBmeOCiwogICAgICAgICAgICAgICAgaWYgKChwQ29sb3ItPmIgPT0gcENvbG9yLT5nKSAmJiAocENvbG9yLT5nID09IHBDb2xvci0+cikgJiYgKHNpa2lpY2hpIDw9IHBDb2xvci0+cikpIHsKICAgICAgICAgICAgICAgICAgICBkYXRhW2ldW2pdLnYgPSAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICAgIGF2ZSA9IChpbnQpIHBDb2xvci0+YiArIChpbnQpIHBDb2xvci0+ZyArIChpbnQpIHBDb2xvci0+cjsKICAgICAgICAgICAgICAgIGF2ZSAvPSAzOwogICAgICAgICAgICAgICAgaWYgKHNpa2lpY2hpIDw9IGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGRhdGFbaV1bal0udiA9IDE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyDnlLvlg4/lh7rlipsKICAgIGZwID0gZm9wZW4oT1VUX0lNQUdFX0ZJTEUsICJ3YiIpOwogICAgZndyaXRlKGhlYWQsIEJNUF9IRUFERVJfU0laRSwgMSwgZnApOwogICAgZm9yIChqID0gSEVJR0hUIC0gMTsgaiA+PSAwOyBqLS0pIHsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgV0lEVEg7IGkrKykgewogICAgICAgICAgICBpZiAoZGF0YVtpXVtqXS52KSB7CiAgICAgICAgICAgICAgICBwdXRjKDB4RkYsIGZwKTsKICAgICAgICAgICAgICAgIHB1dGMoMHhGRiwgZnApOwogICAgICAgICAgICAgICAgcHV0YygweEZGLCBmcCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwdXRjKDAsIGZwKTsKICAgICAgICAgICAgICAgIHB1dGMoMCwgZnApOwogICAgICAgICAgICAgICAgcHV0YygwLCBmcCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBmY2xvc2UoZnApOwogICAgc3lzdGVtKE9VVF9JTUFHRV9DT01NQU5EICIgIiBPVVRfSU1BR0VfRklMRSk7CgogICAgLy8gZW5kCiAgICByZXR1cm4gMDsKfQ==