1 | % * This code was used in the following articles:
|
---|
2 | % * [1] Learning 3-D Scene Structure from a Single Still Image,
|
---|
3 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
4 | % * In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
|
---|
5 | % * (best paper)
|
---|
6 | % * [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
|
---|
7 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
8 | % * In ICCV workshop on Virtual Representations and Modeling
|
---|
9 | % * of Large-scale environments (VRML), 2007.
|
---|
10 | % * [3] 3-D Depth Reconstruction from a Single Still Image,
|
---|
11 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
12 | % * International Journal of Computer Vision (IJCV), Aug 2007.
|
---|
13 | % * [6] Learning Depth from Single Monocular Images,
|
---|
14 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
15 | % * In Neural Information Processing Systems (NIPS) 18, 2005.
|
---|
16 | % *
|
---|
17 | % * These articles are available at:
|
---|
18 | % * http://make3d.stanford.edu/publications
|
---|
19 | % *
|
---|
20 | % * We request that you cite the papers [1], [3] and [6] in any of
|
---|
21 | % * your reports that uses this code.
|
---|
22 | % * Further, if you use the code in image3dstiching/ (multiple image version),
|
---|
23 | % * then please cite [2].
|
---|
24 | % *
|
---|
25 | % * If you use the code in third_party/, then PLEASE CITE and follow the
|
---|
26 | % * LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
|
---|
27 | % *
|
---|
28 | % * Finally, this code is for non-commercial use only. For further
|
---|
29 | % * information and to obtain a copy of the license, see
|
---|
30 | % *
|
---|
31 | % * http://make3d.stanford.edu/publications/code
|
---|
32 | % *
|
---|
33 | % * Also, the software distributed under the License is distributed on an
|
---|
34 | % * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
---|
35 | % * express or implied. See the License for the specific language governing
|
---|
36 | % * permissions and limitations under the License.
|
---|
37 | % *
|
---|
38 | % */
|
---|
39 | function [Region POriReprojM PoccluM OccluDist] = FindOccluRegion(defaultPara, ScaleImg, ITarget, F, TargetPointPix, FieldPointPix, INDTarget, INDField, ModelInfoTarget, ModelInfoField, Pair) |
---|
40 | |
---|
41 | % Function find the closed occlu point and output a region on epipolar line |
---|
42 | % to search for dense match |
---|
43 | |
---|
44 | % find features close to epipoaeline |
---|
45 | |
---|
46 | % initial parameters |
---|
47 | FlagDisp = 1; |
---|
48 | EpipolarThre = 0.1; |
---|
49 | ScaleSlab = 1; |
---|
50 | VertSearchSlab = 0.1; |
---|
51 | HoriSearchSlab = 0.05; |
---|
52 | |
---|
53 | % Initialize Variables |
---|
54 | Region = []; |
---|
55 | POriReprojM= []; |
---|
56 | PoccluM = []; |
---|
57 | OccluDist = []; |
---|
58 | |
---|
59 | % Generate a region for each TargetPointPix point |
---|
60 | NumTargetPointPix = size(TargetPointPix,2); |
---|
61 | for IndexTargetPointPix = 1:NumTargetPointPix |
---|
62 | % iterating over all points in target image |
---|
63 | |
---|
64 | % Pre-filtering the 3-d points in field image that are far from the ray connecting target point to target image-origin. |
---|
65 | %closeToRay = true; |
---|
66 | % depth |
---|
67 | %[DTargetPick INDTargetPick] = PorjPosi2Depth([Iy Ix], [DepthY DepthX], TargetPointPix(:,IndexTargetPointPix), |
---|
68 | % ModelInfoTarget.Model.PlaneParaInfo.FitDepth); |
---|
69 | % computing epipolar lines |
---|
70 | EpipolarLinePara = F*[ TargetPointPix(:,IndexTargetPointPix); 1]; |
---|
71 | EpipolarLinePara = EpipolarLinePara / norm(EpipolarLinePara(1:2)); |
---|
72 | TargetPoint3D = ModelInfoTarget.Model.PlaneParaInfo.Position3DFited(:,INDTarget(:,IndexTargetPointPix)); % 3x1 |
---|
73 | |
---|
74 | %FieldPoint3D = ModelInfoField.Model.PlaneParaInfo.Position3DFitted; % 55x305x3 |
---|
75 | |
---|
76 | % finding the distance to the line |
---|
77 | |
---|
78 | % Find the RePojection of the TargetPick Point in Field-Camera |
---|
79 | x_calib = inv(defaultPara.InrinsicK1)*[ TargetPointPix(:,IndexTargetPointPix); 1]; |
---|
80 | ray = x_calib/norm(x_calib); |
---|
81 | POriReproj = defaultPara.InrinsicK2*(Pair.R*( ray*ModelInfoTarget.Model.PlaneParaInfo.FitDepth( INDTarget(:,IndexTargetPointPix) )) + Pair.T); |
---|
82 | % ModelInfoTarget.Model.PlaneParaInfo.Position3DFited(:,INDTarget(: |
---|
83 | % ,IndexTargetPointPix)) |
---|
84 | POriReproj = POriReproj(1:2,:)./POriReproj(3,:); |
---|
85 | % modify the POriReproj to the closest point in the image |
---|
86 | % First check Horizontal then check Vertical |
---|
87 | LineVector = [ EpipolarLinePara(2) -EpipolarLinePara(1)]; |
---|
88 | if POriReproj(2) > ScaleImg(2) % make POriReproj(2) = ScaleImg(2) |
---|
89 | shift = (ScaleImg(2) - POriReproj(2))/LineVector(2); |
---|
90 | POriReproj(1) = POriReproj(1) + shift*LineVector(1); |
---|
91 | POriReproj(2) = ScaleImg(2); |
---|
92 | elseif POriReproj(2) < 1 |
---|
93 | shift = (1 - POriReproj(2))/LineVector(2); |
---|
94 | POriReproj(1) = POriReproj(1) + shift*LineVector(1); |
---|
95 | POriReproj(2) = 1; |
---|
96 | end |
---|
97 | |
---|
98 | if POriReproj(1) > ScaleImg(1) % make POriReproj(1) = ScaleImg(1) |
---|
99 | shift = (ScaleImg(1) - POriReproj(1))/LineVector(1); |
---|
100 | POriReproj(2) = POriReproj(2) + shift*LineVector(2); |
---|
101 | POriReproj(1) = ScaleImg(1); |
---|
102 | elseif POriReproj(1) < 1 |
---|
103 | shift = (1 - POriReproj(1))/LineVector(1); |
---|
104 | POriReproj(2) = POriReproj(2) + shift*LineVector(2); |
---|
105 | POriReproj(1) = 1; |
---|
106 | end |
---|
107 | |
---|
108 | if abs(EpipolarLinePara' * [POriReproj ; ones(1, 1)]) > 0.1 |
---|
109 | disp('error of POriReproj'); |
---|
110 | end |
---|
111 | |
---|
112 | % Find the ReProjection of the origion of Target camer in Field camera |
---|
113 | %if Pair.T(3) < 0 |
---|
114 | |
---|
115 | %else |
---|
116 | |
---|
117 | %end |
---|
118 | |
---|
119 | Mask = abs(EpipolarLinePara' * [ FieldPointPix; ones(1, size(FieldPointPix,2))]) <= EpipolarThre*max(ScaleImg); |
---|
120 | |
---|
121 | FieldPoint3D = ModelInfoField.Model.PlaneParaInfo.Position3DFited(:,INDField(Mask)); % 3 x NPoints % inFeild coordinate |
---|
122 | FieldPointPixMask = FieldPointPix(:, Mask); |
---|
123 | % change FieldPoint3D into Target Coordinate |
---|
124 | R_f_t = Pair.R'; |
---|
125 | T_f_t = -Pair.R'*Pair.T; |
---|
126 | FieldPoint3D = R_f_t*FieldPoint3D + repmat( T_f_t, 1, size(FieldPoint3D,2)); % now in Target coordinate |
---|
127 | |
---|
128 | % find if the points that occlude |
---|
129 | |
---|
130 | % first find the distance of all the point from the target-origi ray |
---|
131 | if ~isempty(FieldPoint3D) |
---|
132 | u = TargetPoint3D' * FieldPoint3D / (TargetPoint3D' * TargetPoint3D); |
---|
133 | distances = sqrt( sum( ( repmat(u,[3,1]) .* repmat(TargetPoint3D,[1,size(FieldPoint3D,2)]) - FieldPoint3D).^2 ,1)); |
---|
134 | [ V occludingPointsMask] = min(distances); |
---|
135 | % occludingPointsMask = distances < 0.1; % MAGIC number. 1 meter in world units |
---|
136 | else |
---|
137 | occludingPointsMask = []; |
---|
138 | end |
---|
139 | |
---|
140 | % ============ |
---|
141 | % check the plan parameter to make sure alpha'*x - 1 > 0 |
---|
142 | % check is the occlusion reprojection has the same planeparameter index |
---|
143 | % ================ |
---|
144 | |
---|
145 | if ~isempty(occludingPointsMask) && sum(occludingPointsMask) ~=0% occludsion case |
---|
146 | candidateFieldPoints3D = FieldPointPixMask(:,occludingPointsMask); |
---|
147 | candidateU = u(occludingPointsMask); % fraction of TargetPoint3D |
---|
148 | [tmp, IND] = min(candidateU); |
---|
149 | tmp = tmp*norm(TargetPoint3D); % change to real unit meters |
---|
150 | % if tmp < ModelInfoTarget.Model.PlaneParaInfo.FitDepth(INDTarget(:,IndexTargetPointPix)) |
---|
151 | |
---|
152 | % find the closest occlusion to camera-Target origin. |
---|
153 | %[Pocclu] = ClosetOcclusion( defaultPara, ScaleImg, Pair, ModelInfoTarget, ... |
---|
154 | % TargetPointPix(:,IndexTargetPointPix), INDTarget(:,IndexTargetPointPix), ModelInfoField, FieldPointPix(:, Mask), INDField(Mask)); |
---|
155 | |
---|
156 | %if closeToRay |
---|
157 | % consider intersection with the field superpixel of that point |
---|
158 | %end |
---|
159 | |
---|
160 | %if ~isempty(Pocclu) |
---|
161 | % DTargetPick and Pair.T both are rescaled to global scale |
---|
162 | |
---|
163 | % Define rectangular region form Pocclu and POriReproj |
---|
164 | PoccluNearBy = candidateFieldPoints3D(:,IND); |
---|
165 | if isempty(PoccluNearBy) |
---|
166 | disp('error') |
---|
167 | end |
---|
168 | % need to Porject PoccluNearBy to the closest position on the epipolar line |
---|
169 | Pocclu = - ( EpipolarLinePara' * [ PoccluNearBy; ones(1, 1)])* EpipolarLinePara(1:2) + PoccluNearBy; |
---|
170 | if Pocclu(2) > ScaleImg(2) % make Pocclu(2) = ScaleImg(2) |
---|
171 | shift = (ScaleImg(2) - Pocclu(2))/LineVector(2); |
---|
172 | Pocclu(1) = Pocclu(1) + shift*LineVector(1); |
---|
173 | Pocclu(2) = ScaleImg(2); |
---|
174 | elseif Pocclu(2) < 1 |
---|
175 | shift = (1 - Pocclu(2))/LineVector(2); |
---|
176 | Pocclu(1) = Pocclu(1) + shift*LineVector(1); |
---|
177 | Pocclu(2) = 1; |
---|
178 | end |
---|
179 | |
---|
180 | if Pocclu(1) > ScaleImg(1) % make Pocclu(1) = ScaleImg(1) |
---|
181 | shift = (ScaleImg(1) - Pocclu(1))/LineVector(1); |
---|
182 | Pocclu(2) = Pocclu(2) + shift*LineVector(2); |
---|
183 | Pocclu(1) = ScaleImg(1); |
---|
184 | elseif Pocclu(1) < 1 |
---|
185 | shift = (1 - Pocclu(1))/LineVector(1); |
---|
186 | Pocclu(2) = Pocclu(2) + shift*LineVector(2); |
---|
187 | Pocclu(1) = 1; |
---|
188 | end |
---|
189 | |
---|
190 | if abs(EpipolarLinePara' * [Pocclu ; ones(1, 1)]) > 0.1 |
---|
191 | disp('error of Pocclu'); |
---|
192 | end |
---|
193 | |
---|
194 | vari = [[ 0 0 HoriSearchSlab -HoriSearchSlab];... |
---|
195 | [VertSearchSlab -VertSearchSlab 0 0]]; |
---|
196 | vari = vari*max(ScaleImg)*ScaleSlab; |
---|
197 | MaxV = min(max([Pocclu(2)+vari(2,:) POriReproj(2)+vari(2,:)]),ScaleImg(1)); |
---|
198 | MinV = max(min([Pocclu(2)+vari(2,:) POriReproj(2)+vari(2,:)]),1); |
---|
199 | MaxH = min(max([Pocclu(1)+vari(1,:) POriReproj(1)+vari(1,:)]),ScaleImg(2)); |
---|
200 | MinH = max(min([Pocclu(1)+vari(1,:) POriReproj(1)+vari(1,:)]),1); |
---|
201 | %Region = [Region [MaxH; MinH; MaxV; MinV]]; |
---|
202 | Region = [Region [MinH; MaxH; MinV; MaxV]]; |
---|
203 | PoccluM = [PoccluM Pocclu]; |
---|
204 | POriReprojM = [POriReprojM POriReproj]; |
---|
205 | OccluDist =[ OccluDist (ModelInfoTarget.Model.PlaneParaInfo.FitDepth(INDTarget(:,IndexTargetPointPix)) -tmp)]; |
---|
206 | % else |
---|
207 | % Region = [Region zeros(4,1)]; |
---|
208 | % PoccluM = [PoccluM zeros(2,1)]; |
---|
209 | % POriReprojM = [POriReprojM zeros(2,1)]; |
---|
210 | % OccluDist = [ OccluDist zeros(1,1)]; |
---|
211 | % end |
---|
212 | else % case of no occlusion |
---|
213 | Region = [Region zeros(4,1)]; |
---|
214 | PoccluM = [PoccluM zeros(2,1)]; |
---|
215 | POriReprojM = [POriReprojM zeros(2,1)]; |
---|
216 | OccluDist = [ OccluDist zeros(1,1)]; |
---|
217 | end |
---|
218 | end |
---|
219 | |
---|
220 | return; |
---|