]> mj.ucw.cz Git - libucw.git/blob - images/sig-txt.c
removed obsolete segmentation
[libucw.git] / images / sig-txt.c
1 /*
2  *      Image Library -- Detection of textured images
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "sherlock/sherlock.h"
13 #include "images/images.h"
14 #include "images/signature.h"
15 #include "images/math.h"
16
17 #include <string.h>
18
19 #define MAX_CELLS_COLS 4
20 #define MAX_CELLS_ROWS 4
21
22 void
23 image_sig_detect_textured(struct image_sig_data *data)
24 {
25   uns cols = data->cols;
26   uns rows = data->rows;
27   uns cell_cols = MIN((cols + 1) / 2, MAX_CELLS_COLS);
28   uns cell_rows = MIN((rows + 1) / 2, MAX_CELLS_ROWS);
29   uns cell_x[MAX_CELLS_COLS + 1];
30   uns cell_y[MAX_CELLS_ROWS + 1];
31   uns i, j;
32   u32 cnt[IMAGE_REG_MAX];
33
34   if (cell_cols * cell_rows < 4)
35     {
36       DBG("Image is not textured.");
37       return;
38     }
39
40   DBG("Detecting textured image... cols=%u rows=%u cell_cols=%u cell_rows=%u", cols, rows, cell_cols, cell_rows);
41   
42   /* Compute cells boundaries */
43   for (i = 1, j = 0; i < cell_cols; i++)
44     cell_x[i] = fast_div_u32_u8(j += cols, cell_cols);
45   cell_x[0] = 0;
46   cell_x[cell_cols] = cols;
47   for (i = 1, j = 0; i < cell_rows; i++)
48     cell_y[i] = fast_div_u32_u8(j += rows, cell_rows);
49   cell_y[0] = 0;
50   cell_y[cell_rows] = rows;
51
52   /* Preprocess blocks */
53   for (uns i = 0; i < data->regions_count; i++)
54     for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
55       block->region = i;
56   
57   /* Process cells */
58   double e = 0;
59   for (uns j = 0; j < cell_rows; j++)
60     for (uns i = 0; i < cell_cols; i++)
61       {
62         uns cell_area = 0;
63         bzero(cnt, data->regions_count * sizeof(u32));
64         struct image_sig_block *b1 = data->blocks + cell_x[i] + cell_y[j] * cols, *b2;
65         for (uns y = cell_y[j]; y < cell_y[j + 1]; y++, b1 += cols)
66           {
67             b2 = b1;
68             for (uns x = cell_x[i]; x < cell_x[i + 1]; x++, b2++)
69               {
70                 cnt[b2->region]++;
71                 cell_area++;
72               }
73           }
74         for (uns k = 0; k < data->regions_count; k++)
75           {
76             int a = data->blocks_count * cnt[k] - cell_area * data->regions[k].count; 
77             e += (double)a * a / ((double)isqr(data->regions[k].count) * cell_area);
78           }
79       }
80
81   DBG("Coefficient=%g", (double)e / (data->regions_count * data->blocks_count));
82
83   /* Threshold */
84   if (e <= image_sig_textured_threshold * data->regions_count * data->blocks_count)
85     {
86       data->flags |= IMAGE_SIG_TEXTURED;
87       DBG("Image is textured.");
88     }
89   else
90     DBG("Image is not textured.");
91 }