Leptonica 1.68
C Image Processing Library

bilinear_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  * bilineartest.c
00018  */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include "allheaders.h"
00023 
00024 static void MakePtas(l_int32 i, PTA **pptas, PTA **pptad);
00025 
00026     /* Sample values.
00027      *    1: test with relatively large distortion
00028      *    2-3: invertability tests
00029      */
00030 static const l_int32  x1[] =  {  32,   32,   32};
00031 static const l_int32  y1[] =  { 150,  150,  150};
00032 static const l_int32  x2[] =  { 520,  520,  520};
00033 static const l_int32  y2[] =  { 150,  150,  150};
00034 static const l_int32  x3[] =  {  32,   32,   32};
00035 static const l_int32  y3[] =  { 612,  612,  612};
00036 static const l_int32  x4[] =  { 520,  520,  520};
00037 static const l_int32  y4[] =  { 612,  612,  612};
00038 
00039 static const l_int32  xp1[] = {  32,   32,   32};
00040 static const l_int32  yp1[] = { 150,  150,  150};
00041 static const l_int32  xp2[] = { 520,  520,  520};
00042 static const l_int32  yp2[] = {  44,  124,  140};
00043 static const l_int32  xp3[] = {  32,   32,   32};
00044 static const l_int32  yp3[] = { 612,  612,  612};
00045 static const l_int32  xp4[] = { 520,  520,  520};
00046 static const l_int32  yp4[] = { 694,  624,  622};
00047 
00048 #define  ALL                        1
00049 #define  ADDED_BORDER_PIXELS      500
00050 
00051 
00052 main(int    argc,
00053      char **argv)
00054 {
00055 l_int32      i, d, h;
00056 l_float32    rat;
00057 PIX         *pixs, *pixgb, *pixt1, *pixt2, *pixt3, *pixt4, *pixg, *pixd;
00058 PIXA        *pixa;
00059 PTA         *ptas, *ptad;
00060 static char  mainName[] = "bilinear_reg";
00061 
00062     if (argc != 1)
00063         exit(ERROR_INT(" Syntax:  bilinear_reg", mainName, 1));
00064 
00065     pixs = pixRead("feyn.tif");
00066     pixg = pixScaleToGray3(pixs);
00067 
00068 #if ALL
00069         /* Test non-invertability of sampling */
00070     pixa = pixaCreate(0);
00071     for (i = 1; i < 3; i++) {
00072         pixgb = pixAddBorder(pixg, ADDED_BORDER_PIXELS, 255);
00073         MakePtas(i, &ptas, &ptad);
00074         pixt1 = pixBilinearSampledPta(pixgb, ptad, ptas, L_BRING_IN_WHITE);
00075         pixSaveTiled(pixt1, pixa, 2, 1, 20, 8);
00076         pixt2 = pixBilinearSampledPta(pixt1, ptas, ptad, L_BRING_IN_WHITE);
00077         pixSaveTiled(pixt2, pixa, 2, 0, 20, 0);
00078         pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS);
00079         pixInvert(pixd, pixd);
00080         pixXor(pixd, pixd, pixg);
00081         pixSaveTiled(pixd, pixa, 2, 0, 20, 0);
00082         if (i == 0) pixWrite("/tmp/junksamp.png", pixt1, IFF_PNG);
00083         pixDestroy(&pixgb);
00084         pixDestroy(&pixt1);
00085         pixDestroy(&pixt2);
00086         pixDestroy(&pixd);
00087         ptaDestroy(&ptas);
00088         ptaDestroy(&ptad);
00089     }
00090 
00091     pixt1 = pixaDisplay(pixa, 0, 0);
00092     pixWrite("/tmp/junkbilin1.png", pixt1, IFF_PNG);
00093     pixDisplay(pixt1, 100, 300);
00094     pixDestroy(&pixt1);
00095     pixaDestroy(&pixa);
00096 #endif
00097 
00098 #if ALL
00099         /* Test non-invertability of interpolation */
00100     pixa = pixaCreate(0);
00101     for (i = 1; i < 3; i++) {
00102         pixgb = pixAddBorder(pixg, ADDED_BORDER_PIXELS, 255);
00103         MakePtas(i, &ptas, &ptad);
00104         pixt1 = pixBilinearPta(pixgb, ptad, ptas, L_BRING_IN_WHITE);
00105         pixSaveTiled(pixt1, pixa, 2, 1, 20, 8);
00106         pixt2 = pixBilinearPta(pixt1, ptas, ptad, L_BRING_IN_WHITE);
00107         pixSaveTiled(pixt2, pixa, 2, 0, 20, 0);
00108         pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS);
00109         pixInvert(pixd, pixd);
00110         pixXor(pixd, pixd, pixg);
00111         pixSaveTiled(pixd, pixa, 2, 0, 20, 0);
00112         if (i == 0) pixWrite("/tmp/junkinterp.png", pixt1, IFF_PNG);
00113         pixDestroy(&pixgb);
00114         pixDestroy(&pixt1);
00115         pixDestroy(&pixt2);
00116         pixDestroy(&pixd);
00117         ptaDestroy(&ptas);
00118         ptaDestroy(&ptad);
00119     }
00120 
00121     pixt1 = pixaDisplay(pixa, 0, 0);
00122     pixWrite("/tmp/junkbilin2.png", pixt1, IFF_PNG);
00123     pixDisplay(pixt1, 100, 300);
00124     pixDestroy(&pixt1);
00125     pixaDestroy(&pixa);
00126 #endif
00127 
00128 #if ALL   /* test with large distortion and inversion */
00129     MakePtas(0, &ptas, &ptad);
00130     pixa = pixaCreate(0);
00131 
00132     startTimer();
00133     pixt1 = pixBilinearSampledPta(pixg, ptas, ptad, L_BRING_IN_WHITE);
00134     fprintf(stderr, " Time for pixBilinearSampled(): %6.2f sec\n", stopTimer());
00135     pixSaveTiled(pixt1, pixa, 2, 1, 20, 8);
00136 
00137     startTimer();
00138     pixt2 = pixBilinearPta(pixg, ptas, ptad, L_BRING_IN_WHITE);
00139     fprintf(stderr, " Time for pixBilinearInterpolated(): %6.2f sec\n",
00140            stopTimer());
00141     pixSaveTiled(pixt2, pixa, 2, 0, 20, 8);
00142 
00143     pixt3 = pixBilinearSampledPta(pixt1, ptad, ptas, L_BRING_IN_WHITE);
00144     pixSaveTiled(pixt3, pixa, 2, 0, 20, 8);
00145     pixt4 = pixBilinearPta(pixt2, ptad, ptas, L_BRING_IN_WHITE);
00146     pixSaveTiled(pixt4, pixa, 2, 0, 20, 8);
00147     pixDestroy(&pixt1);
00148     pixDestroy(&pixt2);
00149     pixDestroy(&pixt3);
00150     pixDestroy(&pixt4);
00151 
00152     pixt1 = pixaDisplay(pixa, 0, 0);
00153     pixWrite("/tmp/junkbilin3.png", pixt1, IFF_PNG);
00154     pixDisplay(pixt1, 100, 300);
00155     pixDestroy(&pixt1);
00156     pixaDestroy(&pixa);
00157 
00158     pixDestroy(&pixs);
00159     pixDestroy(&pixg);
00160     ptaDestroy(&ptas);
00161     ptaDestroy(&ptad);
00162 #endif
00163 
00164     return 0;
00165 }
00166 
00167 
00168 static void
00169 MakePtas(l_int32  i,
00170          PTA    **pptas,
00171          PTA    **pptad)
00172 {
00173     *pptas = ptaCreate(4);
00174     ptaAddPt(*pptas, x1[i], y1[i]);
00175     ptaAddPt(*pptas, x2[i], y2[i]);
00176     ptaAddPt(*pptas, x3[i], y3[i]);
00177     ptaAddPt(*pptas, x4[i], y4[i]);
00178     *pptad = ptaCreate(4);
00179     ptaAddPt(*pptad, xp1[i], yp1[i]);
00180     ptaAddPt(*pptad, xp2[i], yp2[i]);
00181     ptaAddPt(*pptad, xp3[i], yp3[i]);
00182     ptaAddPt(*pptad, xp4[i], yp4[i]);
00183     return;
00184 }
00185 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines