contrib/oxl/osl/osl_chamfer.cxx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_chamfer.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "osl_chamfer.h"
00010 
00011 //: Determines the minimum of five ints.
00012 int osl_Minimum5(int a, int b, int c, int d, int e)
00013 {
00014   if ( (a<=b) && (a<=c) && (a<=d) && (a<=e) )
00015     return 1;
00016   else if ( (b<=c) && (b<=d) && (b<=e) )
00017     return 2;
00018   else if ( (c<=d) && (c<=e) )
00019     return 3;
00020   else if ( d<=e )
00021     return 4;
00022   else
00023     return 5;
00024 }
00025 
00026 //: Determines the minimum of five ints.
00027 int osl_Minimum4(int a, int b, int c, int d)
00028 {
00029   if ( (a<=b) && (a<=c) && (a<=d) )
00030     return 1;
00031   else if ( (b<=c) && (b<=d) )
00032     return 2;
00033   else if ( (c<=d) )
00034     return 3;
00035   else
00036     return 4;
00037 }
00038 
00039 //-----------------------------------------------------------------------------
00040 //
00041 //:
00042 // Performs a forward chamfer convolution on the dist image and associates
00043 // a send image (param) that reports on some parameter of the nearest pixel.
00044 // The image sizes are mxn.
00045 void osl_chamfer_Forward (int m, int n, int **dist, float **param)
00046 {
00047   for (int i=1; i<m-1; ++i) {
00048     for (int j=1; j<n-1; ++j)  {
00049 
00050       int val = osl_Minimum5(dist[i-1][j-1]+4,
00051                              dist[i-1][j  ]+3,
00052                              dist[i-1][j+1]+4,
00053                              dist[i  ][j-1]+3,
00054                              dist[i  ][j  ]);
00055       switch (val) {
00056       case 1:
00057         dist[i][j] = dist[i-1][j-1]+4;
00058         param[i][j] = param[i-1][j-1];
00059         break;
00060 
00061       case 2:
00062         dist[i][j] = dist[i-1][j]+3;
00063         param[i][j] = param[i-1][j];
00064         break;
00065 
00066       case 3:
00067         dist[i][j] = dist[i-1][j+1]+4;
00068         param[i][j] = param[i-1][j+1];
00069         break;
00070 
00071       case 4:
00072         dist[i][j] = dist[i][j-1]+3;
00073         param[i][j] = param[i][j-1];
00074         break;
00075 
00076       case 5:
00077       default:
00078         break;
00079       }
00080     }
00081   }
00082 }
00083 
00084 //: Performs a backward chamfer convolution on the dist and param images.
00085 void osl_chamfer_Backward(int m, int n, int **dist, float **param)
00086 {
00087   for (int i=m-2; i>0; --i) {
00088     for (int j=n-2; j>0; --j)  {
00089 
00090       int val = osl_Minimum5(dist[i  ][j],
00091                              dist[i  ][j+1]+3,
00092                              dist[i+1][j-1]+4,
00093                              dist[i+1][j  ]+3,
00094                              dist[i+1][j+1]+4 );
00095       switch (val) {
00096       case 1:
00097         break;
00098 
00099       case 2:
00100         dist[i][j] = dist[i][j+1]+3;
00101         param[i][j] = param[i][j+1];
00102         break;
00103 
00104       case 3:
00105         dist[i][j] = dist[i+1][j-1]+4;
00106         param[i][j] = param[i+1][j-1];
00107         break;
00108 
00109       case 4:
00110         dist[i][j] = dist[i+1][j]+3;
00111         param[i][j] = param[i+1][j];
00112         break;
00113 
00114       case 5:
00115         dist[i][j] = dist[i+1][j+1]+4;
00116         param[i][j] = param[i+1][j+1];
00117         break;
00118 
00119       default:
00120         break;
00121       }
00122     }
00123   }
00124 }
00125 
00126 //-----------------------------------------------------------------------------
00127 //
00128 //:
00129 // Performs a chamfer convolution starting from (minx,maxy) on the dist image
00130 // and associates a send image (param) that reports on some parameter of the
00131 // nearest pixel. The image sizes are mxn.
00132 //
00133 void osl_chamfer_Alt1(int m, int n, int **dist, float **param)
00134 {
00135   for (int i=1; i<m-1; ++i) {
00136     for (int j=n-2; j>0; --j)  {
00137 
00138       int val = osl_Minimum5(dist[i-1][j+1]+4,
00139                              dist[i-1][j]+3,
00140                              dist[i-1][j-1]+4,
00141                              dist[i  ][j+1]+3,
00142                              dist[i  ][j  ]);
00143       switch (val) {
00144       case 1:
00145         dist[i][j] = dist[i-1][j+1]+4;
00146         param[i][j] = param[i-1][j+1];
00147         break;
00148 
00149       case 2:
00150         dist[i][j] = dist[i-1][j]+3;
00151         param[i][j] = param[i-1][j];
00152         break;
00153 
00154       case 3:
00155         dist[i][j] = dist[i-1][j-1]+4;
00156         param[i][j] = param[i-1][j-1];
00157         break;
00158 
00159       case 4:
00160         dist[i][j] = dist[i][j+1]+3;
00161         param[i][j] = param[i][j+1];
00162         break;
00163 
00164       case 5:
00165       default:
00166         break;
00167       }
00168     }
00169   }
00170 }
00171 
00172 
00173 //-----------------------------------------------------------------------------
00174 //
00175 //:
00176 // Performs a chamfer convolution starting from (maxx,miny) on the dist image
00177 // and associates a send image (param) that reports on some parameter of the
00178 // nearest pixel. The image sizes are mxn.
00179 //
00180 void osl_chamfer_Alt2(int m, int n, int **dist, float **param)
00181 {
00182   for (int i=m-2; i>0; --i) {
00183     for (int j=1; j<n-1; ++j)
00184     {
00185       int val = osl_Minimum5(dist[i  ][j  ],
00186                              dist[i  ][j+1]+3,
00187                              dist[i+1][j-1]+4,
00188                              dist[i+1][j  ]+3,
00189                              dist[i+1][j+1]+4 );
00190       switch (val) {
00191       case 1:
00192         break;
00193 
00194       case 2:
00195         dist[i][j] = dist[i][j+1]+3;
00196         param[i][j] = param[i][j+1];
00197         break;
00198 
00199       case 3:
00200         dist[i][j] = dist[i+1][j-1]+4;
00201         param[i][j] = param[i+1][j-1];
00202         break;
00203 
00204       case 4:
00205         dist[i][j] = dist[i+1][j]+3;
00206         param[i][j] = param[i+1][j];
00207         break;
00208 
00209       case 5:
00210         dist[i][j] = dist[i+1][j+1]+4;
00211         param[i][j] = param[i+1][j+1];
00212         break;
00213 
00214       default:
00215         break;
00216       }
00217     }
00218   }
00219 }