码迷,mamicode.com
首页 > 其他好文 > 详细

图像匹配-SSD

时间:2020-03-06 17:11:27      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:atl   return   and   dex   als   enc   mat   val   cal   

任務:

  現在有兩來自於stereo-camera拍攝的兩幅圖像:

  左圖, flowers-left.png 右圖,flowers-right.png:

  技术图片         技术图片

 

 現在在左圖中取一個大小爲100*100的patch.

技术图片

 

 在右圖的strip中尋找匹配的patch.

技术图片

 

在此使用 ( sum of square differences )SSD算法進行匹配。也可以使用cross-correlation.

Matlab 程序代碼:

% Load images
left = imread(‘imgs/flowers-left.png‘);
right = imread(‘imgs/flowers-right.png‘);
figure, imshow(left);
figure, imshow(right);

% Convert to grayscale, double, [0, 1] range for easier computation
left_gray = double(rgb2gray(left)) / 255.0;
right_gray = double(rgb2gray(right)) / 255.0;

% Define image patch location (topleft [row col]) and size
patch_loc = [120 170];
patch_size = [100 100];

% Extract patch (from left image)
patch_left = left_gray(patch_loc(1):(patch_loc(1) + patch_size(1) - 1), patch_loc(2):(patch_loc(2) + patch_size(2) - 1));
figure, imshow(patch_left);

% Extract strip (from right image)
strip_right = right_gray(patch_loc(1):(patch_loc(1) + patch_size(1) - 1), :);
figure, imshow(strip_right);

% Now look for the patch in the strip and report the best position (column index of topleft corner)
best_x = find_best_match(patch_left, strip_right);
disp(best_x);
patch_right = right_gray(patch_loc(1):(patch_loc(1) + patch_size(1) - 1), best_x:(best_x + patch_size(2) - 1));
figure, imshow(patch_right);

% Find best match
% Use sum of square differences (SSD), you could also use cross-correlation
function best_x = find_best_match(patch, strip)
    % TODO: Find patch in strip and return column index (x value) of topleft corner
    best_x = 1; % placeholder
    min_diff = Inf;
    [row_strip, col_strip]=size(strip);
    [row_patch, col_patch]=size(patch);
    for x = 1:(col_strip - col_patch + 1 )
        other_patch = strip(:, x:(x + col_patch -1 ));
        diff = sum(sqrt((patch -other_patch).^2), ‘all‘);
%         diff = sumsq((patch - other_patch)(:)); %octave
        if diff < min_diff
            min_diff = diff;
            best_x = x;
        end
    end
end

  

結果:

  右圖爲在右圖中匹配等到的結果。

  技术图片   技术图片

 

图像匹配-SSD

标签:atl   return   and   dex   als   enc   mat   val   cal   

原文地址:https://www.cnblogs.com/yubao/p/12427349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!