2017-04-11 171 views
1

image1找到兩幅圖像重疊區域與MATLAB

image2

image with overlap area indicated in red

我想找到使用MATLAB中的兩個圖像的重疊區域。

+3

到目前爲止你爲了達到目標而嘗試了些什麼?我們不會編寫代碼,但如果您在嘗試自行完成某個特定問題時遇到了問題,我們將幫助您。 – Max

+0

我同意馬克斯 - 你應該更加努力地回答你的問題,並指出你的位置。 [如何問](http://stackoverflow.com/help/how-to-ask)。另外,如果您告訴我們您需要什麼(全景拼接?),您將獲得針對您的實際問題量身打造的更好答案。爲您和回答的人節省工作。這就是說,我仍然發佈了一個答案,希望是你正在尋找的。 – Honeybear

回答

1

如果您的最終目標是拼接全景,您可能需要考慮this code。在任何情況下,爲了獲得重疊區域,您需要首先註冊圖像(瞭解它們如何重疊 - 或者更加數學地說:找到從圖像1到圖像2的轉換)。爲此,您需要在兩張圖像中找到匹配點。

overlayed images after transform

在下面的代碼的確,對於兩個圖像(由this code啓發,它使用較舊的MATLAB函數)。

%% >>>>>>> load images and calculate their transformation <<<<<<< %% 
im1 = imread('1.png'); 
im2 = imread('2.png'); 
imshowpair(im1, im2, 'montage'); 

% calculate features on grayscale image 
im1g = rgb2gray(im1); 
im2g = rgb2gray(im2); 
points1 = detectSURFFeatures(im1g); 
[features1, points1] = extractFeatures(im1g, points1); 
points2 = detectSURFFeatures(im2g); 
[features2, points2] = extractFeatures(im2g, points2); 

% Find correspondences between im1 and im2 
indexPairs = matchFeatures(features1, features2, 'Unique', true); 
matchedPoints1 = points1(indexPairs(:,1), :); 
matchedPoints2 = points2(indexPairs(:,2), :); 

% Identity transformation 
transform_eye = projective2d(eye(3)); 
% Estimate the transformation between im1 and im2 
% we use a 'similarity' transform (translation/rotation), which treats the 
% images as rigid bodys. 'affine'/'projective' transformations allow for 
% warping the images itself (the overlap might not be a rectangle). 
transform = estimateGeometricTransform(matchedPoints1, matchedPoints2,... 
    'similarity', 'Confidence', 99.9, 'MaxNumTrials', 2000); 

%% >>>>>>> apply transformation to images <<<<<<< %% 

% create a world coordinate system (RF) that has space to store 
% the reference image (im1) and the transformed image (im2) 
R2 = imref2d(size(im2)); 
[~, R2T]=imwarp(im2,R2,transform); 
xLimits=[min(0.5,R2T.XWorldLimits(1)) max(size(im1,2), R2T.XWorldLimits(2))]; 
yLimits=[min(0.5,R2T.YWorldLimits(1)) max(size(im1,1), R2T.YWorldLimits(2))]; 
width = round(xLimits(2) - xLimits(1)); 
height = round(yLimits(2) - yLimits(1)); 
RF = imref2d([height width], xLimits, yLimits); 

% transform both images with regard to the world coordinate system RF 
im1t=imwarp(im1,transform_eye,'OutputView',RF); % im1 stays in place (identity transform) 
im2t=imwarp(im2,transform,'OutputView',RF); % im2 is transformed 

% visualize result 
imOverlay = im1t/2 + im2t/2; 
imshow(imOverlay); 

%% >>>>>>> get the overlap area only <<<<<<< %% 
% if you only want the overlap area, apply the transform to image masks 
im1bw = ones(size(im1)); % mask1 
im2bw = ones(size(im2)); % mask2 
im1bwt=imwarp(im1bw,transform_eye,'OutputView',RF); % im1 stays in place (identity transform) 
im2bwt=imwarp(im2bw,transform,'OutputView',RF); % im2 is transformed 

% visualize result 
maskOverlap = im1bwt + im2bwt - 1; 
imshow(maskOverlap); 
% maskOverlap is a bw image that contains 'true' for overlap pixels 
% you can use that for cropping imOverlay or 
% use bwarea or regionprops to calculate the area 
+0

如果沒有重疊,那麼結果是什麼? – Krishna

+1

還沒有測試過,但我認爲代碼將會從'matchFeatures()'返回無匹配或非常少的匹配,這將導致'estimateGeometricTransform()'失敗。對於空匹配列表'matchedPoints1' /'2',您可能會遇到錯誤。對於少數(可能是erroneuos)匹配,該函數將返回狀態碼「1」('matchedPoints1和matchedPoints2輸入不包含足夠的點.')或'2'('找不到足夠的內點)。見[這裏](https://de.mathworks.com/help/vision/ref/estimategeometrictransform.html)。你需要添加一個檢查來使代碼健壯。 – Honeybear

+0

我試過按照這種方式。這是工作。 – Krishna