[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 [X, Y, StepLen, Coeff, Coeffs ]=SearchCorrMatchEpipoarLine... |
---|
| 40 | (defaultPara, ITarget, IField, POriReproj, Pocclu, TargetFullyInField) |
---|
| 41 | |
---|
| 42 | % This function serch along the epipolar line from |
---|
| 43 | % POriReproj to Pocclu with fix step size |
---|
| 44 | |
---|
| 45 | % ParameterSetting |
---|
| 46 | Field2TargetScale = 2; |
---|
| 47 | TargetSize = size(ITarget);%round([Iy/55*5 Ix/305*10]); |
---|
| 48 | FieldSize = round(TargetSize*Field2TargetScale); % 1.5 gives bad result |
---|
| 49 | StepSize = min(FieldSize)/10; % make sure after each close by step the Field overlap % /2 since too big step size |
---|
| 50 | EpiolarLen = norm(POriReproj - Pocclu); |
---|
| 51 | P2 = POriReproj - Pocclu; |
---|
| 52 | UnitEpipoVector = Pocclu - POriReproj; %( X_Hori, Y_Vert) |
---|
| 53 | MargimSerachValue = 0.2*EpiolarLen; % percentage of the marginal search area |
---|
| 54 | DeviateFromEpipoleThre = 10; % pixels |
---|
| 55 | [IyField IxField] = size( IField); |
---|
| 56 | % equalized the images |
---|
| 57 | %ITarget = histeq(ITarget); |
---|
| 58 | % IField = histeq(IField); |
---|
| 59 | UnitEpipoVector = UnitEpipoVector./norm(UnitEpipoVector); |
---|
| 60 | best_max = 0.0; |
---|
| 61 | best_id = []; |
---|
| 62 | Coeffs = []; |
---|
| 63 | % Loop over Scale |
---|
| 64 | %NumScale = length(Scale) |
---|
| 65 | |
---|
| 66 | %for i = 1:NumScale |
---|
| 67 | |
---|
| 68 | % loop for step size along the epipolar line |
---|
| 69 | adj_row = TargetSize(1) ;%- 1; |
---|
| 70 | adj_col = TargetSize(2) ;%- 1; |
---|
| 71 | StepLen = -MargimSerachValue;%0; |
---|
| 72 | Shift_id = 1; |
---|
| 73 | |
---|
| 74 | while StepLen <= EpiolarLen+MargimSerachValue |
---|
| 75 | |
---|
| 76 | % Scale the ITarget |
---|
| 77 | % |
---|
| 78 | |
---|
| 79 | % create the new field for normxcorr2 to run |
---|
| 80 | FieldCenter = round(POriReproj + StepLen * UnitEpipoVector);%( X_Hori, Y_Vert) |
---|
| 81 | FieldCenter(1) = min(max( FieldCenter(1),1) , IxField);%( X_Hori, Y_Vert) |
---|
| 82 | FieldCenter(2) = min(max( FieldCenter(2),1) , IyField);%( X_Hori, Y_Vert) |
---|
| 83 | %figure(1); hold on; scatter(FieldCenter(1,1), FieldCenter(2,1),'y'); |
---|
| 84 | IField_tmp = IField( FieldCenter(2):min( FieldCenter(2)+FieldSize(1), IyField) , ... |
---|
| 85 | FieldCenter(1):min( FieldCenter(1)+FieldSize(2), IxField) ); |
---|
| 86 | if ( (size(ITarget, 1 ) <= size(IField_tmp,1)) && (size(ITarget, 2 ) <= size(IField_tmp,2)) ) % make sure |
---|
| 87 | res = normxcorr2( ITarget, IField_tmp); |
---|
| 88 | if (TargetFullyInField) |
---|
| 89 | % If we know that the target is compeltely within the field, |
---|
| 90 | % exclude portions of the correlation that are generated by |
---|
| 91 | % only a partial overlap of the target and field. |
---|
| 92 | top = size(ITarget,1) + 1; |
---|
| 93 | left = size(ITarget,2) + 1; |
---|
| 94 | height = size(IField_tmp,1) - size(ITarget,1) ;%+ 1; |
---|
| 95 | width = size(IField_tmp,2) - size(ITarget,2) ;%+ 1; |
---|
| 96 | % use only those results where the Target is compeltely |
---|
| 97 | % inside the Field. |
---|
| 98 | res = res( top:(top+height), left:(left+width) ); |
---|
| 99 | adj_row = 0; |
---|
| 100 | |
---|
| 101 | adj_col = 0; |
---|
| 102 | end |
---|
| 103 | |
---|
| 104 | if Shift_id == 2 |
---|
| 105 | disp('check corrolation'); |
---|
| 106 | end |
---|
| 107 | % Max(matrix) returns the largest value in each col, in a row vector |
---|
| 108 | [ max_vals, max_row_inds ] = max( abs( res ) ); |
---|
| 109 | % Find the Col with the bigest Max |
---|
| 110 | [ max_val , max_col_ind ] = max( max_vals ); |
---|
| 111 | % Get the row & col with the largest correlation |
---|
| 112 | max_row_ind = max_row_inds( max_col_ind ); |
---|
| 113 | % Adjust to get offset of scaled Target, in field, in units of |
---|
| 114 | % FIELD pixels. |
---|
| 115 | tmp_offsetRow = max_row_ind - adj_row + FieldCenter(2); |
---|
| 116 | tmp_offsetCol = max_col_ind - adj_col + FieldCenter(1); |
---|
| 117 | |
---|
| 118 | % if distances <= DeviateFromEpipoleThre |
---|
| 119 | Coeffs( Shift_id ).offsetRow = tmp_offsetRow; |
---|
| 120 | Coeffs( Shift_id ).offsetCol = tmp_offsetCol; |
---|
| 121 | Coeffs( Shift_id ).index = Shift_id; |
---|
| 122 | Coeffs( Shift_id ).StepLen = StepLen; |
---|
| 123 | Coeffs( Shift_id ).correlation = max_val; |
---|
| 124 | % StepLen, max_val, best_max, max_row_ind, max_col_ind |
---|
| 125 | % imview close all; |
---|
| 126 | % imview( res ); |
---|
| 127 | % imview( target_tmp); |
---|
| 128 | if (( Shift_id==1) || (max_val > best_max) ) |
---|
| 129 | best_max = max_val; |
---|
| 130 | best_id = Shift_id; |
---|
| 131 | end % check for new best location |
---|
| 132 | % end |
---|
| 133 | end |
---|
| 134 | |
---|
| 135 | Shift_id = Shift_id + 1; |
---|
| 136 | StepLen = StepLen+StepSize; |
---|
| 137 | end |
---|
| 138 | if ~isempty(best_id) |
---|
| 139 | P3 = [Coeffs( best_id ).offsetCol; Coeffs( best_id ).offsetRow] - Pocclu; |
---|
| 140 | u = P2' * P3 / (P2' * P2); |
---|
| 141 | distances = sqrt( sum( ( repmat(u,[2,1]) .* repmat(P2,[1,size(P3,2)]) - P3).^2 ,1)); |
---|
| 142 | if distances <= DeviateFromEpipoleThre |
---|
| 143 | X = Coeffs( best_id ).offsetCol; |
---|
| 144 | Y = Coeffs( best_id ).offsetRow; |
---|
| 145 | StepLen = Coeffs( best_id ).StepLen; |
---|
| 146 | Coeff = Coeffs( best_id ).correlation; |
---|
| 147 | else |
---|
| 148 | X = NaN; |
---|
| 149 | Y = NaN; |
---|
| 150 | StepLen = 0; |
---|
| 151 | Coeff = 0; |
---|
| 152 | end |
---|
| 153 | else |
---|
| 154 | X = NaN; |
---|
| 155 | Y = NaN; |
---|
| 156 | StepLen = 0; |
---|
| 157 | Coeff = 0; |
---|
| 158 | end |
---|
| 159 | |
---|
| 160 | %end |
---|