source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Inference/SupRayAlign.cpp @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 14.5 KB
Line 
1/*
2% *  This code was used in the following articles:
3% *  [1] Learning 3-D Scene Structure from a Single Still Image,
4% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
5% *      In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
6% *      (best paper)
7% *  [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
8% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
9% *      In ICCV workshop on Virtual Representations and Modeling
10% *      of Large-scale environments (VRML), 2007.
11% *  [3] 3-D Depth Reconstruction from a Single Still Image,
12% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
13% *      International Journal of Computer Vision (IJCV), Aug 2007.
14% *  [6] Learning Depth from Single Monocular Images,
15% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
16% *      In Neural Information Processing Systems (NIPS) 18, 2005.
17% *
18% *  These articles are available at:
19% *  http://make3d.stanford.edu/publications
20% *
21% *  We request that you cite the papers [1], [3] and [6] in any of
22% *  your reports that uses this code.
23% *  Further, if you use the code in image3dstiching/ (multiple image version),
24% *  then please cite [2].
25% * 
26% *  If you use the code in third_party/, then PLEASE CITE and follow the
27% *  LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
28% *
29% *  Finally, this code is for non-commercial use only.  For further
30% *  information and to obtain a copy of the license, see
31% *
32% *  http://make3d.stanford.edu/publications/code
33% *
34% *  Also, the software distributed under the License is distributed on an
35% * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
36% *  express or implied.   See the License for the specific language governing
37% *  permissions and limitations under the License.
38% *
39% */
40
41//#include <stdio.h>
42#include <cstdlib>
43#include <math.h>
44#include "mex.h"
45
46/* Input Arguments Total 4 input argument*/
47#define SUP_IN      prhs[0]
48#define VBOUNDARY_IN      prhs[1]
49#define HBOUNDARY_IN      prhs[2]
50#define STRAIGHLINE_IN             prhs[3]
51#define TEXCOORX_IN             prhs[4]
52#define TEXCOORY_IN             prhs[5]
53#define SUPCOUNT_IN             prhs[6]
54
55/* OutPUT Arguments */
56#define TEXCOOR_OUT        plhs[0]
57
58/* Definition */
59#if !defined(MAX)
60#define MAX(A, B)       ((A) > (B) ? (A) : (B))
61#endif
62#if !defined(MIN)
63#define MIN(A, B)       ((A) > (B) ? (B) : (A))
64#endif
65
66
67static void SupRayAlign( double Sup[],
68                double VBoundary[],
69                double HBoundary[],
70                double SupCountIn[],
71                double StraightLine[],
72                double TexCoorXOri[],
73                double TexCoorYOri[], 
74                double TexCoorOut[],
75                unsigned int VSup,
76                unsigned int HSup,
77                unsigned int VB,
78                unsigned int HB,
79                unsigned int NuStr)
80{
81
82 int MaxSup = (VSup*HSup); 
83 int *SupMovedBook = new int[MaxSup];
84 for (int j = 0; j<HSup; j++){
85     int Shift = j*VSup;
86     for ( int i=0; i<VSup; i++){
87         SupMovedBook[i+Shift] = 0; // all value set to zero
88     }
89 }
90
91 /* copy TexCoorOri to TexCoorOut*/
92 for ( int j = 0; j<HSup; j++){
93     int Shift = j*VSup;
94     for ( int i=0; i<VSup; i++){
95         TexCoorOut[i+Shift] = TexCoorXOri[i+Shift]; // all value set to zero
96//         printf("%f\n",TexCoorOut[i+Shift]);
97         TexCoorOut[i+Shift+MaxSup] = TexCoorYOri[i+Shift]; // all value set to zero
98     }
99 } //Checked Coreect copy
100
101  /* for each straight line stitch two rays*/
102  for ( int i= 0; i < NuStr; i++){
103
104     int HStart, HEnd;
105     double Slope;
106     double X1 = StraightLine[i];
107     double Y1 = StraightLine[i+NuStr];
108     double X2 = StraightLine[i+NuStr*2];
109     double Y2 = StraightLine[i+NuStr*3];
110     Slope = ( Y1 - Y2)/( X1 -X2);
111//     printf(" %f %f\n", X1, X2);
112     if ( X1 > X2){
113        HStart = (int) ceil( X2);
114        HEnd = (int) floor( X1);
115     }
116     else{ 
117        HEnd = (int) floor( X2);
118        HStart = (int) ceil( X1);
119     }
120     if (HStart > HEnd){ // exlude case when line didn't pass index
121        continue;
122     }
123     if (HStart <1 | HStart > HSup |
124         HEnd <1 | HEnd > HSup){
125        printf("HEnd %d HStart %d",HEnd,HStart);
126        printf("X1 %f X2 %f\n",X1,X2);
127     }
128 //    printf("%d %d \n", HEnd, HStart);
129 
130     /* bilding up Vertical Stitch */
131     for ( int k = HStart; k<=HEnd; k++){
132         int kMinusOne = k -1;
133         if (HStart == HEnd){
134             break; // invalid vertical stitch for vertical line
135         }
136         double TarStitch = Slope*(k -X1) + Y1;
137         int DownStitch = MIN( (int) ceil( TarStitch), VSup) -1;
138         int UpStitch = MAX(DownStitch - 1,1) -1;
139         if (TarStitch > VSup+0.5 | TarStitch <= 0.5){
140            printf(" VTarStitch = %f k %d X1 %f Y1 %f X2 %f Y2 %f\n", TarStitch,k,X1,Y1,X2,Y2);
141         }
142   
143         if (SupMovedBook[DownStitch + VSup*kMinusOne  ] !=1 ){ 
144            TexCoorOut[ DownStitch + VSup*kMinusOne + MaxSup ] = TarStitch;         
145            SupMovedBook[DownStitch + VSup*kMinusOne ] = 1;         
146//            printf(" TexCoorYOri %f\n", TexCoorYOri[ DownStitch + VSup*k ]);
147         }
148         if (SupMovedBook[UpStitch + VSup*kMinusOne ] !=1 ){ 
149            TexCoorOut[ UpStitch + VSup*kMinusOne + MaxSup ] = TarStitch;         
150            SupMovedBook[UpStitch + VSup*kMinusOne ] = 1;         
151//            printf(" TexCoorYOri %f\n", TexCoorYOri[ DownStitch + VSup*k ]);
152         }
153     }
154
155
156     int VStart, VEnd;
157     if ( Y1 > Y2){
158        VStart = (int) ceil( Y2);
159        VEnd = (int) floor( Y1);
160     }
161     else{ 
162        VEnd = (int) floor( Y2);
163        VStart = (int) ceil( Y1);
164     }
165     if (VStart > VEnd){
166        continue;
167     }
168     if (VStart <1 | VStart > VSup |
169         VEnd <1 | VEnd > VSup){
170        printf("VEnd %d VStart %d",VEnd,VStart);
171        printf("Y1 %f Y2 %f\n",Y1,Y2);
172     }
173//     printf("%d %d \n", VEnd, VStart);
174     /* bilding up Horizontal Stitch */
175   for ( int k = VStart; k<=VEnd; k++){
176         if (VStart == VEnd){
177             break; // invalid vertical stitch for vertical line
178         }
179         double TarStitch = 1/Slope*( k - Y1) + X1;
180         int LeftStitch = MAX( (int) floor( TarStitch),1) - 1;
181         int RightStitch = MIN( LeftStitch + 1, HSup) - 1;
182         if (TarStitch > HSup+0.5 | TarStitch <= 0.5){
183            printf(" HTarStitch = %f k %d X1 %f Y1 %f X2 %f Y2 %f\n", TarStitch,k,X1,Y1,X2,Y2);
184         }
185   
186         if (SupMovedBook[LeftStitch*VSup + k-1] !=1 ){ 
187            TexCoorOut[ LeftStitch*VSup + k-1] = TarStitch;         
188            SupMovedBook[LeftStitch*VSup + k-1] = 1;         
189         }
190         if (SupMovedBook[RightStitch*VSup + k-1] !=1 ){ 
191             TexCoorOut[ RightStitch*VSup + k-1 ] = TarStitch;         
192             SupMovedBook[RightStitch*VSup + k-1] = 1;         
193         }
194     }
195
196  }
197
198
199  /*  Superpixel Vertical boundary stitching */
200 for (int k = 0; k < HSup; k++){
201     int Shift = VSup*k;
202     int ShiftVB = VB*k;
203     for ( int i = 0; i < (VSup - 1); i++){
204         if ( Sup[ i + Shift] != Sup[ i + 1 + Shift ] &&
205              ( SupMovedBook[i + Shift ] !=1 ||
206                SupMovedBook[i + Shift + 1 ] !=1)
207              ){
208            int UpVB = (int) ceil( (i +0.5)*((double)VB/(double)VSup)-0.5 );
209            int DownVB = (int) floor( (i +1.5)*((double)VB/(double)VSup)-0.5);
210//            printf("UpVB %d DownVB %d",UpVB,DownVB);
211//            if ( SupCountIn[i + Shift] > SupCountIn[i + Shift + 1]){
212               for ( int j = UpVB; j<= DownVB; j++){
213//                  printf("j %d",j);
214                     if ( VBoundary[j + ShiftVB] != VBoundary[ j + ShiftVB + 1]){
215                          double TarStitch = (j + 0.5)*((double)VSup/(double)VB) - 0.5 +1;
216//                          printf("%d %f",j,TarStitch);
217                          if (SupMovedBook[i + Shift ] !=1){
218                             TexCoorOut[ i + Shift + MaxSup ] = TarStitch;
219                             SupMovedBook[i + Shift ] = 1;
220                          }
221                         /* if (SupMovedBook[i + Shift + 1] !=1){
222                              TexCoorOut[ i + Shift + 1 + MaxSup ] = TarStitch;
223                              SupMovedBook[i + Shift +1 ] = 1;
224                          }*/
225                     }
226               }
227  //          }
228 //           else{
229            if ( SupCountIn[i + Shift] > SupCountIn[i + Shift + 2]){
230               for ( int j = DownVB; j>= UpVB; j--){
231//                  printf("j %d",j);
232                     if ( VBoundary[j + ShiftVB] != VBoundary[ j + ShiftVB + 1]){
233//                          printf("j %d",j);
234                          double TarStitch = (j + 0.5)*((double)VSup/(double)VB) - 0.5 + 1;
235//                          printf("%d %f",j,TarStitch);
236                       /*   if (SupMovedBook[i + Shift ] !=1){
237                              TexCoorOut[ i + Shift + MaxSup ] = TarStitch;
238                              SupMovedBook[i + Shift ] = 1;
239                          }*/
240                          if (SupMovedBook[i + Shift + 1 ] !=1){
241                              TexCoorOut[ i + Shift + 1 + MaxSup ] = TarStitch;
242                              SupMovedBook[i + Shift +1 ] = 1;
243                          }
244                     }
245               }
246            }
247
248         }
249
250     }
251 }
252 
253 
254  /*  Superpixel Horizontal boundary stitching */
255 for (int k = 0; k < (HSup-2); k++){
256     int Shift = VSup*k;
257     int ShiftHB = VSup*k;
258     for ( int i = 0; i < (VSup - 1); i++){
259         if ( Sup[ i + Shift] != Sup[ i + VSup + Shift ] &&
260              ( SupMovedBook[i + Shift ] !=1 ||
261                SupMovedBook[i + Shift + VSup ] !=1 )
262              ){
263            int LeftVB = (int) ceil( (k +0.5)*((double)HB/(double)HSup)-0.5 );
264            int RightVB = (int) floor( (k +1.5)*((double)HB/(double)HSup)-0.5);
265//            printf("LeftVB %d RightVB %d",LeftVB,RightVB);
266//            if ( SupCountIn[i + Shift] > SupCountIn[i + Shift + VSup]){
267               for ( int j = LeftVB; j<= RightVB; j++){
268                     if ( HBoundary[j*VSup + i] != HBoundary[ j*VSup + i + VSup]){
269                          double TarStitch = (j + 0.5)*( (double)HSup/(double)HB) - 0.5 +1;
270    //                      printf("%d %f",j,TarStitch);
271                          if (SupMovedBook[i + Shift ] !=1){
272                              TexCoorOut[ i + Shift  ] = TarStitch + 1;
273                              SupMovedBook[i + Shift ] = 1;
274                          }
275                     /*     if (SupMovedBook[i + Shift +VSup] !=1){
276                              TexCoorOut[ i + Shift + VSup  ] = TarStitch + 1;
277                              SupMovedBook[i + Shift + VSup ] = 1;
278                          }*/
279                     }
280               }
281  //          }
282//            else{
283            if ( SupCountIn[i + Shift] > SupCountIn[i + Shift + VSup*2]){
284               for ( int j = RightVB; j>= LeftVB; j--){
285                     if ( HBoundary[j*VSup + i] != HBoundary[ j*VSup + i + VSup]){
286                          double TarStitch = (j + 0.5)*((double)HSup/ (double)HB) - 0.5 +1;
287  //                        printf("%d %f\n",j,TarStitch);
288                         /* if (SupMovedBook[i + Shift ] !=1){
289                              TexCoorOut[ i + Shift  ] = TarStitch + 1;
290                              SupMovedBook[i + Shift ] = 1;
291                          }*/
292                          if (SupMovedBook[i + Shift +VSup] !=1){
293                              TexCoorOut[ i + Shift + VSup  ] = TarStitch + 1;
294                              SupMovedBook[i + Shift + VSup ] = 1; 
295                          }
296                     }
297               }
298            }
299
300         }
301
302     }
303 }
304// printf("HB %d HSup %d",HB,HSup);
305 delete [] SupMovedBook;
306 return;
307}
308
309/* mexFunctin (matlab interface will be removed later) */
310void mexFunction( int nlhs, mxArray *plhs[],
311                  int nrhs, const mxArray *prhs[] )
312{
313    double   *Sup;
314    double   *VBoundary;
315    double   *HBoundary;
316    double   *StraightLine;
317    double   *TexCoorXOri;
318    double   *TexCoorYOri;
319    double   *TexCoorOut;
320    double   *SupCountIn;
321
322    unsigned int VSup, HSup, VB, HB, NuStr, v, h;
323
324    /* Check for proper number of arguments */
325
326    if (nrhs != 7) {
327        mexErrMsgTxt("Seven input arguments required.");
328    } else if (nlhs > 1) {
329        mexErrMsgTxt("Too many output arguments.");
330    }
331
332    /* Check the dimensions of the inputs. */
333
334    VSup = mxGetM(SUP_IN);
335    HSup = mxGetN(SUP_IN);
336    if (!mxIsDouble(SUP_IN) || mxIsComplex(SUP_IN) ) {
337        mexErrMsgTxt("The Data Type of Sup shouldn't be complex.");
338    }
339    VB = mxGetM(VBOUNDARY_IN);
340    h = mxGetN(VBOUNDARY_IN);
341    if (!mxIsDouble(VBOUNDARY_IN) || mxIsComplex(VBOUNDARY_IN) ||
342        (h != HSup ) ) {
343        mexErrMsgTxt("The Data Type of VBoundary shouldn't be complex.");
344    }
345    v = mxGetM(HBOUNDARY_IN);
346    HB = mxGetN(HBOUNDARY_IN);
347    if (!mxIsDouble(HBOUNDARY_IN) || mxIsComplex(HBOUNDARY_IN) ||
348        (v != VSup)) {
349        mexErrMsgTxt("The Data Type of HBoundary shouldn't be complex.");
350    }
351    NuStr = mxGetM(STRAIGHLINE_IN);
352    h = mxGetN(STRAIGHLINE_IN);
353    if (!mxIsDouble(STRAIGHLINE_IN) || mxIsComplex(STRAIGHLINE_IN) ||
354        (h != 4) ) {
355        mexErrMsgTxt("StraighLine matrix should always has 4 columns.");
356    }
357    v = mxGetM( TEXCOORX_IN);
358    h = mxGetN( TEXCOORX_IN);
359    if (!mxIsDouble( TEXCOORX_IN) || mxIsComplex( TEXCOORX_IN) ||
360        (v != VSup) || (h != HSup)) {
361        mexErrMsgTxt("TextCoorX matrix should is wrong.");
362    }
363    v = mxGetM( TEXCOORY_IN);
364    h = mxGetN( TEXCOORY_IN);
365    if (!mxIsDouble( TEXCOORY_IN) || mxIsComplex( TEXCOORY_IN) ||
366        (v != VSup) || (h != HSup) ) {
367        mexErrMsgTxt("TextCoorY matrix should is wrong.");
368    }
369    v = mxGetM( SUPCOUNT_IN);
370    h = mxGetN( SUPCOUNT_IN);
371    if (!mxIsDouble( SUPCOUNT_IN) || mxIsComplex( SUPCOUNT_IN) ||
372        (v != VSup) || (h != HSup) ) {
373        mexErrMsgTxt("SUPCOUNT_IN matrix should is wrong.");
374    }
375
376    /* Create a matrix for the return argument */
377    int DIM[3];
378    DIM[0] = VSup;
379    DIM[1] = HSup;
380    DIM[2] = 2;
381    TEXCOOR_OUT = mxCreateNumericArray( 3, DIM, mxDOUBLE_CLASS, mxREAL);
382    TexCoorOut = mxGetPr(TEXCOOR_OUT);
383
384    /* Assign pointers to the various parameters */
385    Sup = mxGetPr( SUP_IN);
386    VBoundary = mxGetPr( VBOUNDARY_IN);
387    HBoundary = mxGetPr( HBOUNDARY_IN);
388    StraightLine =  mxGetPr( STRAIGHLINE_IN);
389    TexCoorXOri =  mxGetPr( TEXCOORX_IN);
390    TexCoorYOri =  mxGetPr( TEXCOORY_IN);
391    SupCountIn =  mxGetPr( SUPCOUNT_IN);
392
393    /* Do the actual computations in a subroutine */
394    SupRayAlign( Sup, VBoundary, HBoundary, SupCountIn, StraightLine, TexCoorXOri, TexCoorYOri, TexCoorOut, VSup, HSup, VB, HB, NuStr);
395    return;
396
397}
398
399
400
Note: See TracBrowser for help on using the repository browser.