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"
22 #include <sys/types.h>
29 Usage: image-sim-test [options] image1 [image2] \n\
31 -q --quiet no progress messages\n\
32 -f --format-1 image1 format (jpeg, gif, png)\n\
33 -F --format-2 image2 format\n\
34 -g --background background color (hexadecimal RRGGBB)\n\
35 -r --segmentation-1 writes image1 segmentation to given file\n\
36 -R --segmentation-2 writes image2 segmentation to given file\n\
41 static char *shortopts = "qf:F:g:t:r:R:" CF_SHORT_OPTS;
42 static struct option longopts[] =
45 { "quiet", 0, 0, 'q' },
46 { "format-1", 0, 0, 'f' },
47 { "format-2", 0, 0, 'F' },
48 { "background", 0, 0, 'g' },
49 { "segmentation-1", 0, 0, 'r' },
50 { "segmentation-2", 0, 0, 'R' },
54 static uns verbose = 1;
55 static byte *file_name_1;
56 static byte *file_name_2;
57 static enum image_format format_1;
58 static enum image_format format_2;
59 static struct color background_color;
60 static byte *segmentation_name_1;
61 static byte *segmentation_name_2;
63 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
64 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
67 msg_str(byte *s, void *param UNUSED)
73 dump_signature(struct image_signature *sig)
75 byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
76 image_vector_dump(buf, &sig->vec);
77 MSG("vector: %s", buf);
78 for (uns i = 0; i < sig->len; i++)
80 image_region_dump(buf, sig->reg + i);
81 MSG("region %u: %s", i, buf);
85 static struct image_context ctx;
86 static struct image_io io;
89 write_segmentation(struct image_sig_data *data, byte *fn)
91 MSG("Writing segmentation to %s", fn);
93 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
95 TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
96 image_clear(&ctx, img);
98 for (uns i = 0; i < data->regions_count; i++)
101 double luv[3], xyz[3], srgb[3];
102 luv[0] = data->regions[i].a[0] * (4 / 2.55);
103 luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
104 luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
105 luv_to_xyz_exact(xyz, luv);
106 xyz_to_srgb_exact(srgb, xyz);
107 c[0] = CLAMP(srgb[0] * 255, 0, 255);
108 c[1] = CLAMP(srgb[1] * 255, 0, 255);
109 c[2] = CLAMP(srgb[2] * 255, 0, 255);
110 for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
112 uns x1 = block->x * 4;
113 uns y1 = block->y * 4;
114 uns x2 = MIN(x1 + 4, img->cols);
115 uns y2 = MIN(y1 + 4, img->rows);
116 byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
117 for (uns y = y1; y < y2; y++, p += img->row_size)
120 for (uns x = x1; x < x2; x++, p2 += 3)
132 io.format = image_file_name_to_format(fn);
133 TRY(image_io_write(&io));
141 main(int argc, char **argv)
145 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
152 if (!(format_1 = image_extension_to_format(optarg)))
156 if (!(format_2 = image_extension_to_format(optarg)))
161 if (strlen(optarg) != 6)
165 long int v = strtol(optarg, &end, 16);
166 if (errno || *end || v < 0)
168 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
172 segmentation_name_1 = optarg;
175 segmentation_name_2 = optarg;
181 if (argc != optind + 2 && argc != optind + 1)
183 file_name_1 = argv[optind++];
185 file_name_2 = argv[optind++];
187 MSG("Initializing image library");
188 srandom(time(NULL) ^ getpid());
190 image_context_init(&ctx);
192 struct image *img1, *img2;
194 TRY(image_io_init(&ctx, &io));
198 MSG("Reading %s", file_name_1);
199 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
200 io.format = format_1 ? : image_file_name_to_format(file_name_1);
201 TRY(image_io_read_header(&io));
202 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
203 if (background_color.color_space)
204 io.background_color = background_color;
205 else if (!io.background_color.color_space)
206 io.background_color = color_black;
207 TRY(image_io_read_data(&io, 1));
210 MSG("Image size=%ux%u", img1->cols, img1->rows);
218 MSG("Reading %s", file_name_2);
219 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
220 io.format = format_2 ? : image_file_name_to_format(file_name_2);
221 TRY(image_io_read_header(&io));
222 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
223 if (background_color.color_space)
224 io.background_color = background_color;
225 else if (!io.background_color.color_space)
226 io.background_color = color_black;
227 TRY(image_io_read_data(&io, 1));
230 MSG("Image size=%ux%u", img2->cols, img2->rows);
236 struct image_signature sig1, sig2;
237 MSG("Computing signatures");
240 struct image_sig_data data;
241 TRY(image_sig_init(&ctx, &data, img1));
242 image_sig_preprocess(&data);
245 image_sig_segmentation(&data);
246 image_sig_detect_textured(&data);
248 if (segmentation_name_1)
249 write_segmentation(&data, segmentation_name_1);
250 image_sig_finish(&data, &sig1);
251 image_sig_cleanup(&data);
252 dump_signature(&sig1);
256 struct image_sig_data data;
257 TRY(image_sig_init(&ctx, &data, img2));
258 image_sig_preprocess(&data);
261 image_sig_segmentation(&data);
262 image_sig_detect_textured(&data);
264 if (segmentation_name_2)
265 write_segmentation(&data, segmentation_name_2);
266 image_sig_finish(&data, &sig2);
267 image_sig_cleanup(&data);
268 dump_signature(&sig2);
276 struct fastbuf *fb = bfdopen(0, 4096);
277 dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
281 dist = image_signatures_dist(&sig1, &sig2);
282 MSG("dist=%u", dist);
290 image_io_cleanup(&io);
291 image_context_cleanup(&ctx);