[37] | 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 [Matches CoeffM Inliers]=CorrolationMatch( defaultPara, Pair, ITarget, IField, PointPix, POriReprojM, FieldOccluPix, MinMaxScaleFactor, Target2NumberOfPatches) |
---|
| 40 | |
---|
| 41 | % Find corrolation match along the epipolar line |
---|
| 42 | % by calling FindTarget.m for each pair of search |
---|
| 43 | |
---|
| 44 | % initializing Parameters |
---|
| 45 | % MatchTHre = 0.3; % Magic number change later |
---|
| 46 | if nargin <9 |
---|
| 47 | %Target2NumberOfPatches = [ 2.5 5]; |
---|
| 48 | Target2NumberOfPatches = [ 1 1]; % used smaller target size to speed up |
---|
| 49 | end |
---|
| 50 | ScaleFactorStep = 0.1; |
---|
| 51 | TargetReduceRatio = 0.95; |
---|
| 52 | [Iy Ix] = size(ITarget); |
---|
| 53 | [IyField IxField] = size(IField); |
---|
| 54 | TargetSize = round([Iy/55*Target2NumberOfPatches(1) Ix/305*Target2NumberOfPatches(2)]);% prevous used 5 and 10 |
---|
| 55 | NumTarget = size(PointPix,2); |
---|
| 56 | |
---|
| 57 | % Process the Target_tmp region |
---|
| 58 | Target_tmp_top = round( PointPix(2,:) - TargetSize(1) ); |
---|
| 59 | Target_tmp_bottom = round( PointPix(2,:) + TargetSize(1) ); |
---|
| 60 | Target_tmp_left = round( PointPix(1,:) - TargetSize(2) ); |
---|
| 61 | Target_tmp_right = round( PointPix(1,:) + TargetSize(2) ); |
---|
| 62 | |
---|
| 63 | % detect region exceed the boundary of images and shift the region accordingly |
---|
| 64 | % Yet make sure the PointPix is as close to the center of the Target_tmp as possible |
---|
| 65 | % (Shift the Target_tmp vertically) |
---|
| 66 | Mask = Target_tmp_top < 1; |
---|
| 67 | shift_vertical(Mask) = (1 - Target_tmp_top(Mask)); |
---|
| 68 | Target_tmp_top(Mask) = Target_tmp_top(Mask) + shift_vertical(Mask); |
---|
| 69 | Target_tmp_bottom(Mask) = Target_tmp_bottom(Mask) + shift_vertical(Mask); |
---|
| 70 | Mask = Target_tmp_bottom > Iy; |
---|
| 71 | shift_vertical(Mask) = ( Iy - Target_tmp_bottom(Mask)); |
---|
| 72 | Target_tmp_top(Mask) = Target_tmp_top(Mask) + shift_vertical(Mask); |
---|
| 73 | Target_tmp_bottom(Mask) = Target_tmp_bottom(Mask) + shift_vertical(Mask); |
---|
| 74 | % (Shift the Target_tmp horizontal) |
---|
| 75 | Mask = Target_tmp_left < 1; |
---|
| 76 | shift_horizontal(Mask) = (1 - Target_tmp_left(Mask)); |
---|
| 77 | Target_tmp_left(Mask) = Target_tmp_left(Mask) + shift_horizontal(Mask); |
---|
| 78 | Target_tmp_right(Mask) = Target_tmp_right(Mask) + shift_horizontal(Mask); |
---|
| 79 | Mask = Target_tmp_right > Ix; |
---|
| 80 | shift_horizontal(Mask) = (Ix - Target_tmp_right(Mask)); |
---|
| 81 | Target_tmp_left(Mask) = Target_tmp_left(Mask) + shift_horizontal(Mask); |
---|
| 82 | Target_tmp_right(Mask) = Target_tmp_right(Mask) + shift_horizontal(Mask); |
---|
| 83 | |
---|
| 84 | % Define the TopMargin, BottomMargin, LeftMargin, and RightMargin of Target_tmp |
---|
| 85 | PointPixTopMargin = PointPix(2,:) - Target_tmp_top; % All Margin are positive |
---|
| 86 | PointPixBottomMargin = Target_tmp_bottom - PointPix(2,:); |
---|
| 87 | PointPixLeftMargin = PointPix(1,:) - Target_tmp_left; |
---|
| 88 | PointPixRightMargin = Target_tmp_right - PointPix(1,:); |
---|
| 89 | |
---|
| 90 | % initializing variables |
---|
| 91 | Matches = zeros(4,NumTarget); |
---|
| 92 | %CoeffM = zeros(1,NumTarget); |
---|
| 93 | CoeffM = zeros(2,NumTarget); |
---|
| 94 | |
---|
| 95 | % 0) determine all possible Scale and region |
---|
| 96 | ScaleFactors = MinMaxScaleFactor(1):ScaleFactorStep:MinMaxScaleFactor(2); |
---|
| 97 | |
---|
| 98 | % 1) Field that fully contain target_tmp |
---|
| 99 | [ Rc ConS RoughConS tempConS] = EndPoint2BoxConS(defaultPara, Ix, Iy, POriReprojM, FieldOccluPix, 1); |
---|
| 100 | Field_top = round( RoughConS(3,:) - PointPixTopMargin*MinMaxScaleFactor(2)); |
---|
| 101 | Field_left = round( RoughConS(1,:) - PointPixLeftMargin*MinMaxScaleFactor(2)); |
---|
| 102 | Field_bottom = round( RoughConS(4,:) + PointPixBottomMargin*MinMaxScaleFactor(2)); |
---|
| 103 | Field_right = round( RoughConS(2,:) + PointPixRightMargin*MinMaxScaleFactor(2)); |
---|
| 104 | |
---|
| 105 | tic |
---|
| 106 | for i= 1:NumTarget |
---|
| 107 | if rem(i,100) == 0 |
---|
| 108 | disp([ num2str( floor(i/100)) 'Target Tested in ' num2str(NumTarget)]) |
---|
| 109 | end |
---|
| 110 | % Pick the Proper target_tmp |
---|
| 111 | ITarget_tmp = ITarget( Target_tmp_top(i):Target_tmp_bottom(i), Target_tmp_left(i):Target_tmp_right(i) ); |
---|
| 112 | |
---|
| 113 | % Define the Point to pick inside the Target Patch |
---|
| 114 | [ PickRowInTarget_tmp PickColInTarget_tmp] = PickAnchorPoint(ITarget_tmp); |
---|
| 115 | PickPointTarget(:,i) = [ PickRowInTarget_tmp + Target_tmp_top(i) - 1; PickColInTarget_tmp+ Target_tmp_left(i) - 1]; |
---|
| 116 | |
---|
| 117 | % prepare to run FindTarget |
---|
| 118 | |
---|
| 119 | % Stop finfing Matches if the IField is crop by the Field image region, since it might cause Target not totally inside the Field |
---|
| 120 | if Field_top(i) < 1 || Field_left(i) < 1 || Field_bottom(i) > IyField || Field_right(i) > IxField |
---|
| 121 | if defaultPara.Flag.FlagCorrRefinement |
---|
| 122 | Field_top(i) = min( max( Field_top(i), 1), IyField); |
---|
| 123 | Field_bottom(i) = min( max( Field_bottom(i), 1), IyField); |
---|
| 124 | Field_left(i) = min( max( Field_left(i), 1), IxField); |
---|
| 125 | Field_right(i) = min( max( Field_right(i), 1), IxField); |
---|
| 126 | else |
---|
| 127 | continue; |
---|
| 128 | end |
---|
| 129 | end |
---|
| 130 | |
---|
| 131 | % Pick the Field_tmp |
---|
| 132 | IField_tmp = IField(Field_top(i):Field_bottom(i), Field_left(i):Field_right(i)); |
---|
| 133 | |
---|
| 134 | % 2) Epipolar geometry |
---|
| 135 | T1_hat = [[0 -Pair.T(3) Pair.T(2)];... |
---|
| 136 | [Pair.T(3) 0 -Pair.T(1)];... |
---|
| 137 | [-Pair.T(2) Pair.T(1) 0]]; |
---|
| 138 | F = inv(defaultPara.InrinsicK2)'*T1_hat*Pair.R*inv(defaultPara.InrinsicK1); |
---|
| 139 | EpipoalLine = F*[ PointPix; ones(1,size(PointPix,2))]; |
---|
| 140 | NormalizedEpipoalLine = EpipoalLine./repmat( sqrt(sum( EpipoalLine(1:2,:).^2,1)),3,1); |
---|
| 141 | % if i == 10 || i ==34 || i ==45 |
---|
| 142 | % pause |
---|
| 143 | % end |
---|
| 144 | % Run Find Target |
---|
| 145 | [ X, Y, Scale, Coeff, Coeffs ] = FindTarget( defaultPara, IField_tmp, ITarget_tmp, Field_top(i), Field_left(i), PickRowInTarget_tmp, PickColInTarget_tmp, NormalizedEpipoalLine(:,i), ... |
---|
| 146 | [Iy Ix], ScaleFactors, 1, 1);% Min disable EpipolarLineFlag |
---|
| 147 | X = X+Field_left(i); |
---|
| 148 | Y = Y+Field_top(i); |
---|
| 149 | CoeffM(:,i) = Coeff; |
---|
| 150 | Matches(:,i) = [ PickPointTarget([2 1],i); X; Y]; |
---|
| 151 | % if i == 10 || i ==34 || i ==45 |
---|
| 152 | % target_tmp = imresize(ITarget_tmp,Scale,'bicubic'); |
---|
| 153 | % TargetPickPoint = [PickRowInTarget_tmp PickColInTarget_tmp]*Scale; |
---|
| 154 | % Box= ImgOverLay(target_tmp, IField, [Y X], TargetPickPoint); |
---|
| 155 | % figure(1); imshow(Box); |
---|
| 156 | % hold on; scatter(X,Y,'r');hold off; |
---|
| 157 | % figure(2); imshow(ITarget); |
---|
| 158 | % hold on; scatter( PickPointTarget(2,i), PickPointTarget(1,i),'r');hold off; |
---|
| 159 | % pause; |
---|
| 160 | % end |
---|
| 161 | end |
---|
| 162 | |
---|
| 163 | % Clean the redundent matches so that each PointPix match a unique points in the Field image //Min add July 10th================ |
---|
| 164 | [Inliers] = CleanMatch(Matches, CoeffM(1,:)); |
---|
| 165 | [InliersReverse] = CleanMatch(Matches(:,Inliers), CoeffM(1,Inliers)); |
---|
| 166 | Inliers = Inliers(InliersReverse); |
---|
| 167 | % =============================================================================================================================== |
---|
| 168 | |
---|
| 169 | TimeForCorrolationMatch = toc; |
---|
| 170 | if defaultPara.Flag.FlagRefinementDisp |
---|
| 171 | disp([ 'End Of CorrolationMatch, it takes ' num2str(TimeForCorrolationMatch) ' seconds']); |
---|
| 172 | end |
---|
| 173 | |
---|
| 174 | return; |
---|