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 BOUNDARY_IN prhs[1] |
---|
49 | #define STRAIGHLINE_IN prhs[2] |
---|
50 | #define TEXCOORX_IN prhs[3] |
---|
51 | #define TEXCOORY_IN prhs[4] |
---|
52 | |
---|
53 | /* OutPUT Arguments */ |
---|
54 | #define TEXCOOR_OUT plhs[0] |
---|
55 | |
---|
56 | /* Definition */ |
---|
57 | #if !defined(MAX) |
---|
58 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
---|
59 | #endif |
---|
60 | #if !defined(MIN) |
---|
61 | #define MIN(A, B) ((A) > (B) ? (B) : (A)) |
---|
62 | #endif |
---|
63 | |
---|
64 | |
---|
65 | static void SupRayAlign( double Sup[], |
---|
66 | double Boundary[], |
---|
67 | double StraightLine[], |
---|
68 | double TexCoorXOri[], |
---|
69 | double TexCoorYOri[], |
---|
70 | double TexCoorOut[], |
---|
71 | unsigned int VSup, |
---|
72 | unsigned int HSup, |
---|
73 | unsigned int VB, |
---|
74 | unsigned int HB, |
---|
75 | unsigned int NuStr) |
---|
76 | { |
---|
77 | |
---|
78 | int MaxSup = (VSup*HSup); |
---|
79 | int *SupMovedBook = new int[MaxSup]; |
---|
80 | for (int j = 0; j<HSup; j++){ |
---|
81 | int Shift = j*VSup; |
---|
82 | for ( int i=0; i<VSup; i++){ |
---|
83 | SupMovedBook[i+Shift] = 0; // all value set to zero |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /* copy TexCoorOru to TexCoorOut*/ |
---|
88 | for ( int j = 0; j<HSup; j++){ |
---|
89 | int Shift = j*VSup; |
---|
90 | for ( int i=0; i<VSup; i++){ |
---|
91 | TexCoorOut[i+Shift] = TexCoorXOri[i+Shift]; // all value set to zero |
---|
92 | // printf("%f\n",TexCoorOut[i+Shift]); |
---|
93 | TexCoorOut[i+Shift+MaxSup] = TexCoorYOri[i+Shift]; // all value set to zero |
---|
94 | } |
---|
95 | } //Checked Coreect copy |
---|
96 | |
---|
97 | /* for each straight line stitch two rays*/ |
---|
98 | for ( int i= 0; i < NuStr; i++){ |
---|
99 | |
---|
100 | int HStart, HEnd; |
---|
101 | double Slope; |
---|
102 | double X1 = StraightLine[i]; |
---|
103 | double Y1 = StraightLine[i+NuStr]; |
---|
104 | double X2 = StraightLine[i+NuStr*2]; |
---|
105 | double Y2 = StraightLine[i+NuStr*3]; |
---|
106 | Slope = ( Y1 - Y2)/( X1 -X2); |
---|
107 | // printf(" %f %f\n", X1, X2); |
---|
108 | if ( X1 > X2){ |
---|
109 | HStart = (int) ceil( X2); |
---|
110 | HEnd = (int) floor( X1); |
---|
111 | } |
---|
112 | else{ |
---|
113 | HEnd = (int) floor( X2); |
---|
114 | HStart = (int) ceil( X1); |
---|
115 | } |
---|
116 | if (HStart > HEnd){ // exlude case when line didn't pass index |
---|
117 | continue; |
---|
118 | } |
---|
119 | if (HStart <1 | HStart > HSup | |
---|
120 | HEnd <1 | HEnd > HSup){ |
---|
121 | printf("HEnd %d HStart %d",HEnd,HStart); |
---|
122 | printf("X1 %f X2 %f\n",X1,X2); |
---|
123 | } |
---|
124 | // printf("%d %d \n", HEnd, HStart); |
---|
125 | |
---|
126 | /* bilding up Vertical Stitch */ |
---|
127 | for ( int k = HStart; k<=HEnd; k++){ |
---|
128 | int kMinusOne = k -1; |
---|
129 | if (HStart == HEnd){ |
---|
130 | break; // invalid vertical stitch for vertical line |
---|
131 | } |
---|
132 | double TarStitch = Slope*(k -X1) + Y1; |
---|
133 | int DownStitch = MIN( (int) ceil( TarStitch), VSup) -1; |
---|
134 | int UpStitch = MAX(DownStitch - 1,1) -1; |
---|
135 | if (TarStitch > VSup+0.5 | TarStitch <= 0.5){ |
---|
136 | printf(" VTarStitch = %f k %d X1 %f Y1 %f X2 %f Y2 %f\n", TarStitch,k,X1,Y1,X2,Y2); |
---|
137 | } |
---|
138 | |
---|
139 | if (SupMovedBook[DownStitch + VSup*kMinusOne ] !=1 ){ |
---|
140 | TexCoorOut[ DownStitch + VSup*kMinusOne + MaxSup ] = TarStitch; |
---|
141 | SupMovedBook[DownStitch + VSup*kMinusOne ] = 1; |
---|
142 | // printf(" TexCoorYOri %f\n", TexCoorYOri[ DownStitch + VSup*k ]); |
---|
143 | } |
---|
144 | if (SupMovedBook[UpStitch + VSup*kMinusOne ] !=1 ){ |
---|
145 | TexCoorOut[ UpStitch + VSup*kMinusOne + MaxSup ] = TarStitch; |
---|
146 | SupMovedBook[UpStitch + VSup*kMinusOne ] = 1; |
---|
147 | // printf(" TexCoorYOri %f\n", TexCoorYOri[ DownStitch + VSup*k ]); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | int VStart, VEnd; |
---|
153 | if ( Y1 > Y2){ |
---|
154 | VStart = (int) ceil( Y2); |
---|
155 | VEnd = (int) floor( Y1); |
---|
156 | } |
---|
157 | else{ |
---|
158 | VEnd = (int) floor( Y2); |
---|
159 | VStart = (int) ceil( Y1); |
---|
160 | } |
---|
161 | if (VStart > VEnd){ |
---|
162 | continue; |
---|
163 | } |
---|
164 | if (VStart <1 | VStart > VSup | |
---|
165 | VEnd <1 | VEnd > VSup){ |
---|
166 | printf("VEnd %d VStart %d",VEnd,VStart); |
---|
167 | printf("Y1 %f Y2 %f\n",Y1,Y2); |
---|
168 | } |
---|
169 | // printf("%d %d \n", VEnd, VStart); |
---|
170 | /* bilding up Horizontal Stitch */ |
---|
171 | for ( int k = VStart; k<=VEnd; k++){ |
---|
172 | if (VStart == VEnd){ |
---|
173 | break; // invalid vertical stitch for vertical line |
---|
174 | } |
---|
175 | double TarStitch = 1/Slope*( k - Y1) + X1; |
---|
176 | int LeftStitch = MAX( (int) floor( TarStitch),1) - 1; |
---|
177 | int RightStitch = MIN( LeftStitch + 1, HSup) - 1; |
---|
178 | if (TarStitch > HSup+0.5 | TarStitch <= 0.5){ |
---|
179 | printf(" HTarStitch = %f k %d X1 %f Y1 %f X2 %f Y2 %f\n", TarStitch,k,X1,Y1,X2,Y2); |
---|
180 | } |
---|
181 | |
---|
182 | if (SupMovedBook[LeftStitch*VSup + k-1] !=1 ){ |
---|
183 | TexCoorOut[ LeftStitch*VSup + k-1] = TarStitch; |
---|
184 | SupMovedBook[LeftStitch*VSup + k-1] = 1; |
---|
185 | } |
---|
186 | if (SupMovedBook[RightStitch*VSup + k-1] !=1 ){ |
---|
187 | TexCoorOut[ RightStitch*VSup + k-1 ] = TarStitch; |
---|
188 | SupMovedBook[RightStitch*VSup + k-1] = 1; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | delete [] SupMovedBook; |
---|
195 | return; |
---|
196 | } |
---|
197 | |
---|
198 | /* mexFunctin (matlab interface will be removed later) */ |
---|
199 | void mexFunction( int nlhs, mxArray *plhs[], |
---|
200 | int nrhs, const mxArray *prhs[] ) |
---|
201 | { |
---|
202 | double *Sup; |
---|
203 | double *Boundary; |
---|
204 | double *StraightLine; |
---|
205 | double *TexCoorXOri; |
---|
206 | double *TexCoorYOri; |
---|
207 | double *TexCoorOut; |
---|
208 | |
---|
209 | unsigned int VSup, HSup, VB, HB, NuStr, v, h; |
---|
210 | |
---|
211 | /* Check for proper number of arguments */ |
---|
212 | |
---|
213 | if (nrhs != 5) { |
---|
214 | mexErrMsgTxt("Four input arguments required."); |
---|
215 | } else if (nlhs > 1) { |
---|
216 | mexErrMsgTxt("Too many output arguments."); |
---|
217 | } |
---|
218 | |
---|
219 | /* Check the dimensions of the inputs. */ |
---|
220 | |
---|
221 | VSup = mxGetM(SUP_IN); |
---|
222 | HSup = mxGetN(SUP_IN); |
---|
223 | if (!mxIsDouble(SUP_IN) || mxIsComplex(SUP_IN) ) { |
---|
224 | mexErrMsgTxt("The Data Type of Sup shouldn't be complex."); |
---|
225 | } |
---|
226 | VB = mxGetM(BOUNDARY_IN); |
---|
227 | HB = mxGetN(BOUNDARY_IN); |
---|
228 | if (!mxIsDouble(BOUNDARY_IN) || mxIsComplex(BOUNDARY_IN) ) { |
---|
229 | mexErrMsgTxt("The Data Type of Boundary shouldn't be complex."); |
---|
230 | } |
---|
231 | NuStr = mxGetM(STRAIGHLINE_IN); |
---|
232 | h = mxGetN(STRAIGHLINE_IN); |
---|
233 | if (!mxIsDouble(STRAIGHLINE_IN) || mxIsComplex(STRAIGHLINE_IN) || |
---|
234 | (h != 4) ) { |
---|
235 | mexErrMsgTxt("StraighLine matrix should always has 4 columns."); |
---|
236 | } |
---|
237 | v = mxGetM( TEXCOORX_IN); |
---|
238 | h = mxGetN( TEXCOORX_IN); |
---|
239 | if (!mxIsDouble( TEXCOORX_IN) || mxIsComplex( TEXCOORX_IN) || |
---|
240 | (v != VSup) || (h != HSup)) { |
---|
241 | mexErrMsgTxt("TextCoorX matrix should is wrong."); |
---|
242 | } |
---|
243 | v = mxGetM( TEXCOORY_IN); |
---|
244 | h = mxGetN( TEXCOORY_IN); |
---|
245 | if (!mxIsDouble( TEXCOORY_IN) || mxIsComplex( TEXCOORY_IN) || |
---|
246 | (v != VSup) || (h != HSup) ) { |
---|
247 | mexErrMsgTxt("TextCoorY matrix should is wrong."); |
---|
248 | } |
---|
249 | |
---|
250 | /* Create a matrix for the return argument */ |
---|
251 | int DIM[3]; |
---|
252 | DIM[0] = VSup; |
---|
253 | DIM[1] = HSup; |
---|
254 | DIM[2] = 2; |
---|
255 | TEXCOOR_OUT = mxCreateNumericArray( 3, DIM, mxDOUBLE_CLASS, mxREAL); |
---|
256 | TexCoorOut = mxGetPr(TEXCOOR_OUT); |
---|
257 | |
---|
258 | /* Assign pointers to the various parameters */ |
---|
259 | Sup = mxGetPr( SUP_IN); |
---|
260 | Boundary = mxGetPr( BOUNDARY_IN); |
---|
261 | StraightLine = mxGetPr( STRAIGHLINE_IN); |
---|
262 | TexCoorXOri = mxGetPr( TEXCOORX_IN); |
---|
263 | TexCoorYOri = mxGetPr( TEXCOORY_IN); |
---|
264 | |
---|
265 | /* Do the actual computations in a subroutine */ |
---|
266 | SupRayAlign( Sup, Boundary, StraightLine, TexCoorXOri, TexCoorYOri, TexCoorOut, VSup, HSup, VB, HB, NuStr); |
---|
267 | return; |
---|
268 | |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | |
---|