# aryan-mann/csc262-feature-matching

Stuff

Salm committed Apr 4, 2019
Showing with 457 additions and 245 deletions.
1. +18 −62 kpdet.m
2. +70 −0 kpdet2.m
3. +54 −58 kpfeat.m
4. +64 −0 kpfeat2.m
5. +0 −125 main.m
6. +251 −0 script.m
80 kpdet.m
 @@ -1,71 +1,27 @@ % Output: A cell containing the following values in order: - % [ 1. ] Points with Features % [ 2. ] Determinant % [ 3. ] Trace % [ 4. ] Determinant/Trace Measure % [5,6.] Partial Derivates w.r.t x and y % [7,8.] Blurred Versions of Square of 5,6 % [ 9. ] Blurred Product of 5,6 % [10. ] Orientation function results = kpdet(img, threshold, orientation_std_dev) function [feature_locs] = kpdet(img, threshold) gauss = gkern(1); gauss_deriv = gkern(1, 1); gauss_blur = gkern(1.5^2); % If no threshold argument is given, set it to a default value if (nargin < 3) orientation_std_dev = 4.5; end if (nargin < 2) threshold = 0.001; end % blurring_variance = 1.5; blurring_std_dev = 1.5; % Create the Gaussian (sigma = 1) and it's derivative gauss_1 = gkern(1); dgauss_1 = gkern(1, 1); % Create the blurring Gaussian (sigma = 1.5) gauss_1_5 = gkern(blurring_std_dev^2); % Convolve to get the partial x and y gradient of the image I_x = conv2(gauss_1, dgauss_1, img, 'same'); I_y = conv2(dgauss_1, gauss_1, img, 'same'); % Square of the partial derivatives I_x2 = I_x .^ 2; I_y2 = I_y .^ 2; % Product of the partial derivatives I_xy = I_x .* I_y; % Blur the squared partial derivatives squared and also their product I_x2_b = conv2(gauss_1_5, gauss_1_5, I_x2, 'same'); I_y2_b = conv2(gauss_1_5, gauss_1_5, I_y2, 'same'); I_xy_b = conv2(gauss_1_5, gauss_1_5, I_xy, 'same'); % The determinant of a 2x2 matrix is ac - bd det_A = (I_x2_b .* I_y2_b) - (I_xy_b .^ 2); horiz_img = conv2(gauss_deriv', gauss, img, 'same'); vert_img = conv2(gauss', gauss_deriv, img, 'same'); % The trace of a 2x2 matrix is the sum of the diagonals trace_A = (I_x2_b + I_y2_b); ix2 = horiz_img .^2; iy2 = vert_img .^2; ixiy = horiz_img .* vert_img; % The measure of uniqueness for a point given by Richard Szeliski det_trace_A = det_A ./ trace_A; ix2_blur = conv2(gauss_blur', gauss_blur, ix2, 'same'); iy2_blur = conv2(gauss_blur', gauss_blur, iy2, 'same'); ixiy_blur = conv2(gauss_blur', gauss_blur, ixiy, 'same'); % Binarize the measure of uniqueness using a certain threshold bin_det_trace_A = (det_trace_A > threshold); det = ix2_blur .* iy2_blur - ixiy_blur.^2; % the maximal points of the binarized image maxes_dt = maxima(bin_det_trace_A); trace = ix2_blur + iy2_blur; % Calculating the orientation of the gradient { Extra Credit Part 1 } gauss_o = gkern(orientation_std_dev^2); dgauss_o = gkern(orientation_std_dev^2, 1); O_x = conv2(gauss_o, dgauss_o, img, 'same'); O_y = conv2(dgauss_o, gauss_o, img, 'same'); orientation = atan2(O_y, O_x); harmonic_mean = det ./ trace; % Return certain features results = { maxes_dt, det_A, trace_A, det_trace_A, ... I_x, I_y, I_x2_b, I_y2_b, I_xy_b, orientation }; thresh_hm = harmonic_mean>threshold; M = maxima(harmonic_mean); feature_locs = thresh_hm .* M; end
 @@ -0,0 +1,70 @@ % Output: A cell containing the following values in order: - % [ 1. ] Points with Features % [ 2. ] Determinant % [ 3. ] Trace % [ 4. ] Determinant/Trace Measure % [5,6.] Partial Derivates w.r.t x and y % [7,8.] Blurred Versions of Square of 5,6 % [ 9. ] Blurred Product of 5,6 % [10. ] Orientation function results = kpdet1(img, threshold, orientation_std_dev) % If no threshold argument is given, set it to a default value if (nargin < 3) orientation_std_dev = 4.5; end if (nargin < 2) threshold = 0.001; end % blurring_variance = 1.5; blurring_std_dev = 1.5; % Create the Gaussian (sigma = 1) and it's derivative gauss_1 = gkern(1); dgauss_1 = gkern(1, 1); % Create the blurring Gaussian (sigma = 1.5) gauss_1_5 = gkern(blurring_std_dev^2); % Convolve to get the partial x and y gradient of the image I_x = conv2(gauss_1, dgauss_1, img, 'same'); I_y = conv2(dgauss_1, gauss_1, img, 'same'); % Square of the partial derivatives I_x2 = I_x .^ 2; I_y2 = I_y .^ 2; % Product of the partial derivatives I_xy = I_x .* I_y; % Blur the squared partial derivatives squared and also their product I_x2_b = conv2(gauss_1_5, gauss_1_5, I_x2, 'same'); I_y2_b = conv2(gauss_1_5, gauss_1_5, I_y2, 'same'); I_xy_b = conv2(gauss_1_5, gauss_1_5, I_xy, 'same'); % The determinant of a 2x2 matrix is ac - bd det_A = (I_x2_b .* I_y2_b) - (I_xy_b .^ 2); % The trace of a 2x2 matrix is the sum of the diagonals trace_A = (I_x2_b + I_y2_b); % The measure of uniqueness for a point given by Richard Szeliski det_trace_A = det_A ./ trace_A; % Binarize the measure of uniqueness using a certain threshold bin_det_trace_A = (abs(det_trace_A) > threshold); % the maximal points of the binarized image maxes_dt = maxima(bin_det_trace_A); % Calculating the orientation of the gradient { Extra Credit Part 1 } gauss_o = gkern(orientation_std_dev^2); dgauss_o = gkern(orientation_std_dev^2, 1); O_x = conv2(gauss_o, dgauss_o, img, 'same'); O_y = conv2(dgauss_o, gauss_o, img, 'same'); orientation = atan2(O_y, O_x); % Return certain features results = { maxes_dt, det_A, trace_A, det_trace_A, ... I_x, I_y, I_x2_b, I_y2_b, I_xy_b, orientation }; end
112 kpfeat.m
 @@ -1,65 +1,61 @@ function [descriptors] = kpfeat(img,keypoints,patch_size,downsampling_factor) function [descriptors] = kpfeat(img,det) % kpfeat General summary % Detailed explanation goes here % add default parameters if(nargin < 4) downsampling_factor = 5; end if(nargin < 3) patch_size = 8; end % find the row and column numbers of the detected features [kp_rows, kp_cols] = find(det); % find indices of keypoint [fcol, frow] = find(keypoints >= 1); % allocate space for result descriptors(size(fcol, 1), patch_size ^ 2) = 0; % N is the total number of detected keypoints N = length(kp_rows); % create a gaussian kernel to blur the image gauss = gkern(5^2); img_blur = conv2(gauss', gauss, img, 'same'); % downsample the blurred image by a factor of 5 ds_factor = 5; img_ds = img_blur(1:ds_factor:end, 1:ds_factor:end); max_row = size(img_ds, 1); max_col = size(img_ds, 2); % preallocate space for the N by 64 matrix of feature descriptions descriptors = zeros(N, 64); for i = 1:N % i is the index of the particular keypoint % find the coordinates of center of patch in downsampled image % rows_k the row number of the patch center in the downsampled image % similarly for cols_K row_center_ds = floor(kp_rows(i) / ds_factor); col_center_ds = floor(kp_cols(i) / ds_factor); % floor to make into integer % find the coordinates of upper left corner of patch in downsampled image row_upper_left_ds = row_center_ds - 3; col_upper_left_ds = col_center_ds - 3; % find the coordinates of lower right corner of patch in downsampled image row_lower_right_ds = row_center_ds + 4; col_lower_right_ds = col_center_ds + 4; % blur and downsample input image gauss_5 = gkern(5^2); blurred_img = conv2(gauss_5, gauss_5, img, 'same'); blurred_img = blurred_img(1:downsampling_factor:end, ... 1:downsampling_factor:end); if (row_upper_left_ds < 1) || (col_upper_left_ds < 1) || ... (row_lower_right_ds > max_row) || (col_lower_right_ds > max_col) descriptors(i, :) = NaN; continue; end patch_ds = img_ds(row_upper_left_ds:row_lower_right_ds, ... col_upper_left_ds:col_lower_right_ds); % loop through all features and extract a patch for k=1:size(fcol,1) % get coordinates of feature in downsampled image kRow = uint8(frow(k)/downsampling_factor); kCol = uint8(fcol(k)/downsampling_factor); % if even patch size if (rem(patch_size,2) == 0) ul_offset = patch_size/2; lr_offset = ul_offset - 1; else ul_offset = floor(patch_size / 2); lr_offset = ul_offset; end % calculate coordinates of corners of patch ul_corner_x = (kRow - ul_offset); ul_corner_y = (kCol - ul_offset); lr_corner_x = (kRow + lr_offset); lr_corner_y = (kCol + lr_offset); [img_width, img_height] = size(blurred_img); % check that patch does not fall outside image bounds if(ul_corner_x < 1 || ul_corner_y < 1 || ... lr_corner_x > img_width || lr_corner_y > img_height) descriptors(k,:) = NaN; continue; end % extract the patch patch = blurred_img(ul_corner_x:lr_corner_x, ... ul_corner_y:lr_corner_y); % check size of patch in the downsampled image (should be 8 by 8) size(patch_ds); normbias_patch_ds = patch_ds - mean(patch_ds(:)); gainbias_patch_ds = normbias_patch_ds / std(patch_ds(:)); % add the current patch to the descriptors (N by 64) matrix descriptors(i, :) = gainbias_patch_ds(:); end % normalize bias and gain of patch bias_norm_patch = patch - mean(patch(:)); norm_patch = bias_norm_patch ./ std(bias_norm_patch(:)); % store into array descriptors(k,:) = norm_patch(:); end end
 @@ -0,0 +1,64 @@ function [descriptors] = kpfeat1(img,keypoints,patch_size,downsampling_factor) % add default parameters if(nargin < 4) downsampling_factor = 5; end if(nargin < 3) patch_size = 8; end % find indices of keypoint [fcol, frow] = find(keypoints >= 1); % allocate space for result descriptors(size(fcol, 1), patch_size ^ 2) = 0; % blur and downsample input image gauss_5 = gkern(5^2); blurred_img = conv2(gauss_5, gauss_5, img, 'same'); blurred_img = blurred_img(1:downsampling_factor:end, ... 1:downsampling_factor:end); % loop through all features and extract a patch for k=1:size(fcol,1) % get coordinates of feature in downsampled image kRow = uint8(frow(k)/downsampling_factor); kCol = uint8(fcol(k)/downsampling_factor); % if even patch size if (rem(patch_size,2) == 0) ul_offset = patch_size/2; lr_offset = ul_offset - 1; else ul_offset = floor(patch_size / 2); lr_offset = ul_offset; end % calculate coordinates of corners of patch ul_corner_x = (kRow - ul_offset); ul_corner_y = (kCol - ul_offset); lr_corner_x = (kRow + lr_offset); lr_corner_y = (kCol + lr_offset); [img_width, img_height] = size(blurred_img); % check that patch does not fall outside image bounds if(ul_corner_x < 1 || ul_corner_y < 1 || ... lr_corner_x > img_width || lr_corner_y > img_height) descriptors(k,:) = NaN; continue; end % extract the patch patch = blurred_img(ul_corner_x:lr_corner_x, ... ul_corner_y:lr_corner_y); % normalize bias and gain of patch bias_norm_patch = patch - mean(patch(:)); norm_patch = bias_norm_patch ./ std(bias_norm_patch(:)); % store into array descriptors(k,:) = norm_patch(:); end end