source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/image3dstiching/Occlu/FindTarget.m @ 37

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

Added original make3d

File size: 8.2 KB
Line 
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% */
39function [ X, Y, Scale, Coeff, Coeffs ] = FindTarget( defaultPara, Field, Target, Field_top, Field_left, target_center_topOffset, target_center_leftOffset, ...
40                NormalizedEpipoalLine, ImgScale, ScaleFactors, TargetFullyInField, EnableEpipolarLineFlag)
41
42% [ X, Y, Scale, Coeff, Coeffs ] = FindTarget( Field, Target, Field_top, Field_left, NormalizedEpipoalLine, ...
43%               ImgScale, ScaleFactors, TargetFullyInField, EnableEpipolarLineFlag)
44
45% Search for Target image in Field image (gray) at multiple different
46% Target sizes (scale factors).
47% Field         - Image being searched
48% Target        - Image being Sought
49% ScaleFactors  - Vector of scale factors for Target, target is scaled
50%   by each of these factors (linear ie # lines in target =
51%   old_target_lies * ScaleFactor)
52% TargetFullyInField - Set to True if you are CERTAIN that the Target
53%   is fully in the Field. Helps ignore spurrious correlations, but
54%   will prevent finding location of Target that is only partially in
55%   Field. True Should be faster, but isn't appreciably.. sigh....
56
57%    local_field     = histeq(Field); % should not to histogram equalization in such a small region
58
59    EpipolarThre = 0.05;% used 0.05
60    num_scales  = size( ScaleFactors, 2 );
61    best_max     = 0.0;
62        best_id = [];
63        Coeffs = [];
64       
65    for scale_id = 1:num_scales
66        current_scale   = ScaleFactors( scale_id );
67        target_tmp      = imresize(Target, current_scale, 'bicubic');
68%       disp('Scaling the target accordingly');
69        top     = size(target_tmp,1);
70        left    = size(target_tmp,2);
71        target_tmp_center_topOffset = target_center_topOffset*current_scale;
72        target_tmp_center_leftOffset = target_center_leftOffset*current_scale;
73        adj_row         = size(target_tmp, 1 ) - 1;
74        adj_col         = size(target_tmp, 2 ) - 1;
75       
76        % Note normxcorr2 barfs if Target > Field, so skip iteration if
77        %   target is scaled too big.
78        if ( (size(target_tmp, 1 ) <= size(Field,1)) && (size(target_tmp, 2 ) <= size(Field,2)) ) % Field is always bigger than Target
79
80            % This is where the Heavy lifting is done. Find norimalzied
81            %   covaraince at all offsets between target and field
82            %   (including partial overlaps)
83            if defaultPara.Flag.UseNormXCorr2 == 1
84                res             = normxcorr2( target_tmp, Field );     
85            elseif defaultPara.Flag.UseNormXCorr2 == 0
86                res             = xcorr2( target_tmp, Field ); 
87            elseif defaultPara.Flag.UseNormXCorr2 == 2
88                res             = normxcorr2_mex( target_tmp, Field );     
89            end
90
91            % When we KNOW the target is completely within the field, trim
92            %   correlations that incldue partial overlaps, this reduces
93            %   false positives.
94            if (TargetFullyInField)
95                % If we know that the target is compeltely within the field,
96                %   exclude portions of the correlation that are generated by
97                %   only a partial overlap of the target and  field.
98                height  = size(Field,1) - size(target_tmp,1) + 1;
99                width   = size(Field,2) - size(target_tmp,2) + 1;
100                clear target_tmp;
101                % use only those results where the Target is compeltely
102                %   inside the Field.
103                res     = res( top:(top+height-1), left:(left+width-1) );
104                adj_row  = 0;
105                adj_col  = 0;
106            end
107
108            if EnableEpipolarLineFlag
109                Xim = [ Field_left + reshape(repmat( 1:size(res,2), size(res,1),1) ,1, []) - adj_col;...
110                    Field_top + reshape(repmat( 1:size(res,1)', 1, size(res,2)) ,1, []) - adj_row];
111                Residual = abs( NormalizedEpipoalLine' *  [ Xim(1:2,:); ones(1, size(Xim(1:2,:),2))]);
112                Mask = Residual <= EpipolarThre*max(ImgScale); 
113                res( ~reshape(Mask, size(res,1), [])) = 0; % set to the lowest possible corrolation value
114            end
115            % Max(matrix) returns the largest value in each col, in a row vector
116%            [ max_vals, max_row_inds ] = max( abs( res ) );
117            % Find the Col with the bigest Max
118%            [ max_val , max_col_ind  ] = max( max_vals );
119            % Get the row & col with the largest correlation
120%            max_row_ind = max_row_inds( max_col_ind );
121            % Adjust to get offset of scaled Target, in field, in units of
122            %   FIELD pixels.
123            if all(res == 0)
124%                disp('Res is all not close enough to Epipolar line');
125                continue;
126            end
127%               disp('Size of Res =');
128%               size(res)
129            [xymax,smax,xymin,smin] = extrema2(abs(res));
130            [ Vsort Isort]= sort(xymax,'descend');
131           
132            if size(Isort,1) >=2
133                [row_ind col_ind] = ind2sub(size(res),smax(Isort(1:2)));
134            elseif size(Isort,1) == 0
135                continue;
136            else
137%               disp('No other Similiar Point');
138                [row_ind col_ind] = ind2sub(size(res),smax(Isort(1)));
139            end         
140
141            Coeffs( scale_id ).index        = scale_id;
142            Coeffs( scale_id ).targetScale  = current_scale;
143            Coeffs( scale_id ).offsetRow    = row_ind(1) - adj_row + target_tmp_center_topOffset;
144            Coeffs( scale_id ).offsetCol    = col_ind(1) - adj_col + target_tmp_center_leftOffset;
145            Coeffs( scale_id ).correlation  = Vsort(1);
146
147            if size(Isort,1) >=2
148                Coeffs( scale_id ).SecondCorrelation  = Vsort(2);
149            else
150%               disp('No other Similiar Point');
151                Coeffs( scale_id ).SecondCorrelation  = 0;
152            end
153%            Coeffs( scale_id ).offsetRow    = max_row_ind - adj_row + target_tmp_center_topOffset;
154%            Coeffs( scale_id ).offsetCol    = max_col_ind - adj_col + target_tmp_center_leftOffset;
155%            Coeffs( scale_id ).correlation  = max_val;
156
157            if (( scale_id==1) || (Vsort(1) > best_max) )
158                best_max = Vsort(1);
159                best_id = scale_id;
160            end % check for new best location among different Scale
161
162        end % make sure target <= Field
163
164    end % loop over all scaled
165   
166    if ~isempty(best_id)
167        X       = Coeffs( best_id ).offsetCol;
168        Y       = Coeffs( best_id ).offsetRow;
169        Scale   = Coeffs( best_id ).targetScale; 
170        Coeff   = [ Coeffs( best_id ).correlation; Coeffs( best_id ).SecondCorrelation];
171    else
172        X       = 0;
173        Y       = 0;
174        Scale   = 1; 
175        Coeff   = [0; 0];
176    end
177
178return  %end function
Note: See TracBrowser for help on using the repository browser.