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 <cstdlib> |
---|
42 | #include <stdio.h> |
---|
43 | #include <math.h> |
---|
44 | /* #include <stdio.h> */ |
---|
45 | #include "mex.h" |
---|
46 | |
---|
47 | /* Input Arguments */ |
---|
48 | |
---|
49 | #define SMATRIX_IN prhs[0] |
---|
50 | #define RowS prhs[1] |
---|
51 | #define ColS prhs[2] |
---|
52 | #define SAMPLE_P prhs[3] |
---|
53 | #define SUP_MASK prhs[4] |
---|
54 | |
---|
55 | |
---|
56 | /* Output Arguments */ |
---|
57 | |
---|
58 | #define SMATRIX_OUT plhs[0] |
---|
59 | |
---|
60 | #if !defined(MAX) |
---|
61 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
---|
62 | #endif |
---|
63 | |
---|
64 | #if !defined(MIN) |
---|
65 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) |
---|
66 | #endif |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | /* avgImg(SparseMOut,*rS,*cS,SamplePIn,SupMask,SparseMIn,m,n,Sm); */ |
---|
71 | static void avgImg( |
---|
72 | double SparseMOut[], |
---|
73 | double rS, |
---|
74 | double cS, |
---|
75 | double SamplePIn[], |
---|
76 | double SupMask[], |
---|
77 | double SparseMIn[], |
---|
78 | unsigned int m, |
---|
79 | unsigned int n, |
---|
80 | unsigned int Sm) |
---|
81 | { |
---|
82 | double sumOut; |
---|
83 | int CountNonZeros; |
---|
84 | int i,k,l; |
---|
85 | int i_adjusted, l_start, l_startIntom, l_end, innerCalc; |
---|
86 | |
---|
87 | /* Pre-calculate the fix number */ |
---|
88 | int cSby2 = (int) floor(cS/2); |
---|
89 | int rSby2 = (int) floor(rS/2); |
---|
90 | // printf("cSby2 = %d", cSby2); //confirmed |
---|
91 | // printf("rSby2 = %d", rSby2); // confirmed |
---|
92 | |
---|
93 | for (i=0;i<Sm;i++){ /*/ Count the No. of Sample points */ |
---|
94 | sumOut=0; |
---|
95 | CountNonZeros = 0; |
---|
96 | |
---|
97 | /* Pre-calculate the fix number */ |
---|
98 | int diffSamplePIn =(int) (SamplePIn[i+Sm] - 1) - cSby2; |
---|
99 | l_start = MAX(diffSamplePIn, 0); |
---|
100 | l_startIntom = l_start*m; |
---|
101 | l_end = (int) MIN(diffSamplePIn + cS, n); |
---|
102 | |
---|
103 | diffSamplePIn = (int)(SamplePIn[i]-1) - rSby2; |
---|
104 | |
---|
105 | for (k= MAX(diffSamplePIn, 0); k< (int)MIN(diffSamplePIn+rS,m); k++){ |
---|
106 | for (l=l_start,innerCalc=l_startIntom+k ;l<l_end; l++){ |
---|
107 | |
---|
108 | /* Pre-calculate the fix number */ |
---|
109 | /*innerCalc = l*m+k;*/ |
---|
110 | if ( SupMask[innerCalc] ) { |
---|
111 | sumOut += SparseMIn[innerCalc]; |
---|
112 | CountNonZeros++; |
---|
113 | } |
---|
114 | else{ |
---|
115 | // printf("SupMask 0\n"); |
---|
116 | } |
---|
117 | innerCalc += m; |
---|
118 | } |
---|
119 | } |
---|
120 | if (CountNonZeros == 0) |
---|
121 | {mexErrMsgTxt("CountNonZeros is zeros. It will cause error."); |
---|
122 | } |
---|
123 | SparseMOut[i]=sumOut/CountNonZeros;/* /(rS*cS) */; |
---|
124 | } |
---|
125 | |
---|
126 | return; |
---|
127 | } |
---|
128 | |
---|
129 | void mexFunction( int nlhs, mxArray *plhs[], |
---|
130 | int nrhs, const mxArray *prhs[] ) |
---|
131 | |
---|
132 | { |
---|
133 | double *SparseMOut; |
---|
134 | double *SparseMIn; |
---|
135 | double *SamplePIn; |
---|
136 | double *SupMask; |
---|
137 | double *rS,*cS; |
---|
138 | unsigned int m,n,Supm,Supn,Sm,Sn; |
---|
139 | |
---|
140 | /* Check for proper number of arguments */ |
---|
141 | |
---|
142 | if (nrhs != 5) { |
---|
143 | mexErrMsgTxt("Five input arguments required."); |
---|
144 | } else if (nlhs > 1) { |
---|
145 | mexErrMsgTxt("Too many output arguments."); |
---|
146 | } |
---|
147 | |
---|
148 | /* Check the dimensions of the inputs. */ |
---|
149 | |
---|
150 | m = mxGetM(RowS); |
---|
151 | n = mxGetN(RowS); |
---|
152 | if (!mxIsDouble(RowS) || mxIsComplex(RowS) || |
---|
153 | (MAX(m,n) != 1)) { |
---|
154 | mexErrMsgTxt("The row size should be a scalar."); |
---|
155 | } |
---|
156 | m = mxGetM(ColS); |
---|
157 | n = mxGetN(ColS); |
---|
158 | if (!mxIsDouble(ColS) || mxIsComplex(ColS) || |
---|
159 | (MAX(m,n) != 1)) { |
---|
160 | mexErrMsgTxt("The column size should be a scalar."); |
---|
161 | } |
---|
162 | m = mxGetM(SMATRIX_IN); |
---|
163 | n = mxGetN(SMATRIX_IN); |
---|
164 | if (!mxIsDouble(SMATRIX_IN) || mxIsComplex(SMATRIX_IN)) { |
---|
165 | mexErrMsgTxt("Input Sparse Matrix Format is not right."); |
---|
166 | } |
---|
167 | Supm = mxGetM(SUP_MASK); |
---|
168 | Supn = mxGetN(SUP_MASK); |
---|
169 | if (!mxIsDouble(SUP_MASK) || mxIsComplex(SUP_MASK) || |
---|
170 | Supm !=m || Supn != n) { |
---|
171 | mexErrMsgTxt("Input Superpixel Mask is not right."); |
---|
172 | } |
---|
173 | Sm = mxGetM(SAMPLE_P); /*/Number of Rows equals to Number of points to calculate*/ |
---|
174 | Sn = mxGetN(SAMPLE_P); |
---|
175 | if (!mxIsDouble(SAMPLE_P) || mxIsComplex(SAMPLE_P) || |
---|
176 | Sn != 2) { |
---|
177 | mexErrMsgTxt("Input Sample Point Index format should be Integer matrix of size M by 2."); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /* Create a matrix for the return argument */ |
---|
182 | SMATRIX_OUT = mxCreateDoubleMatrix(Sm, 1, mxREAL); |
---|
183 | |
---|
184 | /* Assign pointers to the various parameters */ |
---|
185 | SparseMOut = mxGetPr(SMATRIX_OUT); |
---|
186 | |
---|
187 | rS = mxGetPr(RowS); |
---|
188 | cS = mxGetPr(ColS); |
---|
189 | SparseMIn = mxGetPr(SMATRIX_IN); |
---|
190 | SamplePIn = mxGetPr(SAMPLE_P); |
---|
191 | SupMask = mxGetPr(SUP_MASK); |
---|
192 | |
---|
193 | /* Do the actual computations in a subroutine */ |
---|
194 | avgImg(SparseMOut,*rS,*cS,SamplePIn,SupMask,SparseMIn,m,n,Sm); |
---|
195 | return; |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | |
---|