Leptonica 1.68
C Image Processing Library
|
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 * colorspacetest.c 00018 * 00019 */ 00020 00021 #include <stdio.h> 00022 #include <stdlib.h> 00023 #include "allheaders.h" 00024 00025 00026 main(int argc, 00027 char **argv) 00028 { 00029 char label[512]; 00030 l_int32 w, h, i, j, rwhite, gwhite, bwhite, count; 00031 GPLOT *gplot1, *gplot2; 00032 NUMA *naseq, *na; 00033 NUMAA *naa1, *naa2; 00034 PIX *pixs, *pixt, *pixt0, *pixt1, *pixt2; 00035 PIX *pixr, *pixg, *pixb; /* for color content extraction */ 00036 PIXA *pixa; 00037 PIXCMAP *cmap; 00038 static char mainName[] = "colorspacetest"; 00039 00040 if (argc != 2) 00041 exit(ERROR_INT(" Syntax: colorspacetest filein", mainName, 1)); 00042 00043 if ((pixs = pixRead(argv[1])) == NULL) 00044 exit(ERROR_INT("pixs not made", mainName, 1)); 00045 00046 /* Colorspace conversion in rgb */ 00047 pixDisplayWrite(pixs, 1); 00048 pixt = pixConvertRGBToHSV(NULL, pixs); 00049 pixDisplayWrite(pixt, 1); 00050 pixConvertHSVToRGB(pixt, pixt); 00051 pixDisplayWrite(pixt, 1); 00052 pixDestroy(&pixt); 00053 00054 /* Colorspace conversion on a colormap */ 00055 pixt = pixOctreeQuantNumColors(pixs, 25, 0); 00056 pixDisplayWrite(pixt, 1); 00057 cmap = pixGetColormap(pixt); 00058 pixcmapWriteStream(stderr, cmap); 00059 pixcmapConvertRGBToHSV(cmap); 00060 pixcmapWriteStream(stderr, cmap); 00061 pixDisplayWrite(pixt, 1); 00062 pixcmapConvertHSVToRGB(cmap); 00063 pixcmapWriteStream(stderr, cmap); 00064 pixDisplayWrite(pixt, 1); 00065 pixDestroy(&pixt); 00066 00067 /* Color content extraction */ 00068 pixColorContent(pixs, 0, 0, 0, 0, &pixr, &pixg, &pixb); 00069 pixDisplayWrite(pixr, 1); 00070 pixDisplayWrite(pixg, 1); 00071 pixDisplayWrite(pixb, 1); 00072 pixDestroy(&pixr); 00073 pixDestroy(&pixg); 00074 pixDestroy(&pixb); 00075 00076 /* Color content measurement */ 00077 pixa = pixaCreate(20); 00078 naseq = numaMakeSequence(100, 5, 20); 00079 naa1 = numaaCreate(6); 00080 naa2 = numaaCreate(6); 00081 for (i = 0; i < 6; i++) { 00082 na = numaCreate(20); 00083 numaaAddNuma(naa1, na, L_COPY); 00084 numaaAddNuma(naa2, na, L_INSERT); 00085 } 00086 pixGetDimensions(pixs, &w, &h, NULL); 00087 for (i = 0; i < 20; i++) { 00088 rwhite = 100 + 5 * i; 00089 gwhite = 200 - 5 * i; 00090 bwhite = 150; 00091 pixt0 = pixGlobalNormRGB(NULL, pixs, rwhite, gwhite, bwhite, 255); 00092 pixaAddPix(pixa, pixt0, L_INSERT); 00093 pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, 00094 L_MAX_DIFF_FROM_AVERAGE_2); 00095 for (j = 0; j < 6; j++) { 00096 pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); 00097 pixInvert(pixt2, pixt2); 00098 pixCountPixels(pixt2, &count, NULL); 00099 na = numaaGetNuma(naa1, j, L_CLONE); 00100 numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); 00101 numaDestroy(&na); 00102 pixDestroy(&pixt2); 00103 } 00104 pixDestroy(&pixt1); 00105 pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, 00106 L_MAX_MIN_DIFF_FROM_2); 00107 for (j = 0; j < 6; j++) { 00108 pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); 00109 pixInvert(pixt2, pixt2); 00110 pixCountPixels(pixt2, &count, NULL); 00111 na = numaaGetNuma(naa2, j, L_CLONE); 00112 numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); 00113 numaDestroy(&na); 00114 pixDestroy(&pixt2); 00115 } 00116 pixDestroy(&pixt1); 00117 } 00118 gplot1 = gplotCreate("/tmp/junkplot1", GPLOT_X11, 00119 "Fraction with given color (diff from average)", 00120 "white point space for red", "amount of color"); 00121 gplot2 = gplotCreate("/tmp/junkplot2", GPLOT_X11, 00122 "Fraction with given color (min diff)", 00123 "white point space for red", "amount of color"); 00124 for (j = 0; j < 6; j++) { 00125 na = numaaGetNuma(naa1, j, L_CLONE); 00126 sprintf(label, "thresh %d", 30 + 10 * j); 00127 gplotAddPlot(gplot1, naseq, na, GPLOT_LINES, label); 00128 numaDestroy(&na); 00129 na = numaaGetNuma(naa2, j, L_CLONE); 00130 gplotAddPlot(gplot2, naseq, na, GPLOT_LINES, label); 00131 numaDestroy(&na); 00132 } 00133 gplotMakeOutput(gplot1); 00134 gplotMakeOutput(gplot2); 00135 gplotDestroy(&gplot1); 00136 gplotDestroy(&gplot2); 00137 pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 250, 4, 0, 10, 2); 00138 pixWrite("/tmp/junkcolormag", pixt1, IFF_PNG); 00139 pixDisplayWithTitle(pixt1, 0, 100, "Color magnitude", 1); 00140 pixDestroy(&pixt1); 00141 pixaDestroy(&pixa); 00142 numaDestroy(&naseq); 00143 numaaDestroy(&naa1); 00144 numaaDestroy(&naa2); 00145 00146 pixDisplayMultiple("/tmp/junk_write_display*"); 00147 00148 pixDestroy(&pixs); 00149 return 0; 00150 } 00151