2 * Image similarity testing
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "lib/getopt.h"
12 #include "lib/fastbuf.h"
13 #include "images/images.h"
14 #include "images/color.h"
15 #include "images/signature.h"
21 #include <sys/types.h>
28 Usage: image-sim-test [options] image1 [image2] \n\
30 -q --quiet no progress messages\n\
31 -f --format-1 image1 format (jpeg, gif, png)\n\
32 -F --format-2 image2 format\n\
33 -g --background background color (hexadecimal RRGGBB)\n\
34 -s --segmentation-1 writes image1 segmentation to given file\n\
35 -S --segmentation-2 writes image2 segmentation to given file\n\
40 static char *shortopts = "qf:F:g:t:s:S:" CF_SHORT_OPTS;
41 static struct option longopts[] =
44 { "quiet", 0, 0, 'q' },
45 { "format-1", 0, 0, 'f' },
46 { "format-2", 0, 0, 'F' },
47 { "background", 0, 0, 'g' },
48 { "segmentation-1", 0, 0, 's' },
49 { "segmentation-2", 0, 0, 'S' },
53 static uns verbose = 1;
54 static byte *file_name_1;
55 static byte *file_name_2;
56 static enum image_format format_1;
57 static enum image_format format_2;
58 static struct color background_color;
59 static byte *segmentation_name_1;
60 static byte *segmentation_name_2;
62 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
63 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
66 dump_signature(struct image_signature *sig)
68 byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
69 image_vector_dump(buf, &sig->vec);
70 MSG("vector: %s", buf);
71 for (uns i = 0; i < sig->len; i++)
73 image_region_dump(buf, sig->reg + i);
74 MSG("region %u: %s", i, buf);
78 static struct image_thread it;
79 static struct image_io io;
82 write_segmentation(struct image_sig_data *data, byte *fn)
84 MSG("Writing segmentation to %s", fn);
86 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
88 TRY(img = image_new(&it, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
89 image_clear(&it, img);
91 for (uns i = 0; i < data->regions_count; i++)
94 // FIXME: convert from Luv to RGB
95 c[0] = data->regions[i].a[0];
96 c[1] = data->regions[i].a[1];
97 c[2] = data->regions[i].a[2];
98 for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
100 uns x1 = block->x * 4;
101 uns y1 = block->y * 4;
102 uns x2 = MIN(x1 + 4, img->cols);
103 uns y2 = MIN(y1 + 4, img->rows);
104 byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
105 for (uns y = y1; y < y2; y++, p += img->row_size)
108 for (uns x = x1; x < x2; x++, p2 += 3)
120 io.format = image_file_name_to_format(fn);
121 TRY(image_io_write(&io));
129 main(int argc, char **argv)
133 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
140 if (!(format_1 = image_extension_to_format(optarg)))
144 if (!(format_2 = image_extension_to_format(optarg)))
149 if (strlen(optarg) != 6)
153 long int v = strtol(optarg, &end, 16);
154 if (errno || *end || v < 0)
156 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
160 segmentation_name_1 = optarg;
163 segmentation_name_2 = optarg;
169 if (argc != optind + 2 && argc != optind + 1)
171 file_name_1 = argv[optind++];
173 file_name_2 = argv[optind++];
175 MSG("Initializing image library");
176 srandom(time(NULL) ^ getpid());
178 image_thread_init(&it);
180 struct image *img1, *img2;
182 if (!image_io_init(&it, &io))
183 die("Cannot initialize image I/O (%s)", it.err_msg);
187 MSG("Reading %s", file_name_1);
188 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
189 io.format = format_1 ? : image_file_name_to_format(file_name_1);
190 TRY(image_io_read_header(&io));
191 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
192 if (background_color.color_space)
193 io.background_color = background_color;
194 else if (!io.background_color.color_space)
195 io.background_color = color_black;
196 TRY(image_io_read_data(&io, 1));
199 MSG("Image size=%ux%u", img1->cols, img1->rows);
207 MSG("Reading %s", file_name_2);
208 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
209 io.format = format_2 ? : image_file_name_to_format(file_name_2);
210 TRY(image_io_read_header(&io));
211 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
212 if (background_color.color_space)
213 io.background_color = background_color;
214 else if (!io.background_color.color_space)
215 io.background_color = color_black;
216 TRY(image_io_read_data(&io, 1));
219 MSG("Image size=%ux%u", img2->cols, img2->rows);
225 struct image_signature sig1, sig2;
226 MSG("Computing signatures");
229 struct image_sig_data data;
230 TRY(image_sig_init(&it, &data, img1));
231 image_sig_preprocess(&data);
233 image_sig_segmentation(&data);
234 if (segmentation_name_1)
235 write_segmentation(&data, segmentation_name_1);
236 image_sig_finish(&data, &sig1);
237 image_sig_cleanup(&data);
238 dump_signature(&sig1);
242 struct image_sig_data data;
243 TRY(image_sig_init(&it, &data, img2));
244 image_sig_preprocess(&data);
246 image_sig_segmentation(&data);
247 if (segmentation_name_2)
248 write_segmentation(&data, segmentation_name_2);
249 image_sig_finish(&data, &sig2);
250 image_sig_cleanup(&data);
251 dump_signature(&sig2);
256 uns dist = image_signatures_dist(&sig1, &sig2);
257 MSG("dist=%.6f", dist / (double)(1 << IMAGE_SIG_DIST_SCALE));
265 image_io_cleanup(&io);
266 image_thread_cleanup(&it);