/*
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;
unsigned char sikiichi = 0x80; // しきい値(これ以上なら白、未満なら黒)
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];
// ※ RGB値が同値でないドットは「黒」とする
if ((pColor->b == pColor->g) && (pColor->g == pColor->r) && (sikiichi <= pColor->r)) {
data[i][j].v = 1;
}
}
}
// 画像出力
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;
}
LyoKICAgIGh0dHA6Ly9pLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5lLmNvbS9mc2tWbDMKCiAgICBodHRwOi8vdC4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uaC5uZXQvdGVzdC9yZWFkLmNnaS90ZWNoLzEzNDk1Mjc3NTAvOTUxCiAgICAgPiBbMV0g5o6I5qWt5Y2Y5YWD77yaMuWApOWMlgogICAgID4gWzJdIOWVj+mhjOaWhyjlkKvjgrPjg7zjg4km44Oq44Oz44KvKe+8mgogICAgID4gaHR0cDovL2kuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmUuY29tL2Zza1ZsMwogICAgID4g55S75YOP6Kqt44G/6L6844G/4oaSMuWApOWMluKGkjLlgKTljJbjgZfjgZ/nlLvlg4/jgpLooajnpLrjgZnjgovjg5fjg63jgrDjg6njg6DjgpLkvZzmiJDjgZvjgogKICAgICA+IOeUu+WDj+iqreOBv+i+vOOBv+OBvuOBp+OBr+WHuuadpeOBvuOBl+OBn+OBjOOAgTLlgKTljJbjgYzjgojjgY/jgo/jgYvjgorjgb7jgZvjgpPjgafjgZfjgZ/jgIIKICAgICA+IOODleODq+OCq+ODqeODvOWJjeaPkOOBp1JHQuOBruWFqOODgeODo+ODs+ODjeODq+OBjOWQjOWApOOBquOCiea/g+a3oeeUu+WDj+OBq+WkieaPm+OBleOCjOOAgeOBneOBrua/g+a3oeeUu+WDj+OCkumWvuWApOWHp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AgICAg57eRCiAgICB1bnNpZ25lZCBjaGFyIGI7ICAgICAgICAgICAgLy8gICAgICAgICAg6Z2SCiAgICBpbnQgdjsgICAgICAgICAgICAgICAgICAgICAgLy8g5Ye65Yqb55S75YOPIDLlgKQgKDAgb3IgMSkKfSBDT0xPUjsKCmludCBtYWluKCkKewogICAgaW50IGksIGo7CiAgICB1bnNpZ25lZCBjaGFyIHNpa2lpY2hpID0gMHg4MDsgIC8vIOOBl+OBjeOBhOWApCjjgZPjgozku6XkuIrjgarjgonnmb3jgIHmnKrmuoDjgarjgonpu5IpCiAgICB1bnNpZ25lZCBjaGFyIGhlYWRbQk1QX0hFQURFUl9TSVpFXTsgICAgLy8gQk1Q44OY44OD44OACiAgICBzdGF0aWMgQ09MT1IgZGF0YVtXSURUSF1bSEVJR0hUXSwgKnBDb2xvcjsgIC8vIOODh+ODvOOCvwogICAgRklMRSAqZnA7ICAgICAgICAgICAgICAgICAgIC8vIOODleOCoeOCpOODqwoKICAgIC8vIGluaXQKICAgIG1lbXNldChkYXRhLCAweDAwLCBzaXplb2YoZGF0YSkpOwoKICAgIC8vIHJlYWQKICAgIGlmICgoZnAgPSBmb3BlbihJTlBVVF9JTUFHRV9GSUxFLCAicmIiKSkgPT0gTlVMTCkgewogICAgICAgIGZwcmludGYoc3RkZXJyLCAiZmlsZSBvcGVuIGVycm9yXG4iKTsKICAgICAgICBleGl0KDEpOwogICAgfQogICAgcmV3aW5kKGZwKTsKICAgIGZyZWFkKGhlYWQsIEJNUF9IRUFERVJfU0laRSwgMSwgZnApOwogICAgZm9yIChqID0gSEVJR0hUIC0gMTsgaiA+PSAwOyBqLS0pIHsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgV0lEVEg7IGkrKykgewogICAgICAgICAgICBwQ29sb3IgPSAmZGF0YVtpXVtqXTsKICAgICAgICAgICAgcENvbG9yLT5iID0gZ2V0YyhmcCk7CiAgICAgICAgICAgIHBDb2xvci0+ZyA9IGdldGMoZnApOwogICAgICAgICAgICBwQ29sb3ItPnIgPSBnZXRjKGZwKTsKICAgICAgICB9CiAgICB9CiAgICBmY2xvc2UoZnApOwoKICAgIC8vIDLlgKToqIjnrpcKICAgIGZvciAoaiA9IEhFSUdIVCAtIDE7IGogPj0gMDsgai0tKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IFdJRFRIOyBpKyspIHsKICAgICAgICAgICAgcENvbG9yID0gJmRhdGFbaV1bal07CiAgICAgICAgICAgIC8vIOKAuyBSR0LlgKTjgYzlkIzlgKTjgafjgarjgYTjg4njg4Pjg4jjga/jgIzpu5LjgI3jgajjgZnjgosKICAgICAgICAgICAgaWYgKChwQ29sb3ItPmIgPT0gcENvbG9yLT5nKSAmJiAocENvbG9yLT5nID09IHBDb2xvci0+cikgJiYgKHNpa2lpY2hpIDw9IHBDb2xvci0+cikpIHsKICAgICAgICAgICAgICAgIGRhdGFbaV1bal0udiA9IDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8g55S75YOP5Ye65YqbCiAgICBmcCA9IGZvcGVuKE9VVF9JTUFHRV9GSUxFLCAid2IiKTsKICAgIGZ3cml0ZShoZWFkLCBCTVBfSEVBREVSX1NJWkUsIDEsIGZwKTsKICAgIGZvciAoaiA9IEhFSUdIVCAtIDE7IGogPj0gMDsgai0tKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IFdJRFRIOyBpKyspIHsKICAgICAgICAgICAgaWYgKGRhdGFbaV1bal0udikgewogICAgICAgICAgICAgICAgcHV0YygweEZGLCBmcCk7CiAgICAgICAgICAgICAgICBwdXRjKDB4RkYsIGZwKTsKICAgICAgICAgICAgICAgIHB1dGMoMHhGRiwgZnApOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcHV0YygwLCBmcCk7CiAgICAgICAgICAgICAgICBwdXRjKDAsIGZwKTsKICAgICAgICAgICAgICAgIHB1dGMoMCwgZnApOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgZmNsb3NlKGZwKTsKICAgIHN5c3RlbShPVVRfSU1BR0VfQ09NTUFORCAiICIgT1VUX0lNQUdFX0ZJTEUpOwoKICAgIC8vIGVuZAogICAgcmV0dXJuIDA7Cn0=