Leptonica 1.68
C Image Processing Library

projective_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  * projective_reg.c
00018  *
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include "allheaders.h"
00024 
00025 static void MakePtas(l_int32 i, PTA **pptas, PTA **pptad);
00026 
00027     /* Sample values.
00028      *    1-3: invertability tests
00029      *    4: comparison between sampling and sequential
00030      *    5: test with large distortion
00031      */
00032 static const l_int32  x1[] =  { 300,  300,  300,  300,   32};
00033 static const l_int32  y1[] =  {1200, 1200, 1250, 1250,  934};
00034 static const l_int32  x2[] =  {1200, 1200, 1125, 1300,  487};
00035 static const l_int32  y2[] =  {1100, 1100, 1100, 1250,  934};
00036 static const l_int32  x3[] =  { 200,  200,  200,  250,   32};
00037 static const l_int32  y3[] =  { 200,  200,  200,  300,   67};
00038 static const l_int32  x4[] =  {1200, 1200, 1300, 1250,  332};
00039 static const l_int32  y4[] =  { 400,  200,  200,  300,   57};
00040 
00041 static const l_int32  xp1[] = { 300,  300, 1150,  300,   32};
00042 static const l_int32  yp1[] = {1200, 1400, 1150, 1350,  934};
00043 static const l_int32  xp2[] = {1100, 1400,  320, 1300,  487};
00044 static const l_int32  yp2[] = {1000, 1500, 1300, 1200,  904};
00045 static const l_int32  xp3[] = { 250,  200, 1310,  300,   61};
00046 static const l_int32  yp3[] = { 200,  300,  250,  325,   83};
00047 static const l_int32  xp4[] = {1250, 1200,  240, 1250,  412};
00048 static const l_int32  yp4[] = { 300,  300,  250,  350,   83};
00049 
00050 #define   ADDED_BORDER_PIXELS       500
00051 #define   ALL     1
00052 
00053 
00054 main(int    argc,
00055      char **argv)
00056 {
00057 char         bufname[256];
00058 l_int32      i, j, w, h, d, x, y, wpls;
00059 l_uint32    *datas, *lines;
00060 l_float32   *vc;
00061 PIX         *pixs, *pixsc, *pixb, *pixg, *pixc, *pixcs, *pixd;
00062 PIX         *pixt1, *pixt2, *pixt3;
00063 PIXA        *pixa;
00064 PTA         *ptas, *ptad;
00065 static char  mainName[] = "projective_reg";
00066 
00067     if (argc != 1)
00068         exit(ERROR_INT(" Syntax:  projective_reg", mainName, 1));
00069     if ((pixs = pixRead("feyn.tif")) == NULL)
00070         exit(ERROR_INT("pixs not made", mainName, 1));
00071     pixsc = pixScale(pixs, 0.5, 0.5);
00072 
00073 #if ALL
00074         /* Test invertability of sampling */
00075     pixa = pixaCreate(0);
00076     for (i = 0; i < 3; i++) {
00077         pixb = pixAddBorder(pixsc, ADDED_BORDER_PIXELS, 0);
00078         MakePtas(i, &ptas, &ptad);
00079         pixt1 = pixProjectiveSampledPta(pixb, ptad, ptas, L_BRING_IN_WHITE);
00080         pixSaveTiled(pixt1, pixa, 1, 1, 20, 8);
00081         pixt2 = pixProjectiveSampledPta(pixt1, ptas, ptad, L_BRING_IN_WHITE);
00082         pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00083         pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS);
00084         pixXor(pixd, pixd, pixsc);
00085         pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00086         if (i == 0) pixWrite("/tmp/junksamp.png", pixt1, IFF_PNG);
00087         pixDestroy(&pixb);
00088         pixDestroy(&pixt1);
00089         pixDestroy(&pixt2);
00090         pixDestroy(&pixd);
00091         ptaDestroy(&ptas);
00092         ptaDestroy(&ptad);
00093     }
00094 
00095     pixt1 = pixaDisplay(pixa, 0, 0);
00096     pixWrite("/tmp/junkproj1.png", pixt1, IFF_PNG);
00097     pixDisplay(pixt1, 100, 300);
00098     pixDestroy(&pixt1);
00099     pixaDestroy(&pixa);
00100 #endif
00101 
00102 #if ALL
00103         /* Test invertability of interpolation on grayscale */
00104     pixa = pixaCreate(0);
00105     pixg = pixScaleToGray3(pixs);
00106     for (i = 0; i < 3; i++) {
00107         pixb = pixAddBorder(pixg, ADDED_BORDER_PIXELS / 2, 255);
00108         MakePtas(i, &ptas, &ptad);
00109         pixt1 = pixProjectivePta(pixb, ptad, ptas, L_BRING_IN_WHITE);
00110         pixSaveTiled(pixt1, pixa, 1, 1, 20, 8);
00111         pixt2 = pixProjectivePta(pixt1, ptas, ptad, L_BRING_IN_WHITE);
00112         pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00113         pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS / 2);
00114         pixXor(pixd, pixd, pixg);
00115         pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00116         if (i == 0) pixWrite("/tmp/junkinterp.png", pixt1, IFF_PNG);
00117         pixDestroy(&pixb);
00118         pixDestroy(&pixt1);
00119         pixDestroy(&pixt2);
00120         pixDestroy(&pixd);
00121         ptaDestroy(&ptas);
00122         ptaDestroy(&ptad);
00123     }
00124 
00125     pixt1 = pixaDisplay(pixa, 0, 0);
00126     pixWrite("/tmp/junkproj2.png", pixt1, IFF_PNG);
00127     pixDisplay(pixt1, 100, 500);
00128     pixDestroy(&pixt1);
00129     pixaDestroy(&pixa);
00130     pixDestroy(&pixg);
00131 #endif
00132 
00133 #if ALL
00134         /* Test invertability of interpolation on color */
00135     pixa = pixaCreate(0);
00136     pixc = pixRead("test24.jpg");
00137     pixcs = pixScale(pixc, 0.3, 0.3);
00138     for (i = 0; i < 5; i++) {
00139         pixb = pixAddBorder(pixcs, ADDED_BORDER_PIXELS, 0xffffff00);
00140         MakePtas(i, &ptas, &ptad);
00141         pixt1 = pixProjectivePta(pixb, ptad, ptas, L_BRING_IN_WHITE);
00142         pixSaveTiled(pixt1, pixa, 1, 1, 20, 32);
00143         pixt2 = pixProjectivePta(pixt1, ptas, ptad, L_BRING_IN_WHITE);
00144         pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00145         pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS);
00146         pixXor(pixd, pixd, pixcs);
00147         pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00148         pixDestroy(&pixb);
00149         pixDestroy(&pixt1);
00150         pixDestroy(&pixt2);
00151         pixDestroy(&pixd);
00152         ptaDestroy(&ptas);
00153         ptaDestroy(&ptad);
00154     }
00155 
00156     pixt1 = pixaDisplay(pixa, 0, 0);
00157     pixWrite("/tmp/junkproj3.png", pixt1, IFF_PNG);
00158     pixDisplay(pixt1, 100, 500);
00159     pixDestroy(&pixt1);
00160     pixaDestroy(&pixa);
00161     pixDestroy(&pixc);
00162     pixDestroy(&pixcs);
00163 #endif
00164 
00165 #if ALL 
00166        /* Comparison between sampling and interpolated */
00167     MakePtas(3, &ptas, &ptad);
00168     pixa = pixaCreate(0);
00169 
00170         /* Use sampled transform */
00171     pixt1 = pixProjectiveSampledPta(pixs, ptas, ptad, L_BRING_IN_WHITE);
00172     pixSaveTiled(pixt1, pixa, 2, 1, 20, 8);
00173 
00174         /* Use interpolated transforms */
00175     pixt2 = pixProjectivePta(pixs, ptas, ptad, L_BRING_IN_WHITE);
00176     pixSaveTiled(pixt2, pixa, 2, 0, 20, 8);
00177 
00178         /* Compare the results */
00179     pixXor(pixt2, pixt2, pixt1);
00180     pixSaveTiled(pixt2, pixa, 2, 0, 20, 8);
00181 
00182     pixd = pixaDisplay(pixa, 0, 0);
00183     pixWrite("/tmp/junkproj4.png", pixd, IFF_PNG);
00184     pixDisplay(pixd, 100, 700);
00185     pixDestroy(&pixt1);
00186     pixDestroy(&pixt2);
00187     pixDestroy(&pixd);
00188     pixaDestroy(&pixa);
00189     ptaDestroy(&ptas);
00190     ptaDestroy(&ptad);
00191 #endif
00192 
00193 #if ALL
00194        /* Get timings */
00195     MakePtas(4, &ptas, &ptad);
00196     pixa = pixaCreate(0);
00197     pixg = pixScaleToGray3(pixs);
00198 
00199     startTimer();
00200     pixt1 = pixProjectiveSampledPta(pixg, ptas, ptad, L_BRING_IN_WHITE);
00201     fprintf(stderr, " Time for pixProjectiveSampledPta(): %6.2f sec\n", stopTimer());
00202     pixSaveTiled(pixt1, pixa, 1, 1, 20, 8);
00203 
00204     startTimer();
00205     pixt2 = pixProjectivePta(pixg, ptas, ptad, L_BRING_IN_WHITE);
00206     fprintf(stderr, " Time for pixProjectivePta(): %6.2f sec\n", stopTimer());
00207     pixSaveTiled(pixt2, pixa, 1, 0, 20, 8);
00208 
00209     pixXor(pixt1, pixt1, pixt2);
00210     pixSaveTiled(pixt1, pixa, 1, 0, 20, 8);
00211     pixDestroy(&pixt1);
00212     pixDestroy(&pixt2);
00213 
00214     pixd = pixaDisplay(pixa, 0, 0);
00215     pixWrite("/tmp/junkproj5.png", pixd, IFF_PNG);
00216     pixDisplay(pixd, 100, 900);
00217     pixDestroy(&pixd);
00218     pixDestroy(&pixg);
00219     pixaDestroy(&pixa);
00220     ptaDestroy(&ptas);
00221     ptaDestroy(&ptad);
00222 #endif
00223 
00224     pixDestroy(&pixs);
00225     pixDestroy(&pixsc);
00226     return 0;
00227 }
00228 
00229 static void
00230 MakePtas(l_int32  i,
00231          PTA    **pptas,
00232          PTA    **pptad)
00233 {
00234 
00235     *pptas = ptaCreate(4);
00236     ptaAddPt(*pptas, x1[i], y1[i]);
00237     ptaAddPt(*pptas, x2[i], y2[i]);
00238     ptaAddPt(*pptas, x3[i], y3[i]);
00239     ptaAddPt(*pptas, x4[i], y4[i]);
00240     *pptad = ptaCreate(4);
00241     ptaAddPt(*pptad, xp1[i], yp1[i]);
00242     ptaAddPt(*pptad, xp2[i], yp2[i]);
00243     ptaAddPt(*pptad, xp3[i], yp3[i]);
00244     ptaAddPt(*pptad, xp4[i], yp4[i]);
00245     return;
00246 }
00247 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines