Leptonica 1.68
C Image Processing Library

otsutest2.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  *   otsutest2.c
00018  *
00019  *   This demonstrates the usefulness of the modified version of Otsu
00020  *   for thresholding an image that doesn't have a well-defined
00021  *   background color.
00022  *
00023  *   Standard Otsu binarization is done with scorefract = 0.0, which
00024  *   returns the threshold at the maximum value of the score.  However.
00025  *   this value is up on the shoulder of the background, and its
00026  *   use causes some of the dark background to be binarized as foreground.
00027  *
00028  *   Using the modified Otsu with scorefract = 0.1 returns a threshold
00029  *   at the lowest value of this histogram such that the score
00030  *   is at least 0.9 times the maximum value of the score.  This allows
00031  *   the threshold to be taken in the histogram minimum between
00032  *   the fg and bg peaks, producing a much cleaner binarization.
00033  */
00034 
00035 #ifndef  _WIN32
00036 #include <unistd.h>
00037 #else
00038 #include <windows.h>   /* for Sleep() */
00039 #endif  /* _WIN32 */
00040 
00041 #include "allheaders.h"
00042 
00043 
00044 main(int    argc,
00045      char **argv)
00046 {
00047 char       textstr[256];
00048 l_int32    i, thresh, fgval, bgval;
00049 l_float32  scorefract;
00050 L_BMF     *bmf;
00051 PIX       *pixs, *pixb, *pixb2, *pixb3, *pixg, *pixp, *pixt1, *pixt2;
00052 PIXA      *pixa;
00053 
00054     pixs = pixRead("1555-7.jpg");
00055     pixg = pixConvertTo8(pixs, 0);
00056     bmf = bmfCreate("fonts", 8);
00057     for (i = 0; i < 3; i++) {
00058         pixa = pixaCreate(3);
00059         scorefract = 0.1 * i;
00060         pixOtsuAdaptiveThreshold(pixg, 2000, 2000, 0, 0, scorefract,
00061                                  NULL, &pixb);
00062         pixSaveTiledOutline(pixb, pixa, 2, 1, 20, 2, 32);
00063         pixSplitDistributionFgBg(pixg, scorefract, 1, &thresh, &fgval, &bgval, 1);
00064         fprintf(stderr, "thresh = %d, fgval = %d, bgval = %d\n", thresh, fgval,
00065                  bgval);
00066 
00067         /* Give gnuplot time to write out the plot */
00068 #ifndef  _WIN32
00069     sleep(1);
00070 #else
00071     Sleep(1000);
00072 #endif  /* _WIN32 */
00073 
00074         pixp = pixRead("/tmp/histplot.png");
00075         pixSaveTiled(pixp, pixa, 1, 0, 20, 1);
00076         pixt1 = pixaDisplay(pixa, 0, 0);
00077         snprintf(textstr, sizeof(textstr),
00078              "Scorefract = %3.1f ........... Thresh = %d", scorefract, thresh);
00079         pixt2 = pixAddSingleTextblock(pixt1, bmf, textstr, 0x00ff0000,
00080                                       L_ADD_BELOW, NULL);
00081         pixDisplay(pixt2, 100, 100);
00082         snprintf(textstr, sizeof(textstr), "/tmp/otsu.%d.png", i);
00083         pixWrite(textstr, pixt2, IFF_PNG);
00084         pixDestroy(&pixb);
00085         pixDestroy(&pixp);
00086         pixDestroy(&pixt1);
00087         pixDestroy(&pixt2);
00088         pixaDestroy(&pixa);
00089     }
00090 
00091     pixa = pixaCreate(2);
00092     for (i = 0; i < 2; i++) {
00093         scorefract = 0.1 * i;
00094         pixOtsuAdaptiveThreshold(pixg, 300, 300, 0, 0, scorefract,
00095                                  NULL, &pixb);
00096         pixb2 = pixAddBlackBorder(pixb, 2);
00097         snprintf(textstr, sizeof(textstr),
00098              "Scorefract = %3.1f", scorefract);
00099         pixb3 = pixAddSingleTextblock(pixb2, bmf, textstr, 1,
00100                                       L_ADD_BELOW, NULL);
00101         pixSaveTiled(pixb3, pixa, 2, (i + 1) % 1, 20, 32);
00102         pixDestroy(&pixb);
00103         pixDestroy(&pixb2);
00104     }
00105     pixb = pixaDisplay(pixa, 0, 0);
00106     pixWrite("/tmp/otsu-tiled.jpg", pixb, IFF_PNG);
00107     pixDestroy(&pixb);
00108     pixaDestroy(&pixa);
00109 
00110     bmfDestroy(&bmf);
00111     pixDestroy(&pixs);
00112     pixDestroy(&pixg);
00113     return 0;
00114 }
00115 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines