[772] | 1 | // ---------------------- |
---|
| 2 | // --- ecc_features.c --- |
---|
| 3 | // ---------------------- |
---|
| 4 | |
---|
| 5 | /* |
---|
| 6 | * Copyright (c) 2012 - 2014, Lionel Lacassagne, All rights reserved |
---|
| 7 | * University of Paris Sud, Laboratoire de Recherche en Informatique |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | // Caracteristiques d'une region / label |
---|
| 11 | |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <stddef.h> |
---|
| 14 | #include <stdlib.h> |
---|
| 15 | #include <malloc.h> |
---|
[821] | 16 | #include <string.h> |
---|
[772] | 17 | |
---|
| 18 | |
---|
| 19 | #include "nrc_os_config.h" |
---|
[821] | 20 | #include "config.h" |
---|
[772] | 21 | #include "nrc.h" |
---|
| 22 | |
---|
[798] | 23 | |
---|
| 24 | #if TARGET_OS == LINUX |
---|
| 25 | #include <sys/types.h> |
---|
| 26 | #include <sys/stat.h> |
---|
| 27 | #include <fcntl.h> |
---|
[821] | 28 | #include <unistd.h> |
---|
[798] | 29 | #endif |
---|
| 30 | |
---|
| 31 | |
---|
[772] | 32 | #include "ecc_features.h" |
---|
| 33 | |
---|
[821] | 34 | #define BUFF_SIZE 1024 // for snprintf |
---|
| 35 | |
---|
| 36 | |
---|
[822] | 37 | // ----------------------------------------------------- |
---|
| 38 | void RegionStats_Init(RegionStats * Stats, uint32 nemax) |
---|
| 39 | // ----------------------------------------------------- |
---|
| 40 | { |
---|
| 41 | for (int i = 0; i < (int) nemax; i++) { |
---|
| 42 | #if PARMERGE |
---|
| 43 | pthread_spin_init(&Stats[i].lock, PTHREAD_PROCESS_PRIVATE); |
---|
| 44 | #endif |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
[821] | 49 | // ------------------------------------------------------------- |
---|
| 50 | void RegionStats_Constructor(RegionStats ** Stats, uint32 nemax) |
---|
| 51 | // ------------------------------------------------------------- |
---|
[772] | 52 | { |
---|
| 53 | *Stats = RegionStats_pConstructor(nemax); |
---|
| 54 | } |
---|
[821] | 55 | |
---|
| 56 | |
---|
| 57 | // ------------------------------------------------- |
---|
| 58 | RegionStats * RegionStats_pConstructor(uint32 nemax) |
---|
| 59 | // ------------------------------------------------- |
---|
[772] | 60 | { |
---|
[821] | 61 | RegionStats * Stats; |
---|
[772] | 62 | |
---|
[821] | 63 | Stats = (RegionStats *) malloc((nemax) * sizeof(RegionStats)); |
---|
| 64 | if (Stats == NULL) { |
---|
| 65 | nrerror("allocation failed in RegionStats_pConstructor"); |
---|
| 66 | } |
---|
[772] | 67 | |
---|
[822] | 68 | RegionStats_Init(Stats, nemax); |
---|
[772] | 69 | |
---|
| 70 | return Stats; |
---|
| 71 | } |
---|
[821] | 72 | |
---|
| 73 | |
---|
| 74 | // ------------------------------------------------------------ |
---|
| 75 | void RegionStats_Destructor(RegionStats ** Stats, uint32 nemax) |
---|
| 76 | // ------------------------------------------------------------ |
---|
[772] | 77 | { |
---|
| 78 | RegionStats_pDestructor(*Stats, nemax); |
---|
| 79 | } |
---|
[821] | 80 | |
---|
| 81 | |
---|
| 82 | // ------------------------------------------------------------ |
---|
| 83 | void RegionStats_pDestructor(RegionStats * Stats, uint32 nemax) |
---|
| 84 | // ------------------------------------------------------------ |
---|
[772] | 85 | { |
---|
[822] | 86 | //RegionStats_Clear(Stats, nemax); |
---|
[772] | 87 | free(Stats); |
---|
| 88 | } |
---|
[821] | 89 | |
---|
| 90 | // ------------------------------------------------------ |
---|
| 91 | void RegionStats_Clear(RegionStats * Stats, uint32 nemax) |
---|
| 92 | // ------------------------------------------------------ |
---|
[772] | 93 | { |
---|
[821] | 94 | for (int i = 0; i < (int) nemax; i++) { |
---|
[822] | 95 | #if FEATURES |
---|
[772] | 96 | Stats[i].xmin = 65535; |
---|
| 97 | Stats[i].xmax = 0; |
---|
| 98 | Stats[i].ymin = 65535; |
---|
| 99 | Stats[i].ymax = 0; |
---|
| 100 | |
---|
| 101 | Stats[i].S = 0; |
---|
| 102 | |
---|
| 103 | Stats[i].Sx = 0; |
---|
| 104 | Stats[i].Sy = 0; |
---|
| 105 | #endif |
---|
| 106 | } |
---|
| 107 | } |
---|
[821] | 108 | |
---|
[822] | 109 | #if FEATURES |
---|
[821] | 110 | // ----------------------------------------- |
---|
| 111 | void RegionStats_Clear1(RegionStats * stats) |
---|
| 112 | // ----------------------------------------- |
---|
[772] | 113 | { |
---|
| 114 | stats->xmin = 0; |
---|
| 115 | stats->xmax = 0; |
---|
| 116 | stats->ymin = 0; |
---|
| 117 | stats->ymax = 0; |
---|
| 118 | |
---|
| 119 | stats->S = 0; |
---|
| 120 | |
---|
| 121 | stats->Sx = 0; |
---|
| 122 | stats->Sy = 0; |
---|
| 123 | } |
---|
[821] | 124 | |
---|
| 125 | |
---|
| 126 | // ----------------------------------------- |
---|
| 127 | int RegionStats_Create_File(char * filename) |
---|
| 128 | // ----------------------------------------- |
---|
[772] | 129 | { |
---|
| 130 | int fd; |
---|
| 131 | |
---|
| 132 | fd = open(filename, O_CREAT | O_TRUNC); |
---|
| 133 | if (fd < 0) { |
---|
| 134 | printf("RegionStats_Open_File : can't create file %s\n", filename); |
---|
[821] | 135 | exit(1); |
---|
[772] | 136 | } |
---|
| 137 | return fd; |
---|
| 138 | } |
---|
[821] | 139 | |
---|
| 140 | |
---|
| 141 | // --------------------------------------- |
---|
| 142 | int RegionStats_Open_File(char * filename) |
---|
| 143 | // --------------------------------------- |
---|
[772] | 144 | { |
---|
| 145 | int fd; |
---|
| 146 | |
---|
| 147 | fd = open(filename, O_RDONLY); |
---|
| 148 | if (fd < 0) { |
---|
| 149 | printf("RegionStats_Open_File : can't open file %s\n", filename); |
---|
[821] | 150 | exit(1); |
---|
[772] | 151 | } |
---|
| 152 | return fd; |
---|
| 153 | } |
---|
[821] | 154 | |
---|
| 155 | |
---|
| 156 | // -------------------------------- |
---|
[772] | 157 | void RegionStats_Close_File(int fd) |
---|
[821] | 158 | // -------------------------------- |
---|
[772] | 159 | { |
---|
| 160 | close(fd); |
---|
| 161 | } |
---|
[821] | 162 | |
---|
| 163 | |
---|
| 164 | #if 0 // pb : fscanf requires manipulating FILE * |
---|
| 165 | // -------------------------------- |
---|
[772] | 166 | int RegionStats_Read_Header(int fd) |
---|
[821] | 167 | // -------------------------------- |
---|
[772] | 168 | { |
---|
[798] | 169 | int ne = 0; |
---|
[772] | 170 | fscanf(fd, "%d", &ne); |
---|
| 171 | return ne; |
---|
| 172 | } |
---|
[821] | 173 | #endif |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | // ------------------------------------------ |
---|
[772] | 177 | void RegionStats_Write_Header(int ne, int fd) |
---|
[821] | 178 | // ------------------------------------------ |
---|
[772] | 179 | { |
---|
[821] | 180 | char buff[BUFF_SIZE]; |
---|
| 181 | snprintf(buff, BUFF_SIZE, "%d\n", ne); |
---|
| 182 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 183 | } |
---|
[821] | 184 | |
---|
| 185 | |
---|
| 186 | #if 0 |
---|
[772] | 187 | // -------------------------------------------------------------- |
---|
[821] | 188 | void RegionStats_Read_Stats1(int fd, int ne, RegionStats * Stats) |
---|
[772] | 189 | // -------------------------------------------------------------- |
---|
| 190 | { |
---|
[798] | 191 | int i; |
---|
[772] | 192 | |
---|
| 193 | for (i = 1; i <= ne; i++) { |
---|
| 194 | fscanf(fd, "%d%d%d%d%d%d%d%d\n", |
---|
| 195 | &t, |
---|
| 196 | &(Stats[i].xmin), |
---|
| 197 | &(Stats[i].xmax), |
---|
| 198 | &(Stats[i].ymin), |
---|
| 199 | &(Stats[i].ymax), |
---|
| 200 | &(Stats[i].S), |
---|
| 201 | &(Stats[i].Sx), |
---|
| 202 | &(Stats[i].Sy)); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | #endif |
---|
[821] | 206 | |
---|
| 207 | |
---|
[772] | 208 | // --------------------------------------------------------------- |
---|
[821] | 209 | void RegionStats_Write_Stats1(RegionStats * Stats, int ne, int fd) |
---|
[772] | 210 | // --------------------------------------------------------------- |
---|
| 211 | { |
---|
[821] | 212 | char buff[BUFF_SIZE]; |
---|
[772] | 213 | |
---|
[821] | 214 | for (int i = 1; i <= ne; i++) { |
---|
| 215 | snprintf(buff, BUFF_SIZE, "%4d %5d %5d %5d %5d %7d %8d %8d\n", |
---|
[772] | 216 | i, |
---|
| 217 | Stats[i].xmin, |
---|
| 218 | Stats[i].xmax, |
---|
| 219 | Stats[i].ymin, |
---|
| 220 | Stats[i].ymax, |
---|
| 221 | |
---|
| 222 | Stats[i].S, |
---|
| 223 | Stats[i].Sx, |
---|
| 224 | Stats[i].Sy); |
---|
[821] | 225 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 226 | } |
---|
| 227 | } |
---|
[821] | 228 | |
---|
| 229 | |
---|
| 230 | // --------------------------------------------------------------------------------------------------- |
---|
| 231 | void RegionStats_Write_Stats1_Sparse(RegionStats * Stats, uint32 * EQ, uint32 ne0, uint32 ne1, int fd) |
---|
| 232 | // --------------------------------------------------------------------------------------------------- |
---|
[772] | 233 | { |
---|
| 234 | uint32 e; |
---|
[821] | 235 | char buff[BUFF_SIZE]; |
---|
[772] | 236 | |
---|
| 237 | for (e = ne0; e <= ne1; e++) { |
---|
| 238 | if ((e == EQ[e]) && (Stats[e].S > 0)) { |
---|
[821] | 239 | snprintf(buff, BUFF_SIZE, "%4d %5d %5d %5d %5d %7d %8d %8d\n", |
---|
[772] | 240 | e, |
---|
| 241 | Stats[e].xmin, |
---|
| 242 | Stats[e].xmax, |
---|
| 243 | Stats[e].ymin, |
---|
| 244 | Stats[e].ymax, |
---|
| 245 | |
---|
| 246 | Stats[e].S, |
---|
| 247 | Stats[e].Sx, |
---|
| 248 | Stats[e].Sy); |
---|
[821] | 249 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 250 | } |
---|
| 251 | } |
---|
| 252 | } |
---|
[821] | 253 | |
---|
| 254 | |
---|
[772] | 255 | // ----------------------------------------------------------------- |
---|
[821] | 256 | void RegionStats_Write_pStats1(RegionStats ** Stats, int ne, int fd) |
---|
[772] | 257 | // ----------------------------------------------------------------- |
---|
| 258 | { |
---|
[821] | 259 | char buff[BUFF_SIZE]; |
---|
[772] | 260 | |
---|
[821] | 261 | for (int i = 1; i <= ne; i++) { |
---|
| 262 | snprintf(buff, BUFF_SIZE, "%4d %5d %5d %5d %5d %7d %8d %8d\n", |
---|
[772] | 263 | i, |
---|
| 264 | Stats[i]->xmin, |
---|
| 265 | Stats[i]->xmax, |
---|
| 266 | Stats[i]->ymin, |
---|
| 267 | Stats[i]->ymax, |
---|
| 268 | Stats[i]->S, |
---|
| 269 | Stats[i]->Sx, |
---|
| 270 | Stats[i]->Sy); |
---|
[821] | 271 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 272 | } |
---|
| 273 | } |
---|
[821] | 274 | |
---|
| 275 | |
---|
| 276 | #if 0 |
---|
| 277 | // -------------------------------------------------------------------------- |
---|
| 278 | void RegionStats_Load_Stats1(char * filename, int * ne, RegionStats ** Stats) |
---|
| 279 | // -------------------------------------------------------------------------- |
---|
[772] | 280 | { |
---|
| 281 | int fd; |
---|
| 282 | |
---|
| 283 | fd = RegionStats_Open_File(filename); |
---|
| 284 | |
---|
| 285 | *ne = RegionStats_Read_Header(fd); |
---|
| 286 | |
---|
| 287 | RegionStats_Constructor(Stats, *ne); |
---|
| 288 | RegionStats_Read_Stats1(fd, *ne, *Stats); |
---|
| 289 | RegionStats_Close_File(fd); |
---|
| 290 | } |
---|
[821] | 291 | |
---|
| 292 | |
---|
| 293 | // -------------------------------------------------------------------------- |
---|
| 294 | void RegionStats_MLoad_Stats1(char * filename, int * ne, RegionStats * Stats) |
---|
| 295 | // -------------------------------------------------------------------------- |
---|
[772] | 296 | { |
---|
| 297 | int fd; |
---|
| 298 | |
---|
| 299 | fd = RegionStats_Open_File(filename); |
---|
| 300 | |
---|
| 301 | *ne = RegionStats_Read_Header(fd); |
---|
| 302 | RegionStats_Read_Stats1(fd, *ne, Stats); |
---|
| 303 | RegionStats_Close_File(fd); |
---|
| 304 | } |
---|
[821] | 305 | #endif |
---|
| 306 | |
---|
[772] | 307 | // ----------------------------------------------------------------------- |
---|
[821] | 308 | void RegionStats_Save_Stats1(RegionStats * Stats, int ne, char * filename) |
---|
[772] | 309 | // ----------------------------------------------------------------------- |
---|
| 310 | { |
---|
| 311 | int fd; |
---|
| 312 | |
---|
| 313 | fd = RegionStats_Create_File(filename); |
---|
| 314 | |
---|
| 315 | RegionStats_Write_Header(ne, fd); |
---|
| 316 | RegionStats_Write_Stats1(Stats, ne, fd); |
---|
| 317 | RegionStats_Close_File(fd); |
---|
| 318 | } |
---|
[821] | 319 | |
---|
| 320 | |
---|
| 321 | // ------------------------------------------------------------------------- |
---|
| 322 | void RegionStats_Save_pStats1(RegionStats ** Stats, int ne, char * filename) |
---|
| 323 | // ------------------------------------------------------------------------- |
---|
[772] | 324 | { |
---|
| 325 | int fd; |
---|
| 326 | |
---|
| 327 | fd = RegionStats_Create_File(filename); |
---|
| 328 | |
---|
| 329 | RegionStats_Write_Header(ne, fd); |
---|
| 330 | RegionStats_Write_pStats1(Stats, ne, fd); |
---|
| 331 | RegionStats_Close_File(fd); |
---|
| 332 | } |
---|
[821] | 333 | |
---|
| 334 | |
---|
| 335 | // ---------------------------------------------------------------------- |
---|
| 336 | void RegionStats_Display_Stats1(RegionStats * Stats, int ne, char * name) |
---|
| 337 | // ---------------------------------------------------------------------- |
---|
[772] | 338 | { |
---|
| 339 | if (name != NULL) { |
---|
| 340 | printf("%s : %d\n", name, ne); |
---|
| 341 | } |
---|
| 342 | else { |
---|
| 343 | printf("RegionStats : %d\n", ne); |
---|
| 344 | } |
---|
[821] | 345 | for (int i = 1; i <= ne; i++) { |
---|
[772] | 346 | printf("#%3d: %4d %4d %4d %4d %6d %8d %8d\n", |
---|
| 347 | i, |
---|
| 348 | Stats[i].xmin, |
---|
| 349 | Stats[i].xmax, |
---|
| 350 | Stats[i].ymin, |
---|
| 351 | Stats[i].ymax, |
---|
| 352 | Stats[i].S, |
---|
| 353 | Stats[i].Sx, |
---|
| 354 | Stats[i].Sy); |
---|
| 355 | } |
---|
| 356 | } |
---|
[821] | 357 | |
---|
| 358 | |
---|
| 359 | // ------------------------------------------------------------------------ |
---|
| 360 | void RegionStats_Display_pStats1(RegionStats ** Stats, int ne, char * name) |
---|
| 361 | // ------------------------------------------------------------------------ |
---|
[772] | 362 | { |
---|
| 363 | if (name != NULL) { |
---|
| 364 | printf("%s : %d\n", name, ne); |
---|
| 365 | } |
---|
| 366 | else { |
---|
| 367 | printf("RegionStats : %d\n", ne); |
---|
| 368 | } |
---|
[821] | 369 | for (int i = 1; i <= ne; i++) { |
---|
[772] | 370 | printf("#%3d: %4d %4d %4d %4d %6d %8d %8d\n", |
---|
| 371 | i, |
---|
| 372 | Stats[i]->xmin, |
---|
| 373 | Stats[i]->xmax, |
---|
| 374 | Stats[i]->ymin, |
---|
| 375 | Stats[i]->ymax, |
---|
| 376 | Stats[i]->S, |
---|
| 377 | Stats[i]->Sx, |
---|
| 378 | Stats[i]->Sy); |
---|
| 379 | } |
---|
| 380 | } |
---|
[821] | 381 | |
---|
| 382 | |
---|
| 383 | // ---------------------------------------------------------------------------------------------- |
---|
| 384 | void RegionStats_SetRectangle(RegionStats * Stats, int e, int ymin, int ymax, int xmin, int xmax) |
---|
| 385 | // ---------------------------------------------------------------------------------------------- |
---|
[772] | 386 | { |
---|
| 387 | Stats[e].ymin = ymin; |
---|
| 388 | Stats[e].xmin = xmin; |
---|
| 389 | Stats[e].ymax = ymax; |
---|
| 390 | Stats[e].xmax = xmax; |
---|
| 391 | } |
---|
[821] | 392 | |
---|
| 393 | |
---|
| 394 | // --------------------------------------------------------- |
---|
| 395 | void RegionStats_Copy1(RegionStats * src, RegionStats * dst) |
---|
| 396 | // --------------------------------------------------------- |
---|
[772] | 397 | { |
---|
| 398 | dst->xmin = src->xmin; |
---|
| 399 | dst->xmax = src->xmax; |
---|
| 400 | dst->ymin = src->ymin; |
---|
| 401 | dst->ymax = src->ymax; |
---|
| 402 | |
---|
| 403 | dst->S = src->S; |
---|
| 404 | |
---|
| 405 | dst->Sx = src->Sx; |
---|
| 406 | dst->Sy = src->Sy; |
---|
| 407 | } |
---|
[822] | 408 | #endif |
---|
[821] | 409 | |
---|
[772] | 410 | // =============================== |
---|
| 411 | // === nouvelles versions 2009 === |
---|
| 412 | // =============================== |
---|
| 413 | |
---|
[821] | 414 | // -------------------------------------------- |
---|
| 415 | RegionStats * RegionStatsVector(int i0, int i1) |
---|
| 416 | // -------------------------------------------- |
---|
[772] | 417 | // allocate a float vector with subscript range v[i0..i1] |
---|
| 418 | { |
---|
| 419 | RegionStats * v; |
---|
| 420 | |
---|
| 421 | v = (RegionStats *) malloc((size_t) ((i1 - i0 + 1 + NR_END) * sizeof(RegionStats))); |
---|
[821] | 422 | if (!v) { |
---|
| 423 | nrerror("allocation failure in %s()", __func__); |
---|
| 424 | return NULL; |
---|
| 425 | } |
---|
[822] | 426 | RegionStats_Init(v, i1 - i0 + 1 + NR_END); |
---|
[772] | 427 | return v - i0 + NR_END; |
---|
| 428 | } |
---|
[821] | 429 | |
---|
| 430 | |
---|
| 431 | #if TARGET_OS == GIETVM |
---|
| 432 | // ----------------------------------------------------------------- |
---|
| 433 | RegionStats * remote_RegionStatsVector(int i0, int i1, int x, int y) |
---|
| 434 | // ----------------------------------------------------------------- |
---|
[772] | 435 | // allocate a float vector with subscript range v[i0..i1] |
---|
| 436 | { |
---|
[821] | 437 | RegionStats * v; |
---|
[772] | 438 | |
---|
[821] | 439 | v = (RegionStats *) remote_malloc((size_t) ((i1 - i0 + 1 + NR_END) * sizeof(RegionStats)), x, y); |
---|
| 440 | if (!v) { |
---|
| 441 | nrerror("allocation failure in %s()", __func__); |
---|
| 442 | return NULL; |
---|
| 443 | } |
---|
[822] | 444 | RegionStats_Init(v, i1 - i0 + 1 + NR_END); |
---|
[821] | 445 | return v - i0 + NR_END; |
---|
| 446 | } |
---|
| 447 | #endif |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | // --------------------------------------------- |
---|
| 451 | RegionStats * RegionStatsVector0(int i0, int i1) |
---|
| 452 | // --------------------------------------------- |
---|
| 453 | // allocate a float vector with subscript range v[i0..i1] |
---|
| 454 | { |
---|
| 455 | RegionStats * v; |
---|
| 456 | |
---|
[772] | 457 | v = (RegionStats *) calloc((size_t) (i1 - i0 + 1 + NR_END), sizeof(RegionStats)); |
---|
[821] | 458 | if (!v) { |
---|
| 459 | nrerror("allocation failure in RegionStatsVector0()"); |
---|
| 460 | return NULL; |
---|
| 461 | } |
---|
[772] | 462 | return v - i0 + NR_END; |
---|
| 463 | } |
---|
[821] | 464 | |
---|
| 465 | |
---|
| 466 | // --------------------------------------------------------- |
---|
| 467 | void free_RegionStatsVector(RegionStats * v, int i0, int i1) |
---|
| 468 | // --------------------------------------------------------- |
---|
[772] | 469 | // free a RegionStats vector allocated with vector() |
---|
| 470 | { |
---|
[821] | 471 | free(v + i0 - NR_END); |
---|
[772] | 472 | } |
---|
| 473 | |
---|
[821] | 474 | |
---|
| 475 | // ------------------------------------------------------------- |
---|
| 476 | RegionStats ** RegionStatsMatrix(int i0, int i1, int j0, int j1) |
---|
| 477 | // ------------------------------------------------------------- |
---|
| 478 | |
---|
[772] | 479 | // allocate a RegionStats matrix with subscript range m[nrl..nrh][ncl..nch] |
---|
| 480 | { |
---|
| 481 | long nrow = i1 - i0 + 1; |
---|
| 482 | long ncol = j1 - j0 + 1; |
---|
[821] | 483 | RegionStats ** m; |
---|
[772] | 484 | |
---|
| 485 | // allocate pointers to rows |
---|
| 486 | m = (RegionStats **) malloc((size_t) ((nrow + NR_END) * sizeof(RegionStats *))); |
---|
[821] | 487 | if (!m) { |
---|
| 488 | nrerror("allocation failure 1 in RegionStatsMatrix()"); |
---|
| 489 | } |
---|
[772] | 490 | m += NR_END; |
---|
| 491 | m -= i0; |
---|
| 492 | |
---|
| 493 | // allocate rows and set pointers to them |
---|
| 494 | m[i0] = (RegionStats *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof(RegionStats))); |
---|
[821] | 495 | if (!m[i0]) { |
---|
| 496 | nrerror("allocation failure 2 in RegionStatsMatrix()"); |
---|
| 497 | } |
---|
[772] | 498 | m[i0] += NR_END; |
---|
| 499 | m[i0] -= j0; |
---|
| 500 | |
---|
[821] | 501 | for (int i = i0 + 1; i <= i1; i++) { |
---|
[772] | 502 | m[i] = m[i - 1] + ncol; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | // return pointer to array of pointers to rows |
---|
| 506 | return m; |
---|
| 507 | } |
---|
| 508 | |
---|
[821] | 509 | |
---|
| 510 | // -------------------------------------------------------------- |
---|
| 511 | RegionStats ** RegionStatsMatrix0(int i0, int i1, int j0, int j1) |
---|
| 512 | // -------------------------------------------------------------- |
---|
[772] | 513 | // allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] |
---|
| 514 | { |
---|
[821] | 515 | long i; |
---|
| 516 | long nrow = i1 - i0 + 1; |
---|
| 517 | long ncol = j1 - j0 + 1; |
---|
| 518 | RegionStats ** m; |
---|
[772] | 519 | |
---|
| 520 | // allocate pointers to rows |
---|
[821] | 521 | m= (RegionStats **) malloc((size_t) ((nrow + NR_END) * sizeof(RegionStats*))); |
---|
| 522 | if (!m) { |
---|
| 523 | nrerror("allocation failure 1 in RegionStatsMatrix()"); |
---|
| 524 | } |
---|
[772] | 525 | m += NR_END; |
---|
| 526 | m -= i0; |
---|
| 527 | |
---|
| 528 | // allocate rows and set pointers to them |
---|
[821] | 529 | m[i0] = (RegionStats *) calloc((size_t) (nrow * ncol + NR_END), sizeof(RegionStats)); |
---|
| 530 | if (!m[i0]) { |
---|
| 531 | nrerror("allocation failure 2 in RegionStatsMatrix()"); |
---|
| 532 | } |
---|
[772] | 533 | m[i0] += NR_END; |
---|
| 534 | m[i0] -= j0; |
---|
| 535 | |
---|
[821] | 536 | for (i = i0 + 1; i <= i1; i++) { |
---|
| 537 | m[i] = m[i - 1] + ncol; |
---|
| 538 | } |
---|
[772] | 539 | |
---|
| 540 | // return pointer to array of pointers to rows |
---|
| 541 | return m; |
---|
| 542 | } |
---|
[821] | 543 | |
---|
| 544 | |
---|
| 545 | // -------------------------------------------------------------------------- |
---|
| 546 | void free_RegionStatsMatrix(RegionStats ** m, int i0, int i1, int j0, int j1) |
---|
| 547 | // -------------------------------------------------------------------------- |
---|
[772] | 548 | { |
---|
[821] | 549 | free(m[i0] + j0 - NR_END); |
---|
| 550 | free(m + i0 - NR_END); |
---|
[772] | 551 | } |
---|
[821] | 552 | |
---|
| 553 | |
---|
[822] | 554 | #if FEATURES |
---|
[821] | 555 | // ----------------------------------- |
---|
| 556 | void zero_RegionStats(RegionStats * x) |
---|
| 557 | // ----------------------------------- |
---|
[772] | 558 | { |
---|
| 559 | x->xmin = 32767; |
---|
| 560 | x->xmax = 0; |
---|
| 561 | x->ymin = 32767; |
---|
| 562 | x->ymax = 0; |
---|
| 563 | |
---|
| 564 | x->S = 0; |
---|
| 565 | x->Sx = 0; |
---|
| 566 | x->Sy = 0; |
---|
| 567 | } |
---|
[821] | 568 | |
---|
| 569 | |
---|
| 570 | // --------------------------------------------------------- |
---|
| 571 | void zero_RegionStatsVector(RegionStats * v, int i0, int i1) |
---|
| 572 | // --------------------------------------------------------- |
---|
[772] | 573 | { |
---|
[821] | 574 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 575 | zero_RegionStats(&v[i]); |
---|
| 576 | } |
---|
| 577 | } |
---|
[821] | 578 | |
---|
| 579 | |
---|
| 580 | // -------------------------------------------------------------------------- |
---|
| 581 | void zero_RegionStatsMatrix(RegionStats ** m, int i0, int i1, int j0, int j1) |
---|
| 582 | // -------------------------------------------------------------------------- |
---|
[772] | 583 | { |
---|
[821] | 584 | for (int i = i0; i <= i1; i++) { |
---|
| 585 | for (int j = j0; j <= j1; j++) { |
---|
[772] | 586 | zero_RegionStats(&(m[i][j])); |
---|
| 587 | } |
---|
| 588 | } |
---|
| 589 | } |
---|
[821] | 590 | |
---|
| 591 | |
---|
| 592 | // --------------------------------------------------- |
---|
| 593 | void display_RegionStats(RegionStats * x, char * name) |
---|
| 594 | // --------------------------------------------------- |
---|
[772] | 595 | { |
---|
[821] | 596 | if (name != NULL) { |
---|
| 597 | printf("%s : \n", name); |
---|
| 598 | } |
---|
| 599 | |
---|
[772] | 600 | printf("%4d %4d %4d %4d %6d %8d %8d\n", |
---|
| 601 | x->xmin, |
---|
| 602 | x->xmax, |
---|
| 603 | x->ymin, |
---|
| 604 | x->ymax, |
---|
| 605 | |
---|
| 606 | x->S, |
---|
| 607 | x->Sx, |
---|
| 608 | x->Sy); |
---|
| 609 | } |
---|
[821] | 610 | |
---|
| 611 | |
---|
| 612 | // ------------------------------------------------------------------------ |
---|
| 613 | void display_RegionStatsVector(RegionStats * v, int i0, int i1, char *name) |
---|
| 614 | // ------------------------------------------------------------------------ |
---|
[772] | 615 | { |
---|
[821] | 616 | if (name != NULL) { |
---|
| 617 | printf("%s : [%d..%d]\n", name, i0, i1); |
---|
| 618 | } |
---|
| 619 | else { |
---|
| 620 | printf("RegionStats : [%d..%d]\n", i0, i1); |
---|
| 621 | } |
---|
| 622 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 623 | printf("#%3d: ", i); |
---|
| 624 | display_RegionStats(&(v[i]), NULL); |
---|
| 625 | } |
---|
| 626 | } |
---|
[821] | 627 | |
---|
| 628 | |
---|
| 629 | // ------------------------------------------------------------------------------------------ |
---|
| 630 | void display_RegionStatsMatrix(RegionStats ** m, int i0, int i1, int j0, int j1, char * name) |
---|
| 631 | // ------------------------------------------------------------------------------------------ |
---|
[772] | 632 | { |
---|
| 633 | |
---|
[821] | 634 | if (name != NULL) { |
---|
| 635 | printf("%s : [%d..%d][%d..%d]\n", name, i0, i1, j0, j1); |
---|
| 636 | } |
---|
| 637 | else { |
---|
| 638 | printf("RegionStats : [%d..%d][%d..%d]\n", i0, i1, j0, j1); |
---|
| 639 | } |
---|
| 640 | for (int i = i0; i <= i1; i++) { |
---|
| 641 | for (int j = j0; j <= j1; j++) { |
---|
[772] | 642 | printf("#%3d: ", i); |
---|
| 643 | display_RegionStats(&(m[i][j]), NULL); |
---|
| 644 | } |
---|
| 645 | } |
---|
| 646 | } |
---|
[821] | 647 | |
---|
| 648 | |
---|
| 649 | // ------------------------------------------------ |
---|
| 650 | void save_RegionStats(RegionStats * x, char * name) |
---|
| 651 | // ------------------------------------------------ |
---|
[772] | 652 | { |
---|
| 653 | int fd = -1; |
---|
[821] | 654 | char buff[BUFF_SIZE]; |
---|
[772] | 655 | |
---|
| 656 | if (name == NULL) { |
---|
| 657 | return; |
---|
| 658 | } |
---|
| 659 | // assume name != NULL if single element |
---|
| 660 | // assume name == NULL if vector or matrix |
---|
| 661 | fd = RegionStats_Create_File(name); |
---|
| 662 | if (fd <= 0) { |
---|
| 663 | printf("*** Erreur : ouverture du fichier %s dans %s\n", name, __func__); |
---|
| 664 | } |
---|
[821] | 665 | snprintf(buff, BUFF_SIZE, "%s: %4d %4d %4d %4d %6d %8d %8d\n", |
---|
| 666 | name, |
---|
[772] | 667 | x->xmin, |
---|
| 668 | x->xmax, |
---|
| 669 | x->ymin, |
---|
| 670 | x->ymax, |
---|
| 671 | |
---|
| 672 | x->S, |
---|
| 673 | x->Sx, |
---|
| 674 | x->Sy); |
---|
[821] | 675 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 676 | |
---|
| 677 | if (name) { |
---|
| 678 | RegionStats_Close_File(fd); |
---|
| 679 | } |
---|
| 680 | } |
---|
[821] | 681 | |
---|
| 682 | |
---|
| 683 | // ---------------------------------------------------------------------- |
---|
| 684 | void save_RegionStatsVector(RegionStats * v, int i0, int i1, char * name) |
---|
| 685 | // ---------------------------------------------------------------------- |
---|
[772] | 686 | { |
---|
| 687 | int fd; |
---|
[821] | 688 | char buff[BUFF_SIZE]; |
---|
[772] | 689 | |
---|
| 690 | if (name == NULL) { |
---|
| 691 | name = "RegionStatsVector"; |
---|
| 692 | } |
---|
| 693 | fd = RegionStats_Create_File(name); |
---|
| 694 | |
---|
[821] | 695 | snprintf(buff, BUFF_SIZE, "%s : [%d..%d]\n", name, i0, i1); |
---|
| 696 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 697 | |
---|
[821] | 698 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 699 | printf("#%3d: ", i); |
---|
| 700 | save_RegionStats(&v[i], NULL); |
---|
| 701 | } |
---|
| 702 | RegionStats_Close_File(fd); |
---|
| 703 | } |
---|
[821] | 704 | |
---|
| 705 | |
---|
| 706 | // --------------------------------------------------------------------------------------- |
---|
| 707 | void save_RegionStatsMatrix(RegionStats ** m, int i0, int i1, int j0, int j1, char * name) |
---|
| 708 | // --------------------------------------------------------------------------------------- |
---|
[772] | 709 | { |
---|
| 710 | int fd; |
---|
[821] | 711 | char buff[BUFF_SIZE]; |
---|
[772] | 712 | |
---|
| 713 | if (name == NULL) { |
---|
| 714 | name = "RegionStatsMatrix"; |
---|
| 715 | } |
---|
| 716 | fd = RegionStats_Create_File(name); |
---|
| 717 | |
---|
[821] | 718 | snprintf(buff, BUFF_SIZE, "%s : [%d..%d]\n", name, i0, i1); |
---|
| 719 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 720 | |
---|
[821] | 721 | for (int i = i0; i <= i1; i++) { |
---|
| 722 | for (int j = j0; j <= j1; j++) { |
---|
| 723 | snprintf(buff, BUFF_SIZE, "#%3d: ", i); |
---|
| 724 | write(fd, buff, strlen(buff) + 1); |
---|
[772] | 725 | save_RegionStats(&m[i][j], NULL); |
---|
| 726 | } |
---|
| 727 | } |
---|
| 728 | RegionStats_Close_File(fd); |
---|
| 729 | } |
---|
[821] | 730 | |
---|
| 731 | |
---|
| 732 | // ------------------------------------------------------------------------------- |
---|
| 733 | void RegionStats_Calc1_Features_1Pass(RegionStats * Stats, uint32 e, int i, int j) |
---|
| 734 | // ------------------------------------------------------------------------------- |
---|
[772] | 735 | { |
---|
| 736 | // calcul sur 1 point et non sur toute l'image |
---|
| 737 | // Rectangle |
---|
| 738 | |
---|
[821] | 739 | if (i < Stats[e].ymin) { |
---|
| 740 | Stats[e].ymin = i; |
---|
| 741 | } |
---|
| 742 | if (i > Stats[e].ymax) { |
---|
| 743 | Stats[e].ymax = i; |
---|
| 744 | } |
---|
[772] | 745 | |
---|
[821] | 746 | if (j < Stats[e].xmin) { |
---|
| 747 | Stats[e].xmin = j; |
---|
| 748 | } |
---|
| 749 | if (j > Stats[e].xmax) { |
---|
| 750 | Stats[e].xmax = j; |
---|
| 751 | } |
---|
[772] | 752 | |
---|
| 753 | // Moment1 |
---|
| 754 | Stats[e].S += 1; |
---|
| 755 | Stats[e].Sx += j; |
---|
| 756 | Stats[e].Sy += i; |
---|
| 757 | |
---|
| 758 | return; |
---|
| 759 | } |
---|
[821] | 760 | |
---|
| 761 | |
---|
[772] | 762 | // -------------------------------- |
---|
| 763 | // --- fonctions de 2013 ---------- |
---|
| 764 | // -------------------------------- |
---|
[821] | 765 | // --------------------------------------------------------------------------------------------- |
---|
| 766 | void RegionStats_Calc_Rectangle_Moment1(uint32 ** E, int height, int width, RegionStats * Stats) |
---|
| 767 | // --------------------------------------------------------------------------------------------- |
---|
[772] | 768 | { |
---|
| 769 | uint32 x, y; |
---|
| 770 | uint32 e; |
---|
| 771 | |
---|
[821] | 772 | for (int i = 0; i < height; i++) { |
---|
| 773 | for (int j = 0; j < width; j++) { |
---|
[772] | 774 | |
---|
| 775 | e = E[i][j]; |
---|
| 776 | if (e) { |
---|
| 777 | x = j; |
---|
| 778 | y = i; |
---|
| 779 | |
---|
[821] | 780 | if (i < Stats[e].ymin) { |
---|
| 781 | Stats[e].ymin = y; |
---|
| 782 | } |
---|
| 783 | if (i > Stats[e].ymax) { |
---|
| 784 | Stats[e].ymax = y; |
---|
| 785 | } |
---|
[772] | 786 | |
---|
[821] | 787 | if (j < Stats[e].xmin) { |
---|
| 788 | Stats[e].xmin = x; |
---|
| 789 | } |
---|
| 790 | if (j > Stats[e].xmax) { |
---|
| 791 | Stats[e].xmax = x; |
---|
| 792 | } |
---|
[772] | 793 | |
---|
| 794 | Stats[e].S += 1; |
---|
| 795 | Stats[e].Sx += x; |
---|
| 796 | Stats[e].Sy += y; |
---|
| 797 | } |
---|
| 798 | } |
---|
| 799 | } |
---|
| 800 | } |
---|
[821] | 801 | |
---|
| 802 | |
---|
| 803 | // ------------------------------------------------------------------------------------------------------------------------------- |
---|
| 804 | void RegionStats_calc_Status(RegionStats * Stats, uint32 ne, uint32 min_height, uint32 min_width, uint32 min_area, uint8 * status) |
---|
| 805 | // ------------------------------------------------------------------------------------------------------------------------------- |
---|
[772] | 806 | { |
---|
| 807 | uint16 xmin, xmax, ymin, ymax, xsize, ysize; |
---|
| 808 | uint32 size; |
---|
| 809 | uint32 e; |
---|
| 810 | |
---|
| 811 | for (e = 1; e < ne; e++) { |
---|
| 812 | |
---|
| 813 | ymin = Stats[e].ymin; |
---|
| 814 | ymax = Stats[e].ymax; |
---|
| 815 | |
---|
| 816 | xmin = Stats[e].xmin; |
---|
| 817 | xmax = Stats[e].xmax; |
---|
| 818 | |
---|
| 819 | ysize = ymax - ymin + 1; |
---|
| 820 | xsize = xmax - xmin + 1; |
---|
| 821 | size = xsize * ysize; |
---|
| 822 | |
---|
| 823 | if ((size > min_area) && (xsize >= min_width) && (ysize >= min_height)) { |
---|
| 824 | status[e] = 1; |
---|
| 825 | } |
---|
| 826 | else { |
---|
| 827 | status[e] = 0; |
---|
| 828 | } |
---|
| 829 | } |
---|
| 830 | } |
---|
[821] | 831 | |
---|
| 832 | |
---|
| 833 | // ---------------------------------------------------------------------------- |
---|
| 834 | uint32 RegionStats_UpdateEQ_with_Status(uint8 * status, uint32 ne, uint32 * EQ) |
---|
| 835 | // ---------------------------------------------------------------------------- |
---|
[772] | 836 | { |
---|
| 837 | uint32 e; |
---|
| 838 | uint32 na = 0; |
---|
| 839 | for (e = 1; e < ne; e++) { |
---|
| 840 | if (status[e]) { |
---|
| 841 | EQ[e] = ++na; |
---|
| 842 | } |
---|
| 843 | else { |
---|
| 844 | EQ[e] = 0; |
---|
| 845 | } |
---|
| 846 | } |
---|
| 847 | return na; |
---|
| 848 | } |
---|
[821] | 849 | |
---|
| 850 | |
---|
| 851 | // ------------------------------------------------------------------------------ |
---|
| 852 | void RegionStats_UpdateStats_with_EQ(uint32 * EQ, uint32 ne, RegionStats * Stats) |
---|
| 853 | // ------------------------------------------------------------------------------ |
---|
[772] | 854 | { |
---|
| 855 | uint32 e, a; |
---|
| 856 | for (e = 1; e < ne; e++) { |
---|
| 857 | a = EQ[e]; |
---|
| 858 | if (a != e) { |
---|
| 859 | // copy |
---|
| 860 | RegionStats_Copy1(&Stats[e], &Stats[a]); |
---|
| 861 | } |
---|
| 862 | else { |
---|
| 863 | // do nothing |
---|
| 864 | } |
---|
| 865 | } |
---|
| 866 | } |
---|
[821] | 867 | |
---|
| 868 | |
---|
| 869 | // ----------------------------------------------------------------------------- |
---|
| 870 | void featuresComputation(uint32 ** E, int height,int width, RegionStats * Stats) |
---|
| 871 | // ----------------------------------------------------------------------------- |
---|
[772] | 872 | { |
---|
| 873 | //uint32 nemax = height * width /2; |
---|
| 874 | RegionStats_Calc_Rectangle_Moment1(E, height, width, Stats); |
---|
| 875 | } |
---|
[821] | 876 | |
---|
| 877 | |
---|
| 878 | // --------------------------------------------------------------------------- |
---|
| 879 | void pointFeaturesComputation( uint32 ** E, int i, int j, RegionStats * Stats) |
---|
| 880 | // --------------------------------------------------------------------------- |
---|
[772] | 881 | { |
---|
| 882 | uint32 x, y; |
---|
| 883 | uint32 e; |
---|
| 884 | |
---|
| 885 | e = E[i][j]; |
---|
| 886 | if (e) { |
---|
| 887 | |
---|
| 888 | x = j; |
---|
| 889 | y = i; |
---|
| 890 | |
---|
[821] | 891 | if (i < Stats[e].ymin) { |
---|
| 892 | Stats[e].ymin = y; |
---|
| 893 | } |
---|
| 894 | if (i > Stats[e].ymax) { |
---|
| 895 | Stats[e].ymax = y; |
---|
| 896 | } |
---|
[772] | 897 | |
---|
[821] | 898 | if (j < Stats[e].xmin) { |
---|
| 899 | Stats[e].xmin = x; |
---|
| 900 | } |
---|
| 901 | if (j > Stats[e].xmax) { |
---|
| 902 | Stats[e].xmax = x; |
---|
| 903 | } |
---|
[772] | 904 | |
---|
| 905 | Stats[e].S += 1; |
---|
| 906 | Stats[e].Sx += x; |
---|
| 907 | Stats[e].Sy += y; |
---|
| 908 | } |
---|
| 909 | } |
---|
[821] | 910 | |
---|
| 911 | |
---|
| 912 | // ----------------------------------------------------------------------------- |
---|
| 913 | void lineFeaturesComputation(uint32 ** E, int i, int width, RegionStats * Stats) |
---|
| 914 | // ----------------------------------------------------------------------------- |
---|
[772] | 915 | { |
---|
| 916 | // line RegionStats_Calc_Rectangle_Moment1 |
---|
| 917 | |
---|
| 918 | uint32 x, y; |
---|
| 919 | uint32 e; |
---|
| 920 | |
---|
[821] | 921 | for (int j = 0; j < width; j++) { |
---|
[772] | 922 | |
---|
| 923 | e = E[i][j]; |
---|
| 924 | if (e) { |
---|
| 925 | |
---|
| 926 | x = j; |
---|
| 927 | y = i; |
---|
| 928 | |
---|
[821] | 929 | if (i < Stats[e].ymin) { |
---|
| 930 | Stats[e].ymin = y; |
---|
| 931 | } |
---|
| 932 | if (i > Stats[e].ymax) { |
---|
| 933 | Stats[e].ymax = y; |
---|
| 934 | } |
---|
[772] | 935 | |
---|
[821] | 936 | if (j < Stats[e].xmin) { |
---|
| 937 | Stats[e].xmin = x; |
---|
| 938 | } |
---|
| 939 | if (j > Stats[e].xmax) { |
---|
| 940 | Stats[e].xmax = x; |
---|
| 941 | } |
---|
[772] | 942 | |
---|
| 943 | Stats[e].S += 1; |
---|
| 944 | Stats[e].Sx += x; |
---|
| 945 | Stats[e].Sy += y; |
---|
| 946 | } |
---|
| 947 | } |
---|
| 948 | } |
---|
[821] | 949 | |
---|
| 950 | |
---|
| 951 | // -------------------------------------------------------------------------------------- |
---|
| 952 | void bandFeaturesComputation(uint32 ** E, int i0, int i1, int width, RegionStats * Stats) |
---|
| 953 | // -------------------------------------------------------------------------------------- |
---|
[772] | 954 | { |
---|
[821] | 955 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 956 | lineFeaturesComputation(E, i, width, Stats); |
---|
| 957 | } |
---|
| 958 | } |
---|
[821] | 959 | |
---|
| 960 | |
---|
| 961 | // ----------------------------------------------------------------------------------- |
---|
| 962 | void imageFeaturesComputation(uint32 ** E, int height, int width, RegionStats * Stats) |
---|
| 963 | // ----------------------------------------------------------------------------------- |
---|
[772] | 964 | { |
---|
| 965 | // image RegionStats_Calc_Rectangle_Moment1 |
---|
[821] | 966 | for (int i = 0; i < height; i++) { |
---|
[772] | 967 | lineFeaturesComputation(E, i, width, Stats); |
---|
| 968 | } |
---|
| 969 | } |
---|
[821] | 970 | |
---|
| 971 | |
---|
[772] | 972 | // --------------------------------------- |
---|
| 973 | // --- Fonctions 2014 -------------------- |
---|
| 974 | // --------------------------------------- |
---|
| 975 | |
---|
[821] | 976 | // --------------------------------------------------------------------------------------- |
---|
| 977 | void RegionStats_Copy_Stats1_From_Index(RegionStats * Stats, int dst_index, int src_index) |
---|
| 978 | // --------------------------------------------------------------------------------------- |
---|
[772] | 979 | { |
---|
| 980 | // R[dst] = R[src] |
---|
| 981 | RegionStats_Copy1(&Stats[src_index], &Stats[dst_index]); |
---|
| 982 | } |
---|
[821] | 983 | |
---|
| 984 | |
---|
| 985 | // --------------------------------------------------------------------------------------------- |
---|
| 986 | void RegionStats_Accumulate_Stats1_From_Index(RegionStats * Stats, int dst_index, int src_index) |
---|
| 987 | // --------------------------------------------------------------------------------------------- |
---|
[772] | 988 | { |
---|
| 989 | // R[dst] += R[src] |
---|
| 990 | Stats[dst_index].xmin = ui16min2(Stats[dst_index].xmin, Stats[src_index].xmin); |
---|
| 991 | Stats[dst_index].xmax = ui16max2(Stats[dst_index].xmax, Stats[src_index].xmax); |
---|
| 992 | Stats[dst_index].ymin = ui16min2(Stats[dst_index].ymin, Stats[src_index].ymin); |
---|
| 993 | Stats[dst_index].ymax = ui16max2(Stats[dst_index].ymax, Stats[src_index].ymax); |
---|
| 994 | |
---|
| 995 | Stats[dst_index].S += Stats[src_index].S; |
---|
| 996 | Stats[dst_index].Sx += Stats[src_index].Sx; |
---|
| 997 | Stats[dst_index].Sy += Stats[src_index].Sy; |
---|
| 998 | } |
---|
[821] | 999 | |
---|
| 1000 | |
---|
| 1001 | // --------------------------------------------------------------------------------------------------------------------------- |
---|
| 1002 | void RegionStats_DisplayStats_Sparse(uint32 * EQ, uint32 ne0, uint32 ne1, RegionStats * Stats, char * name, int * start_index) |
---|
| 1003 | // --------------------------------------------------------------------------------------------------------------------------- |
---|
[772] | 1004 | { |
---|
| 1005 | // n'affiche que les racines. |
---|
| 1006 | // ne pas utiliser apres un pack, car le test n'a plus de sens |
---|
| 1007 | uint32 e; |
---|
| 1008 | uint32 na; // compteur |
---|
| 1009 | |
---|
[821] | 1010 | if (name) { |
---|
| 1011 | printf(name); |
---|
| 1012 | } |
---|
[772] | 1013 | |
---|
[821] | 1014 | //na = RegionStats_Count_Roots_Sparse(Stats, EQ, ne0, ne1); |
---|
| 1015 | //printf("%d\n", na); |
---|
[772] | 1016 | |
---|
| 1017 | for (e = ne0; e <= ne1; e++) { |
---|
[821] | 1018 | if (e == EQ[e] && Stats[e].S > 0) { |
---|
| 1019 | if (start_index != NULL) { |
---|
| 1020 | printf("%5d ", *start_index); |
---|
| 1021 | *start_index = *start_index + 1; |
---|
| 1022 | } |
---|
| 1023 | else { |
---|
| 1024 | printf("%5d ", e); |
---|
| 1025 | } |
---|
[772] | 1026 | display_RegionStats(&Stats[e], NULL); |
---|
| 1027 | } |
---|
| 1028 | } |
---|
| 1029 | } |
---|
[821] | 1030 | |
---|
| 1031 | |
---|
| 1032 | // ------------------------------------------------------------------------------------------------------- |
---|
| 1033 | void RegionStats_DisplayStats_Range(uint32 * EQ, uint32 ne0, uint32 ne1, RegionStats * Stats, char * name) |
---|
| 1034 | // ------------------------------------------------------------------------------------------------------- |
---|
[772] | 1035 | { |
---|
| 1036 | // affichage dense (apres un pack) |
---|
| 1037 | |
---|
| 1038 | uint32 e; |
---|
| 1039 | |
---|
[821] | 1040 | if (name) { |
---|
| 1041 | printf(name); |
---|
| 1042 | } |
---|
[772] | 1043 | |
---|
| 1044 | for (e = ne0; e <= ne1; e++) { |
---|
| 1045 | printf("%5d ", e); |
---|
| 1046 | display_RegionStats(&Stats[e], NULL); |
---|
| 1047 | } |
---|
| 1048 | } |
---|
[821] | 1049 | |
---|
| 1050 | |
---|
| 1051 | // ----------------------------------------------------------------------------------------------------------- |
---|
| 1052 | void RegionStats_Save_Stats1_Sparse(RegionStats * Stats, uint32 * EQ, uint32 ne0, uint32 ne1, char * filename) |
---|
| 1053 | // ----------------------------------------------------------------------------------------------------------- |
---|
[772] | 1054 | { |
---|
| 1055 | int fd; |
---|
| 1056 | uint32 na = 0; |
---|
| 1057 | |
---|
| 1058 | fd = RegionStats_Create_File(filename); |
---|
| 1059 | na = RegionStats_Count_Roots_Sparse(Stats, EQ, ne0, ne1); |
---|
| 1060 | |
---|
| 1061 | RegionStats_Write_Header(na, fd); |
---|
| 1062 | RegionStats_Write_Stats1_Sparse(Stats, EQ, ne0, ne1, fd); |
---|
| 1063 | RegionStats_Close_File(fd); |
---|
| 1064 | } |
---|
[821] | 1065 | |
---|
| 1066 | |
---|
| 1067 | // -------------------------------------------------------------------------------------------- |
---|
| 1068 | uint32 RegionStats_Count_Roots_Sparse(RegionStats * Stats, uint32 * EQ, uint32 ne0, uint32 ne1) |
---|
| 1069 | // -------------------------------------------------------------------------------------------- |
---|
[772] | 1070 | { |
---|
| 1071 | uint32 e, c = 0; // compteur |
---|
| 1072 | |
---|
| 1073 | for (e = ne0; e <= ne1; e++) { |
---|
| 1074 | if ((e == EQ[e]) && (Stats[e].S > 0)) { |
---|
| 1075 | c++; |
---|
| 1076 | } |
---|
| 1077 | } |
---|
| 1078 | return c; |
---|
| 1079 | } |
---|
[821] | 1080 | |
---|
| 1081 | |
---|
| 1082 | // ----------------------------------------------------------------------------------- |
---|
| 1083 | uint32 RegionStats_Count_Roots_Sparse1(RegionStats * Stats, uint32 * EQ, uint32 nemax) |
---|
| 1084 | // ----------------------------------------------------------------------------------- |
---|
[772] | 1085 | { |
---|
| 1086 | return RegionStats_Count_Roots_Sparse(Stats, EQ, 1, nemax); |
---|
| 1087 | } |
---|
[821] | 1088 | |
---|
| 1089 | |
---|
| 1090 | // --------------------------------------------------------------------------------------------- |
---|
| 1091 | uint32 RegionStats_Count_Labels_Sparse(RegionStats * Stats, uint32 * EQ, uint32 ne0, uint32 ne1) |
---|
| 1092 | // --------------------------------------------------------------------------------------------- |
---|
[772] | 1093 | { |
---|
| 1094 | uint32 e, c = 0; // compteur |
---|
| 1095 | |
---|
| 1096 | for (e = ne0; e <= ne1; e++) { |
---|
| 1097 | if (Stats[e].S > 0) { |
---|
| 1098 | c++; |
---|
| 1099 | } |
---|
| 1100 | } |
---|
| 1101 | return c; |
---|
| 1102 | } |
---|
[821] | 1103 | |
---|
| 1104 | |
---|
| 1105 | // ------------------------------------------------------------------------------------ |
---|
| 1106 | uint32 RegionStats_Count_Labels_Sparse1(RegionStats * Stats, uint32 * EQ, uint32 nemax) |
---|
| 1107 | // ------------------------------------------------------------------------------------ |
---|
[772] | 1108 | { |
---|
| 1109 | return RegionStats_Count_Labels_Sparse(Stats, EQ, 1, nemax); |
---|
| 1110 | } |
---|
[821] | 1111 | |
---|
| 1112 | |
---|
| 1113 | // ----------------------------------------------------------------------- |
---|
| 1114 | void copy_features_ui32matrix(RegionStats * Stats, uint32 ne, uint32 ** m) |
---|
| 1115 | // ----------------------------------------------------------------------- |
---|
[772] | 1116 | { |
---|
[821] | 1117 | for (int i = 0; i <= (int) ne; i++) { |
---|
[772] | 1118 | |
---|
| 1119 | m[i][0] = i; |
---|
| 1120 | |
---|
| 1121 | // 16 bits: clean but requires 2 sorts |
---|
| 1122 | //m[i][1] = Stats[i].xmin; |
---|
| 1123 | //m[i][2] = Stats[i].xmax; |
---|
| 1124 | //m[i][3] = Stats[i].ymin; |
---|
| 1125 | //m[i][4] = Stats[i].ymax; |
---|
| 1126 | |
---|
| 1127 | // 32 bits: dirty, but requires only 1 sort |
---|
| 1128 | m[i][1] = (Stats[i].ymin << 16) | Stats[i].ymax; |
---|
| 1129 | m[i][2] = (Stats[i].xmin << 16) | Stats[i].xmax; |
---|
| 1130 | |
---|
| 1131 | // 32 bits |
---|
| 1132 | m[i][3] = Stats[i].S; |
---|
| 1133 | m[i][4] = Stats[i].Sx; |
---|
| 1134 | m[i][5] = Stats[i].Sy; |
---|
| 1135 | } |
---|
| 1136 | } |
---|
[821] | 1137 | |
---|
| 1138 | |
---|
| 1139 | // ----------------------------------------------------------------------- |
---|
| 1140 | void copy_ui32matrix_features(uint32 ** m, uint32 ne, RegionStats * Stats) |
---|
| 1141 | // ----------------------------------------------------------------------- |
---|
[772] | 1142 | { |
---|
[821] | 1143 | for (int i = 0; i <= (int) ne; i++) { |
---|
[772] | 1144 | |
---|
| 1145 | Stats[i].xmin = m[i][2] >> 16; |
---|
| 1146 | Stats[i].xmax = m[i][2] & 0xffff; |
---|
| 1147 | Stats[i].ymin = m[i][1] >> 16; |
---|
| 1148 | Stats[i].ymax = m[i][1] & 0xffff; |
---|
| 1149 | |
---|
| 1150 | Stats[i].S = m[i][3]; |
---|
| 1151 | Stats[i].Sx = m[i][4]; |
---|
| 1152 | Stats[i].Sy = m[i][5]; |
---|
| 1153 | } |
---|
| 1154 | } |
---|
[821] | 1155 | |
---|
| 1156 | |
---|
| 1157 | // ---------------------------------------------------------------------------- |
---|
| 1158 | void sortv_ui32matrix_col(uint32 ** m, int i0, int i1, int j0, int j1, int col) |
---|
| 1159 | // ---------------------------------------------------------------------------- |
---|
[772] | 1160 | { |
---|
| 1161 | // nrsort2 for NRC2 |
---|
| 1162 | //sortv_ui32matrix_selection_min(m, i0, i1, j0, j1, col); |
---|
| 1163 | |
---|
| 1164 | long nrl = i0; |
---|
| 1165 | long nrh = i1; |
---|
| 1166 | long nc = col; |
---|
| 1167 | |
---|
| 1168 | /* |
---|
| 1169 | * sort an matrix of int, with the selection algorithm. |
---|
| 1170 | * the key is in column nc |
---|
| 1171 | * the sort is performed, by doing a purmutation on the lines, |
---|
| 1172 | * instead of copying the lines. |
---|
| 1173 | */ |
---|
| 1174 | |
---|
| 1175 | uint32 x, min, pos; |
---|
| 1176 | uint32 * ptr; |
---|
| 1177 | |
---|
[821] | 1178 | for (int i = nrl; i < nrh; i++) { |
---|
[772] | 1179 | min = m[i][nc]; |
---|
| 1180 | pos = i; |
---|
[821] | 1181 | for (int j = i + 1; j <= nrh; j++) { |
---|
[772] | 1182 | x = m[j][nc]; |
---|
| 1183 | if (x < min) { |
---|
| 1184 | min = x; |
---|
| 1185 | pos = j; |
---|
| 1186 | } |
---|
| 1187 | } // j |
---|
| 1188 | |
---|
| 1189 | // permutation des pointeurs de ligne de la matrice |
---|
| 1190 | ptr = m[i]; |
---|
| 1191 | m[i] = m[pos]; |
---|
| 1192 | m[pos] = ptr; |
---|
| 1193 | } // i |
---|
| 1194 | } |
---|
[821] | 1195 | |
---|
| 1196 | |
---|
| 1197 | // ------------------------------------------------------------- |
---|
| 1198 | void RegionStats_SortFeatures(RegionStats * Stats, uint32 nemax) |
---|
| 1199 | // ------------------------------------------------------------- |
---|
[772] | 1200 | { |
---|
| 1201 | uint32 ** m = NULL; |
---|
| 1202 | |
---|
| 1203 | m = ui32matrix(0, nemax, 0, 7); |
---|
| 1204 | |
---|
| 1205 | copy_features_ui32matrix(Stats, nemax, m); |
---|
| 1206 | sortv_ui32matrix_col(m, 0, nemax, 0, 5, 1); |
---|
| 1207 | copy_ui32matrix_features(m, nemax, Stats); |
---|
| 1208 | } |
---|
| 1209 | |
---|
| 1210 | |
---|
[821] | 1211 | // -------------------------------------------------------------------------------- |
---|
| 1212 | void calc_xmin(uint32 ** restrict E, int height, int width, uint16 * restrict Xmin) |
---|
| 1213 | // -------------------------------------------------------------------------------- |
---|
[772] | 1214 | { |
---|
[821] | 1215 | uint32 x; |
---|
[772] | 1216 | uint32 e; |
---|
[821] | 1217 | for (int i = 0; i < height; i++) { |
---|
| 1218 | for (int j = 0; j < width; j++) { |
---|
[772] | 1219 | e = E[i][j]; |
---|
| 1220 | if (e) { |
---|
| 1221 | x = j; |
---|
[821] | 1222 | if (x < Xmin[e]) { |
---|
| 1223 | Xmin[e] = x; |
---|
[772] | 1224 | } |
---|
| 1225 | } |
---|
| 1226 | } |
---|
| 1227 | } |
---|
| 1228 | } |
---|
[821] | 1229 | |
---|
| 1230 | |
---|
| 1231 | // -------------------------------------------------------------------------------- |
---|
| 1232 | void calc_xmax(uint32 ** restrict E, int height, int width, uint16 * restrict Xmax) |
---|
| 1233 | // -------------------------------------------------------------------------------- |
---|
[772] | 1234 | { |
---|
| 1235 | uint32 x; |
---|
| 1236 | uint32 e; |
---|
[821] | 1237 | for (int i = 0; i < height; i++) { |
---|
| 1238 | for (int j = 0; j < width; j++) { |
---|
[772] | 1239 | e = E[i][j]; |
---|
| 1240 | if (e) { |
---|
| 1241 | x = j; |
---|
| 1242 | if (x > Xmax[e]) Xmax[e] = x; |
---|
| 1243 | } |
---|
| 1244 | } |
---|
| 1245 | } |
---|
| 1246 | } |
---|
[821] | 1247 | |
---|
| 1248 | |
---|
| 1249 | // -------------------------------------------------------------------------------- |
---|
| 1250 | void calc_ymin(uint32 ** restrict E, int height, int width, uint16 * restrict Ymin) |
---|
| 1251 | // -------------------------------------------------------------------------------- |
---|
[772] | 1252 | { |
---|
| 1253 | uint32 y; |
---|
| 1254 | uint32 e; |
---|
[821] | 1255 | for (int i = 0; i < height; i++) { |
---|
| 1256 | for (int j = 0; j < width; j++) { |
---|
[772] | 1257 | e = E[i][j]; |
---|
[821] | 1258 | if (e) { |
---|
[772] | 1259 | y = i; |
---|
[821] | 1260 | if (y < Ymin[e]) { |
---|
| 1261 | Ymin[e] = y; |
---|
| 1262 | } |
---|
[772] | 1263 | } |
---|
| 1264 | } |
---|
| 1265 | } |
---|
| 1266 | } |
---|
[821] | 1267 | |
---|
| 1268 | |
---|
| 1269 | // -------------------------------------------------------------------------------- |
---|
| 1270 | void calc_ymax(uint32 ** restrict E, int height, int width, uint16 * restrict Ymax) |
---|
| 1271 | // -------------------------------------------------------------------------------- |
---|
[772] | 1272 | { |
---|
| 1273 | uint32 y; |
---|
| 1274 | uint32 e; |
---|
[821] | 1275 | for (int i = 0; i < height; i++) { |
---|
| 1276 | for (int j = 0; j < width; j++) { |
---|
[772] | 1277 | e = E[i][j]; |
---|
[821] | 1278 | if (e) { |
---|
[772] | 1279 | y = i; |
---|
[821] | 1280 | if (y > Ymax[e]) { |
---|
| 1281 | Ymax[e] = y; |
---|
| 1282 | } |
---|
[772] | 1283 | } |
---|
| 1284 | } |
---|
| 1285 | } |
---|
| 1286 | } |
---|
[821] | 1287 | |
---|
| 1288 | |
---|
| 1289 | // -------------------------------------------------------------------------- |
---|
| 1290 | void calc_s(uint32 ** restrict E, int height, int width, uint32 * restrict S) |
---|
| 1291 | // -------------------------------------------------------------------------- |
---|
[772] | 1292 | { |
---|
| 1293 | uint32 e; |
---|
[821] | 1294 | for (int i = 0; i < height; i++) { |
---|
| 1295 | for (int j = 0; j < width; j++) { |
---|
[772] | 1296 | e = E[i][j]; |
---|
[821] | 1297 | if (e) { |
---|
[772] | 1298 | S[e] += 1; |
---|
| 1299 | } |
---|
| 1300 | } |
---|
| 1301 | } |
---|
| 1302 | } |
---|
[821] | 1303 | |
---|
| 1304 | |
---|
| 1305 | // ---------------------------------------------------------------------------- |
---|
| 1306 | void calc_sx(uint32 ** restrict E, int height, int width, uint32 * restrict Sx) |
---|
| 1307 | // ---------------------------------------------------------------------------- |
---|
[772] | 1308 | { |
---|
| 1309 | uint32 e; |
---|
[821] | 1310 | for (int i = 0; i < height; i++) { |
---|
| 1311 | for (int j = 0; j < width; j++) { |
---|
[772] | 1312 | e = E[i][j]; |
---|
[821] | 1313 | if (e) { |
---|
[772] | 1314 | Sx[e] += j; |
---|
| 1315 | } |
---|
| 1316 | } |
---|
| 1317 | } |
---|
| 1318 | } |
---|
[821] | 1319 | |
---|
| 1320 | |
---|
| 1321 | // ---------------------------------------------------------------------------- |
---|
| 1322 | void calc_sy(uint32 ** restrict E, int height, int width, uint32 * restrict Sy) |
---|
| 1323 | // ---------------------------------------------------------------------------- |
---|
[772] | 1324 | { |
---|
| 1325 | uint32 e; |
---|
[821] | 1326 | for (int i = 0; i < height; i++) { |
---|
| 1327 | for (int j = 0; j < width; j++) { |
---|
[772] | 1328 | e = E[i][j]; |
---|
| 1329 | if (e) { |
---|
| 1330 | Sy[e] += i; |
---|
| 1331 | } |
---|
| 1332 | } |
---|
| 1333 | } |
---|
| 1334 | } |
---|
| 1335 | |
---|
[821] | 1336 | |
---|
| 1337 | // -------------------------------------------------------- |
---|
| 1338 | int RegionStats_Compare(RegionStats * S1, RegionStats * S2) |
---|
| 1339 | // -------------------------------------------------------- |
---|
[772] | 1340 | { |
---|
| 1341 | //puts("----------------------------------------"); |
---|
| 1342 | //display_RegionStats(S1, "S1"); |
---|
| 1343 | //display_RegionStats(S2, "S2"); |
---|
[821] | 1344 | if ((S1->xmin == S2->xmin) && |
---|
[772] | 1345 | (S1->xmax == S2->xmax) && |
---|
| 1346 | (S1->ymin == S2->ymin) && |
---|
| 1347 | (S1->ymax == S2->ymax) && |
---|
[821] | 1348 | (S1->S == S2->S) && |
---|
| 1349 | (S1->Sx == S2->Sx) && |
---|
| 1350 | (S1->Sy == S2->Sy)) { |
---|
[772] | 1351 | return 1; |
---|
[821] | 1352 | } |
---|
| 1353 | else { |
---|
[772] | 1354 | return 0; |
---|
[821] | 1355 | } |
---|
[772] | 1356 | } |
---|
[821] | 1357 | |
---|
| 1358 | |
---|
| 1359 | // ------------------------------------------------------------------------------ |
---|
| 1360 | int RegionStatsVector_Compare(RegionStats * S1, int i0, int i1, RegionStats * S2) |
---|
| 1361 | // ------------------------------------------------------------------------------ |
---|
[772] | 1362 | { |
---|
| 1363 | int c; // resultat de la comparaison 0 = identique, 1 = different |
---|
| 1364 | int s = 0; // somme |
---|
| 1365 | |
---|
[821] | 1366 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 1367 | c = RegionStats_Compare(&S1[i], &S2[i]); |
---|
| 1368 | s += c; |
---|
| 1369 | } |
---|
| 1370 | return s; |
---|
| 1371 | } |
---|
[821] | 1372 | |
---|
| 1373 | |
---|
| 1374 | // -------------------------------------------------------------------------------------------- |
---|
| 1375 | int RegionStatsVector_Match(RegionStats * S1, int i0, int i1, RegionStats * S2, int j0, int j1) |
---|
| 1376 | // -------------------------------------------------------------------------------------------- |
---|
[772] | 1377 | { |
---|
[821] | 1378 | int j, pos; |
---|
[772] | 1379 | int c; // resultat de la comparaison 1 = identique, 0 = different |
---|
| 1380 | int a; // accumulateur de c |
---|
| 1381 | int s = 0; // somme |
---|
| 1382 | int perm = 0; // permutation de numero de features |
---|
[821] | 1383 | int n1 = i1 - i0 + 1; |
---|
| 1384 | int n2 = j1 - j0 + 1; |
---|
[772] | 1385 | |
---|
| 1386 | //printf("[RegionStatsVector_Match]: [%d..%d]=%d vs [%d..%d]=%d\n", i0, i1, n1, j0,j1,n2); |
---|
| 1387 | if (n1 != n2) { |
---|
| 1388 | printf("card(S1) = %d neq card(S2) = %d ", n1, n2); |
---|
| 1389 | return 1; |
---|
| 1390 | } |
---|
| 1391 | |
---|
[821] | 1392 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 1393 | a = 0; |
---|
| 1394 | pos = -1; |
---|
| 1395 | |
---|
| 1396 | for (j = j0; j <= j1; j++) { |
---|
| 1397 | c = RegionStats_Compare(&S1[i], &S2[j]); |
---|
| 1398 | a = a + c; |
---|
[821] | 1399 | if (c) { |
---|
| 1400 | pos = j; |
---|
| 1401 | } |
---|
[772] | 1402 | } |
---|
| 1403 | s += a; |
---|
| 1404 | |
---|
| 1405 | if (a > 1) { |
---|
| 1406 | printf("erreur: il y a plusieurs fois la composante S1[%d] dans S2\n", i); |
---|
| 1407 | for (j = j0; j <= j1; j++) { |
---|
| 1408 | c = RegionStats_Compare(&S1[i], &S2[j]); |
---|
[821] | 1409 | if (c) { |
---|
| 1410 | printf("S2[%d] ", j); |
---|
| 1411 | } |
---|
[772] | 1412 | } |
---|
[821] | 1413 | printf("\n"); |
---|
| 1414 | exit(1); |
---|
[772] | 1415 | } |
---|
| 1416 | |
---|
| 1417 | if (i != pos) { |
---|
| 1418 | //printf("perm(%d,%d)", i, pos); |
---|
| 1419 | perm++; |
---|
| 1420 | } |
---|
| 1421 | if (a) { |
---|
| 1422 | //printf("S1[%d] = S2[%d]\n", i, pos); |
---|
| 1423 | } |
---|
| 1424 | else { |
---|
| 1425 | //printf("S1[%d] not matched\n", i); |
---|
| 1426 | } |
---|
| 1427 | |
---|
| 1428 | } |
---|
| 1429 | //printf("%4d", n1 - s); // le nb d'erreur |
---|
| 1430 | printf("perm = %d ", perm); |
---|
| 1431 | return n1 - s; |
---|
| 1432 | } |
---|
[822] | 1433 | #endif // FEATURES |
---|
[821] | 1434 | |
---|
| 1435 | // Local Variables: |
---|
| 1436 | // tab-width: 4 |
---|
| 1437 | // c-basic-offset: 4 |
---|
| 1438 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 1439 | // indent-tabs-mode: nil |
---|
| 1440 | // End: |
---|
| 1441 | |
---|
| 1442 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 1443 | |
---|