图像匹配-SSD

Posted yubao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像匹配-SSD相关的知识,希望对你有一定的参考价值。

任務:

  現在有兩來自於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的主要内容,如果未能解决你的问题,请参考以下文章

目标检测:YOLO和SSD 简介

数字图像处理-模板匹配

哪种立体匹配算法不是全局匹配算法

使用OpenCV进行模板匹配(原图-模板图)

Pytorch—SSD模型训练—步骤一:训练前的了解

视频目标检测