Leptonica 1.68
C Image Processing Library

comparetest.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * comparetest.c
00018  *
00019  *      comparetest filein1 filein2 type fileout
00020  *
00021  *    where
00022  *      type = {0, 1} for {abs-diff and subtraction} comparisons
00023  *
00024  *    Compares two images, using either the absolute value of the
00025  *    pixel differences or the difference clipped to 0.  For RGB,
00026  *    the differences are computed separately on each component.
00027  *    If one has a colormap and the other doesn't, the colormap
00028  *    is removed before making the comparison.
00029  *
00030  *    Warning: you usually want to use abs-diff to compare
00031  *    two grayscale or color images.  If you use subtraction,
00032  *    the result you get will depend on the order of the input images.
00033  *    For example, if pix2 = pixDilateGray(pix1), then every
00034  *    pixel in pix1 will be equal to or greater than pix2.  So if
00035  *    you subtract pix2 from pix1, you will get 0 for all pixels,
00036  *    which looks like they're the same!
00037  *
00038  *    Here's an interesting observation.  Take an image that has
00039  *    been jpeg compressed at a quality = 75.  If you re-compress
00040  *    the image, what quality factor should be used to minimize
00041  *    the change?  Answer:  75 (!)
00042  */
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include "allheaders.h"
00047 
00048 
00049 main(int    argc,
00050      char **argv)
00051 {
00052 l_int32      type, comptype, d1, d2, same, first, last;
00053 l_float32    fract, diff, rmsdiff;
00054 char        *filein1, *filein2, *fileout;
00055 GPLOT       *gplot;
00056 NUMA        *na1, *na2;
00057 PIX         *pixs1, *pixs2, *pixd;
00058 static char  mainName[] = "comparetest";
00059 
00060     if (argc != 5)
00061         exit(ERROR_INT(" Syntax:  comparetest filein1 filein2 type fileout",
00062                        mainName, 1));
00063 
00064     filein1 = argv[1];
00065     filein2 = argv[2];
00066     type = atoi(argv[3]);
00067     pixd = NULL;
00068     fileout = argv[4];
00069     l_pngSetStrip16To8(0);
00070 
00071     if ((pixs1 = pixRead(filein1)) == NULL)
00072         exit(ERROR_INT("pixs1 not made", mainName, 1));
00073     if ((pixs2 = pixRead(filein2)) == NULL)
00074         exit(ERROR_INT("pixs2 not made", mainName, 1));
00075     d1 = pixGetDepth(pixs1);
00076     d2 = pixGetDepth(pixs2);
00077 
00078     if (d1 == 1 && d2 == 1) {
00079         pixEqual(pixs1, pixs2, &same);
00080         if (same) {
00081             fprintf(stderr, "Images are identical\n");
00082             pixd = pixCreateTemplate(pixs1);  /* write empty pix for diff */
00083         }
00084         else {
00085             if (type == 0)
00086                 comptype = L_COMPARE_XOR;
00087             else
00088                 comptype = L_COMPARE_SUBTRACT;
00089             pixCompareBinary(pixs1, pixs2, comptype, &fract, &pixd);
00090             fprintf(stderr, "Fraction of different pixels: %10.6f\n", fract);
00091         }
00092         pixWrite(fileout, pixd, IFF_PNG);
00093     }
00094     else {
00095         if (type == 0)
00096             comptype = L_COMPARE_ABS_DIFF;
00097         else
00098             comptype = L_COMPARE_SUBTRACT;
00099         pixCompareGrayOrRGB(pixs1, pixs2, comptype, GPLOT_X11, &same, &diff,
00100                             &rmsdiff, &pixd);
00101         if (type == 0) {
00102             if (same)
00103                 fprintf(stderr, "Images are identical\n");
00104             else {
00105                 fprintf(stderr, "Images differ: <diff> = %10.6f\n", diff);
00106                 fprintf(stderr, "               <rmsdiff> = %10.6f\n", rmsdiff);
00107             }
00108         }
00109         else {  /* subtraction */
00110             if (same)
00111                 fprintf(stderr, "pixs2 strictly greater than pixs1\n");
00112             else {
00113                 fprintf(stderr, "Images differ: <diff> = %10.6f\n", diff);
00114                 fprintf(stderr, "               <rmsdiff> = %10.6f\n", rmsdiff);
00115             }
00116         }
00117         if (d1 != 16)
00118             pixWrite(fileout, pixd, IFF_JFIF_JPEG);
00119         else 
00120             pixWrite(fileout, pixd, IFF_PNG);
00121 
00122         if (d1 != 16) {
00123             na1 = pixCompareRankDifference(pixs1, pixs2, 1);
00124             if (na1) {
00125                 fprintf(stderr, "na1[150] = %20.10f\n", na1->array[150]);
00126                 fprintf(stderr, "na1[200] = %20.10f\n", na1->array[200]);
00127                 fprintf(stderr, "na1[250] = %20.10f\n", na1->array[250]);
00128                 numaGetNonzeroRange(na1, 0.00005, &first, &last);
00129                 fprintf(stderr, "Nonzero diff range: first = %d, last = %d\n",
00130                         first, last);
00131                 na2 = numaClipToInterval(na1, first, last);
00132                 gplot = gplotCreate("/tmp/junkrank", GPLOT_X11,
00133                                     "Pixel Rank Difference", "pixel val",
00134                                     "rank");
00135                 gplotAddPlot(gplot, NULL, na2, GPLOT_LINES, "rank");
00136                 gplotMakeOutput(gplot);
00137                 gplotDestroy(&gplot);
00138                 numaDestroy(&na1);
00139                 numaDestroy(&na2);
00140             }
00141         }
00142     } 
00143 
00144     pixDestroy(&pixs1);
00145     pixDestroy(&pixs2);
00146     pixDestroy(&pixd);
00147     return 0;
00148 }
00149 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines