回答
如果您的最终目标是拼接全景,您可能需要考虑this code。在任何情况下,为了获得重叠区域,您需要首先注册图像(了解它们如何重叠 - 或者更加数学地说:找到从图像1到图像2的转换)。为此,您需要在两张图像中找到匹配点。
在下面的代码的确,对于两个图像(由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
如果没有重叠,那么结果是什么? – Krishna
还没有测试过,但我认为代码将会从'matchFeatures()'返回无匹配或非常少的匹配,这将导致'estimateGeometricTransform()'失败。对于空匹配列表'matchedPoints1' /'2',您可能会遇到错误。对于少数(可能是erroneuos)匹配,该函数将返回状态码“1”('matchedPoints1和matchedPoints2输入不包含足够的点.')或'2'('找不到足够的内点)。见[这里](https://de.mathworks.com/help/vision/ref/estimategeometrictransform.html)。你需要添加一个检查来使代码健壮。 – Honeybear
我试过按照这种方式。这是工作。 – Krishna
- 1. R:使用R找到重叠区域
- 2. MATLAB:区域重叠的圆圈
- 3. 两个图像之间的重叠区域
- 4. 在Matlab中重叠图像
- 5. matlab中两幅图像的融合
- 6. 如何将一幅图像叠加到另一幅图像上?
- 7. 找到包围黑色区域的图像的宽度:Matlab
- 8. 寻找两个矩形(在C#)的重叠区域
- 9. 查找图像的区域
- 10. 如何在MATLAB中将图像与图形重叠?
- 11. XNA深度图重叠区域颜色
- 12. Bootstrap与重叠图像
- 13. 图像与div重叠
- 14. 如何使重叠的图像与实际重叠的图像重叠?
- 15. 重叠事件区域
- 16. 比较两幅图像
- 17. 两幅图像的图像比较
- 18. 在RGB图像上重叠二值图像MATLAB
- 19. 使用matlab对两幅图像进行颜色比较
- 20. 计算matlab中两幅图像的协方差
- 21. 在HTML5中重叠两个图像
- 22. 两个图像和文字重叠
- 23. 图像重叠两个div(垂直)
- 24. 查找两个图像之间的不同区域
- 25. 查找c#中两个整数范围之间的重叠区域
- 26. 查找图像中的空白区域
- 27. Javascript - 查找图像的最暗区域
- 28. 如何查找使用Python的两幅图像是否有区别?
- 29. 图像重叠的砌体和谷歌地图不占用区域的宽度
- 30. 如何使两个图像与WPF重叠?
到目前为止你为了达到目标而尝试了些什么?我们不会编写代码,但如果您在尝试自行完成某个特定问题时遇到了问题,我们将帮助您。 – Max
我同意马克斯 - 你应该更加努力地回答你的问题,并指出你的位置。 [如何问](http://stackoverflow.com/help/how-to-ask)。另外,如果您告诉我们您需要什么(全景拼接?),您将获得针对您的实际问题量身打造的更好答案。为您和回答的人节省工作。这就是说,我仍然发布了一个答案,希望是你正在寻找的。 – Honeybear