Leptonica 1.68
C Image Processing Library

colormask_reg.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  *   colormask_reg.c
00018  *
00019  *   This tests the ability to identify regions in HSV color space
00020  *   by analyzing the HS histogram and building masks that cover
00021  *   peaks in HS.
00022  */
00023 
00024 #include <math.h>
00025 #ifndef  _WIN32
00026 #include <unistd.h>
00027 #else
00028 #include <windows.h>   /* for Sleep() */
00029 #endif  /* _WIN32 */
00030 #include "allheaders.h"
00031 
00032 
00033 main(int    argc,
00034      char **argv)
00035 {
00036 l_int32       i, j, x, y, rval, gval, bval;
00037 l_uint32      pixel;
00038 l_float32     frval, fgval, fbval;
00039 NUMA         *nahue, *nasat, *napk;
00040 PIX          *pixs, *pixhsv, *pixh, *pixg, *pixf, *pixd;
00041 PIX          *pixr, *pixt1, *pixt2, *pixt3;
00042 PIXA         *pixa, *pixapk;
00043 PTA          *ptapk;
00044 L_REGPARAMS  *rp;
00045 
00046     if (regTestSetup(argc, argv, &rp))
00047         return 1;
00048 
00049         /* Make a graded frame color */
00050     pixs = pixCreate(650, 900, 32);
00051     for (i = 0; i < 900; i++) {
00052         rval = 40 + i / 30;
00053         for (j = 0; j < 650; j++) {
00054             gval = 255 - j / 30;
00055             bval = 70 + j / 30;
00056             composeRGBPixel(rval, gval, bval, &pixel);
00057             pixSetPixel(pixs, j, i, pixel);
00058         }
00059     }
00060             
00061         /* Place an image inside the frame and convert to HSV */
00062     pixt1 = pixRead("1555-3.jpg");
00063     pixt2 = pixScale(pixt1, 0.5, 0.5);
00064     pixRasterop(pixs, 100, 100, 2000, 2000, PIX_SRC, pixt2, 0, 0);
00065     pixDestroy(&pixt1);
00066     pixDestroy(&pixt2);
00067     pixDisplayWithTitle(pixs, 400, 0, "Input image", rp->display);
00068     pixa = pixaCreate(0);
00069     pixhsv = pixConvertRGBToHSV(NULL, pixs);
00070 
00071         /* Work in the HS projection of HSV */
00072     pixh = pixMakeHistoHS(pixhsv, 5, &nahue, &nasat);
00073     pixg = pixMaxDynamicRange(pixh, L_LOG_SCALE);
00074     pixf = pixConvertGrayToFalseColor(pixg, 1.0);
00075     regTestWritePixAndCheck(rp, pixf, IFF_PNG);   /* 0 */
00076     pixDisplayWithTitle(pixf, 100, 0, "False color HS histo", rp->display);
00077     pixaAddPix(pixa, pixs, L_COPY);
00078     pixaAddPix(pixa, pixhsv, L_INSERT);
00079     pixaAddPix(pixa, pixg, L_INSERT);
00080     pixaAddPix(pixa, pixf, L_INSERT);
00081     gplotSimple1(nahue, GPLOT_PNG, "/tmp/junkhue", "Histogram of hue values");
00082 #ifndef  _WIN32
00083     sleep(1);
00084 #else
00085     Sleep(1000);
00086 #endif  /* _WIN32 */
00087     pixt3 = pixRead("/tmp/junkhue.png");
00088     regTestWritePixAndCheck(rp, pixt3, IFF_PNG);  /* 1 */
00089     pixDisplayWithTitle(pixt3, 100, 300, "Histo of hue", rp->display);
00090     pixaAddPix(pixa, pixt3, L_INSERT);
00091     gplotSimple1(nasat, GPLOT_PNG, "/tmp/junksat",
00092                  "Histogram of saturation values");
00093 #ifndef  _WIN32
00094     sleep(1);
00095 #else
00096     Sleep(1000);
00097 #endif  /* _WIN32 */
00098     pixt3 = pixRead("/tmp/junksat.png");
00099     regTestWritePixAndCheck(rp, pixt3, IFF_PNG);  /* 2 */
00100     pixDisplayWithTitle(pixt3, 100, 800, "Histo of saturation", rp->display);
00101     pixaAddPix(pixa, pixt3, L_INSERT);
00102     pixd = pixaDisplayTiledAndScaled(pixa, 32, 270, 7, 0, 30, 3);
00103     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 3 */
00104     pixDisplayWithTitle(pixd, 0, 400, "Hue and Saturation Mosaic", rp->display);
00105     pixDestroy(&pixd);
00106     pixaDestroy(&pixa);
00107     numaDestroy(&nahue);
00108     numaDestroy(&nasat);
00109 
00110         /* Find all the peaks */
00111     pixFindHistoPeaksHSV(pixh, L_HS_HISTO, 20, 20, 6, 2.0,
00112                          &ptapk, &napk, &pixapk);
00113     numaWriteStream(stderr, napk);
00114     ptaWriteStream(stderr, ptapk, 1);
00115     pixd = pixaDisplayTiledInRows(pixapk, 32, 1400, 1.0, 0, 30, 2);
00116     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 4 */
00117     pixDisplayWithTitle(pixd, 0, 550, "Peaks in HS", rp->display);
00118     pixDestroy(&pixh);
00119     pixDestroy(&pixd);
00120     pixaDestroy(&pixapk);
00121 
00122         /* Make masks for each of the peaks */
00123     pixa = pixaCreate(0);
00124     pixr = pixScaleBySampling(pixs, 0.4, 0.4);
00125     for (i = 0; i < 6; i++) {
00126         ptaGetIPt(ptapk, i, &x, &y);
00127         pixt1 = pixMakeRangeMaskHS(pixr, y, 20, x, 20, L_INCLUDE_REGION);
00128         pixaAddPix(pixa, pixt1, L_INSERT);
00129         pixGetAverageMaskedRGB(pixr, pixt1, 0, 0, 1, L_MEAN_ABSVAL,
00130                                &frval, &fgval, &fbval);
00131         composeRGBPixel((l_int32)frval, (l_int32)fgval, (l_int32)fbval,
00132                         &pixel);
00133         pixt2 = pixCreateTemplate(pixr);
00134         pixSetAll(pixt2);
00135         pixPaintThroughMask(pixt2, pixt1, 0, 0, pixel);
00136         pixaAddPix(pixa, pixt2, L_INSERT);
00137         pixt3 = pixCreateTemplate(pixr);
00138         pixSetAllArbitrary(pixt3, pixel);
00139         pixaAddPix(pixa, pixt3, L_INSERT);
00140     }
00141     pixd = pixaDisplayTiledAndScaled(pixa, 32, 225, 3, 0, 30, 3);
00142     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 5 */
00143     pixDisplayWithTitle(pixd, 600, 0, "Masks over peaks", rp->display);
00144     pixDestroy(&pixs);
00145     pixDestroy(&pixr);
00146     pixDestroy(&pixd);
00147     pixaDestroy(&pixa);
00148     ptaDestroy(&ptapk);
00149     numaDestroy(&napk);
00150 
00151     regTestCleanup(rp);
00152     return 0;
00153 }
00154 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines