fork download
  1. // mesh_3b.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define X_SIZE 32
  7. #define Y_SIZE 32
  8. #define X_BLOCK 4
  9. #define Y_BLOCK 4
  10. #define DIM ((Y_SIZE / Y_BLOCK) * (X_SIZE / X_BLOCK))
  11.  
  12. typedef unsigned char u_char;
  13.  
  14. void pattern(int n);
  15. double simple_similarity(int* fv_mesh_a, int* fv_mesh_b);
  16. void mesh(int* fv_mesh);
  17. void load_image_data(char* f_name);
  18.  
  19. u_char image[Y_SIZE][X_SIZE];
  20.  
  21. int main()
  22. {
  23. int i;
  24.  
  25. for (i = 0; i < 10; i++) {
  26. pattern(i);
  27. }
  28. return 0;
  29. }
  30.  
  31. void pattern(int n)
  32. {
  33. char f_name[256];
  34. int fv_mesh_a[DIM];
  35. int fv_mesh_b[DIM];
  36. int i, imax;
  37. double s, smax;
  38.  
  39. sprintf_s(f_name, _countof(f_name), "num%d.pgm", n);
  40. load_image_data(f_name);
  41. printf("\n%s\n", f_name);
  42. mesh(fv_mesh_a);
  43.  
  44. for (i = 0; i < 10; i++) {
  45. sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
  46. load_image_data(f_name);
  47. mesh(fv_mesh_b);
  48. s = simple_similarity(fv_mesh_a, fv_mesh_b);
  49. printf("%d %f\n", i, s);
  50. if (i == 0 || smax < s) {
  51. imax = i;
  52. smax = s;
  53. }
  54. }
  55. printf("識別結果:%d %f\n", imax, smax);
  56. }
  57.  
  58. double simple_similarity(int* fv_mesh_a, int* fv_mesh_b)
  59. {
  60. double s;
  61. int k;
  62.  
  63. s = 0.0;
  64. for (k = 0; k < DIM; k++) {
  65. s += 1.0 - abs(fv_mesh_a[k] - fv_mesh_b[k]) / 127.5;
  66. }
  67. return s;
  68. }
  69.  
  70. void mesh(int* fv_mesh)
  71. {
  72. int ix, iy;
  73. int k;
  74.  
  75. for (k = 0; k < DIM; k++) {
  76. fv_mesh[k] = 0;
  77. }
  78. for (iy = 0; iy < Y_SIZE; iy++) {
  79. for (ix = 0; ix < X_SIZE; ix++) {
  80. k = (iy / Y_BLOCK) * (X_SIZE / X_BLOCK) + (ix / X_BLOCK);
  81. fv_mesh[k] += image[iy][ix];
  82. }
  83. }
  84. for (k = 0; k < DIM; k++) {
  85. fv_mesh[k] /= X_BLOCK * Y_BLOCK;
  86. }
  87.  
  88. for (iy = 0; iy < Y_SIZE / Y_BLOCK; iy++) {
  89. for (ix = 0; ix < X_SIZE / X_BLOCK; ix++) {
  90. k = iy * (X_SIZE / X_BLOCK) + ix;
  91. }
  92. }
  93. }
  94.  
  95. void load_image_data(char* f_name)
  96. {
  97. FILE* fp;
  98. char buf[640];
  99. errno_t err;
  100. int x_size, y_size;
  101. int max_gray;
  102. int ix, iy;
  103.  
  104. err = fopen_s(&fp, f_name, "rb");
  105. if (err) {
  106. fprintf(stderr, "fopen_s\n");
  107. return;
  108. }
  109.  
  110. // P5
  111. fgets(buf, sizeof buf, fp);
  112. if (buf[0] != 'P' || buf[1] != '5') {
  113. fprintf(stderr, "not P5\n");
  114. return;
  115. }
  116.  
  117. // x y
  118. x_size = y_size = 0;
  119. while (x_size == 0) {
  120. fgets(buf, sizeof buf, fp);
  121. if (buf[0] == '#') continue;
  122. sscanf_s(buf, "%d%d", &x_size, &y_size);
  123. }
  124.  
  125. // max
  126. max_gray = 0;
  127. while (max_gray == 0) {
  128. fgets(buf, sizeof buf, fp);
  129. if (buf[0] == '#') continue;
  130. sscanf_s(buf, "%d", &max_gray);
  131. }
  132.  
  133. for (iy = 0; iy < y_size; iy++) {
  134. for (ix = 0; ix < x_size; ix++) {
  135. image[iy][ix] = fgetc(fp);
  136. }
  137. }
  138.  
  139. fclose(fp);
  140. }
  141.  
  142. /*
  143. num0.pgm
  144. 0 61.168627
  145. 1 34.188235
  146. 2 38.603922
  147. 3 43.003922
  148. 4 32.376471
  149. 5 44.737255
  150. 6 50.368627
  151. 7 34.839216
  152. 8 42.572549
  153. 9 51.419608
  154. 識別結果:0 61.168627
  155.  
  156. num1.pgm
  157. 0 34.376471
  158. 1 61.529412
  159. 2 38.996078
  160. 3 37.043137
  161. 4 40.580392
  162. 5 37.223529
  163. 6 35.513725
  164. 7 46.023529
  165. 8 33.898039
  166. 9 35.498039
  167. 識別結果:1 61.529412
  168.  
  169. num2.pgm
  170. 0 37.137255
  171. 1 37.200000
  172. 2 60.188235
  173. 3 41.247059
  174. 4 37.976471
  175. 5 36.376471
  176. 6 39.952941
  177. 7 37.976471
  178. 8 42.494118
  179. 9 38.792157
  180. 識別結果:2 60.188235
  181.  
  182. num3.pgm
  183. 0 41.294118
  184. 1 34.972549
  185. 2 40.643137
  186. 3 60.211765
  187. 4 42.039216
  188. 5 50.713725
  189. 6 46.321569
  190. 7 35.749020
  191. 8 48.141176
  192. 9 41.443137
  193. 識別結果:3 60.211765
  194.  
  195. num4.pgm
  196. 0 32.494118
  197. 1 41.701961
  198. 2 37.898039
  199. 3 42.784314
  200. 4 60.031373
  201. 5 38.650980
  202. 6 38.603922
  203. 7 36.345098
  204. 8 36.674510
  205. 9 32.486275
  206. 識別結果:4 60.031373
  207.  
  208. num5.pgm
  209. 0 41.984314
  210. 1 34.501961
  211. 2 34.901961
  212. 3 50.000000
  213. 4 37.223529
  214. 5 59.074510
  215. 6 45.098039
  216. 7 33.396078
  217. 8 44.015686
  218. 9 42.619608
  219. 識別結果:5 59.074510
  220.  
  221. num6.pgm
  222. 0 49.231373
  223. 1 33.482353
  224. 2 40.266667
  225. 3 47.207843
  226. 4 37.662745
  227. 5 46.431373
  228. 6 59.623529
  229. 7 34.556863
  230. 8 49.976471
  231. 9 43.152941
  232. 識別結果:6 59.623529
  233.  
  234. num7.pgm
  235. 0 31.411765
  236. 1 43.458824
  237. 2 36.768627
  238. 3 34.909804
  239. 4 33.772549
  240. 5 32.988235
  241. 6 31.952941
  242. 7 59.686275
  243. 8 31.341176
  244. 9 31.670588
  245. 識別結果:7 59.686275
  246.  
  247. num8.pgm
  248. 0 41.443137
  249. 1 31.780392
  250. 2 43.788235
  251. 3 49.694118
  252. 4 37.011765
  253. 5 44.305882
  254. 6 48.823529
  255. 7 32.886275
  256. 8 59.411765
  257. 9 42.062745
  258. 識別結果:8 59.411765
  259.  
  260. num9.pgm
  261. 0 51.521569
  262. 1 34.094118
  263. 2 39.905882
  264. 3 42.360784
  265. 4 31.231373
  266. 5 44.266667
  267. 6 42.917647
  268. 7 34.054902
  269. 8 43.043137
  270. 9 60.125490
  271. 識別結果:9 60.125490
  272. */
  273.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty