// mesh_3b.c
#include <stdio.h>
#include <stdlib.h>
#define X_SIZE 32
#define Y_SIZE 32
#define X_BLOCK 4
#define Y_BLOCK 4
#define DIM ((Y_SIZE / Y_BLOCK) * (X_SIZE / X_BLOCK))
typedef unsigned char u_char;
void pattern(int n);
double simple_similarity(int* fv_mesh_a, int* fv_mesh_b);
void mesh(int* fv_mesh);
void load_image_data(char* f_name);
u_char image[Y_SIZE][X_SIZE];
int main()
{
int i;
for (i = 0; i < 10; i++) {
pattern(i);
}
return 0;
}
void pattern(int n)
{
char f_name[256];
int fv_mesh_a[DIM];
int fv_mesh_b[DIM];
int i, imax;
double s, smax;
sprintf_s(f_name, _countof(f_name), "num%d.pgm", n);
load_image_data(f_name);
mesh(fv_mesh_a);
for (i = 0; i < 10; i++) {
sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
load_image_data(f_name);
mesh(fv_mesh_b);
s = simple_similarity(fv_mesh_a, fv_mesh_b);
if (i == 0 || smax < s) {
imax = i;
smax = s;
}
}
printf("識別結果:%d %f\n", imax
, smax
); }
double simple_similarity(int* fv_mesh_a, int* fv_mesh_b)
{
double s;
int k;
s = 0.0;
for (k = 0; k < DIM; k++) {
s
+= 1.0 - abs(fv_mesh_a
[k
] - fv_mesh_b
[k
]) / 127.5; }
return s;
}
void mesh(int* fv_mesh)
{
int ix, iy;
int k;
for (k = 0; k < DIM; k++) {
fv_mesh[k] = 0;
}
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
k = (iy / Y_BLOCK) * (X_SIZE / X_BLOCK) + (ix / X_BLOCK);
fv_mesh[k] += image[iy][ix];
}
}
for (k = 0; k < DIM; k++) {
fv_mesh[k] /= X_BLOCK * Y_BLOCK;
}
for (iy = 0; iy < Y_SIZE / Y_BLOCK; iy++) {
for (ix = 0; ix < X_SIZE / X_BLOCK; ix++) {
k = iy * (X_SIZE / X_BLOCK) + ix;
}
}
}
void load_image_data(char* f_name)
{
FILE* fp;
char buf[640];
errno_t err;
int x_size, y_size;
int max_gray;
int ix, iy;
err = fopen_s(&fp, f_name, "rb");
if (err) {
return;
}
// P5
fgets(buf
, sizeof buf
, fp
); if (buf[0] != 'P' || buf[1] != '5') {
return;
}
// x y
x_size = y_size = 0;
while (x_size == 0) {
fgets(buf
, sizeof buf
, fp
); if (buf[0] == '#') continue;
sscanf_s(buf, "%d%d", &x_size, &y_size);
}
// max
max_gray = 0;
while (max_gray == 0) {
fgets(buf
, sizeof buf
, fp
); if (buf[0] == '#') continue;
sscanf_s(buf, "%d", &max_gray);
}
for (iy = 0; iy < y_size; iy++) {
for (ix = 0; ix < x_size; ix++) {
image
[iy
][ix
] = fgetc(fp
); }
}
}
/*
num0.pgm
0 61.168627
1 34.188235
2 38.603922
3 43.003922
4 32.376471
5 44.737255
6 50.368627
7 34.839216
8 42.572549
9 51.419608
識別結果:0 61.168627
num1.pgm
0 34.376471
1 61.529412
2 38.996078
3 37.043137
4 40.580392
5 37.223529
6 35.513725
7 46.023529
8 33.898039
9 35.498039
識別結果:1 61.529412
num2.pgm
0 37.137255
1 37.200000
2 60.188235
3 41.247059
4 37.976471
5 36.376471
6 39.952941
7 37.976471
8 42.494118
9 38.792157
識別結果:2 60.188235
num3.pgm
0 41.294118
1 34.972549
2 40.643137
3 60.211765
4 42.039216
5 50.713725
6 46.321569
7 35.749020
8 48.141176
9 41.443137
識別結果:3 60.211765
num4.pgm
0 32.494118
1 41.701961
2 37.898039
3 42.784314
4 60.031373
5 38.650980
6 38.603922
7 36.345098
8 36.674510
9 32.486275
識別結果:4 60.031373
num5.pgm
0 41.984314
1 34.501961
2 34.901961
3 50.000000
4 37.223529
5 59.074510
6 45.098039
7 33.396078
8 44.015686
9 42.619608
識別結果:5 59.074510
num6.pgm
0 49.231373
1 33.482353
2 40.266667
3 47.207843
4 37.662745
5 46.431373
6 59.623529
7 34.556863
8 49.976471
9 43.152941
識別結果:6 59.623529
num7.pgm
0 31.411765
1 43.458824
2 36.768627
3 34.909804
4 33.772549
5 32.988235
6 31.952941
7 59.686275
8 31.341176
9 31.670588
識別結果:7 59.686275
num8.pgm
0 41.443137
1 31.780392
2 43.788235
3 49.694118
4 37.011765
5 44.305882
6 48.823529
7 32.886275
8 59.411765
9 42.062745
識別結果:8 59.411765
num9.pgm
0 51.521569
1 34.094118
2 39.905882
3 42.360784
4 31.231373
5 44.266667
6 42.917647
7 34.054902
8 43.043137
9 60.125490
識別結果:9 60.125490
*/
Ly8gbWVzaF8zYi5jCgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKI2RlZmluZSBYX1NJWkUJMzIKI2RlZmluZSBZX1NJWkUJMzIKI2RlZmluZSBYX0JMT0NLCTQKI2RlZmluZSBZX0JMT0NLCTQKI2RlZmluZSBESU0JKChZX1NJWkUgLyBZX0JMT0NLKSAqIChYX1NJWkUgLyBYX0JMT0NLKSkKCnR5cGVkZWYgdW5zaWduZWQgY2hhciB1X2NoYXI7Cgp2b2lkIHBhdHRlcm4oaW50IG4pOwpkb3VibGUgc2ltcGxlX3NpbWlsYXJpdHkoaW50KiBmdl9tZXNoX2EsIGludCogZnZfbWVzaF9iKTsKdm9pZCBtZXNoKGludCogZnZfbWVzaCk7CnZvaWQgbG9hZF9pbWFnZV9kYXRhKGNoYXIqIGZfbmFtZSk7Cgp1X2NoYXIJaW1hZ2VbWV9TSVpFXVtYX1NJWkVdOwoKaW50IG1haW4oKQp7CglpbnQJaTsKCglmb3IgKGkgPSAwOyBpIDwgMTA7IGkrKykgewoJCXBhdHRlcm4oaSk7Cgl9CglyZXR1cm4gMDsKfQoKdm9pZCBwYXR0ZXJuKGludCBuKQp7CgljaGFyCWZfbmFtZVsyNTZdOwoJaW50CWZ2X21lc2hfYVtESU1dOwoJaW50CWZ2X21lc2hfYltESU1dOwoJaW50CWksIGltYXg7Cglkb3VibGUJcywgc21heDsKCglzcHJpbnRmX3MoZl9uYW1lLCBfY291bnRvZihmX25hbWUpLCAibnVtJWQucGd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