如何从两个图像中检测差异并显示差异?

2024-06-28 19:56:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做的是:

我有两张相似的照片。图像的位置可能不同。 所以我用冲浪特征探测器。对两幅图像进行特征匹配,得到变换矩阵。 我用这个变换矩阵扭曲了第一幅图像。 结果是第二幅图像有轻微的偏移。 所以我不能用减法求差。 我如何发现差异并通过在差异周围画圆圈来显示?在

我现在正在使用matlab和python。在

这是我的matlab代码。在

%% Step 1: Read Images
% Read the reference image containing the object of interest.
oimg1 = imread('test3_im1.jpg');
img1 = imresize(rgb2gray(oimg1),0.2);
figure;
imshow(img1);
title('First Image');

%%
% Read the target image containing a cluttered scene.
oimg2 = imread('test3_im2.jpg');
img2 = imresize(rgb2gray(oimg2),0.2);
figure; 
imshow(img2);
title('Second Image');

%% Step 2: Detect Feature Points
% Detect feature points in both images.
points1 = detectSURFFeatures(img1);
points2 = detectSURFFeatures(img2);

%% 
% Visualize the strongest feature points found in the reference image.
figure; 
imshow(img1);
title('500 Strongest Feature Points from Box Image');
hold on;
plot(selectStrongest(points1, 500));

%% 
% Visualize the strongest feature points found in the target image.
figure; 
imshow(img2);
title('500 Strongest Feature Points from Scene Image');
hold on;
plot(selectStrongest(points2, 500));

%% Step 3: Extract Feature Descriptors
% Extract feature descriptors at the interest points in both images.
[features1, points1] = extractFeatures(img1, points1);
[features2, points2] = extractFeatures(img2, points2);

%% Step 4: Find Putative Point Matches
% Match the features using their descriptors. 
pairs = matchFeatures(features1, features2);

%% 
% Display putatively matched features. 
matchedPoints1 = points1(pairs(:, 1), :);
matchedPoints2 = points2(pairs(:, 2), :);
figure;
showMatchedFeatures(img1, img2, matchedPoints1, matchedPoints2, 'montage');
title('Putatively Matched Points (Including Outliers)');

%% Step 5: Locate the Object in the Scene Using Putative Matches
% |estimateGeometricTransform| calculates the transformation relating the
% matched points, while eliminating outliers. This transformation allows us
% to localize the object in the scene.
[tform, inlierPoints1, inlierPoints2] = ...
    estimateGeometricTransform(matchedPoints1, matchedPoints2, 'affine');
% tform_m = cp2tform(inlierPoints1,inlierPoints2,'piecewise linear');
% TFORM = cp2tform(movingPoints,fixedPoints,'piecewise linear')
%%
% Display the matching point pairs with the outliers removed
showMatchedFeatures(img1, img2, inlierPoints1, inlierPoints2, 'montage');
title('Matched Points (Inliers Only)');

%% detect difference
imgw = imwarp(oimg1, tform);
gim1 = rgb2gray(imgw);
gim2 = rgb2gray(oimg2);
sub = abs(gim1 - gim2);
imshow(sub);

Tags: thein图像imagetitlesteppointsfigure
2条回答

我不完全确定它是否能解决您的问题,但您可能需要考虑使用:

Template Matching

,从Scikit-Image定位表观子集。从你的描述看来,你已经做了类似的事情,但仍然有一些位置上的差异。如果我们讨论的是微小的差异,请考虑给出一个公差,并在一个窗口中测试所有的平均差异。假设你的子集合在位置i,j。在一个窗口[i-10,i+10],[y-10,y+10]中测试所有的平均差,会给你一个确切的位置,其中这个数字更小,几率说这是你的正确位置(注意,这可能是计算机密集型的)。从这一点上来说,只要按照你自己的建议去做,来对比一下这些差异。在

匹配位置,然后运行:

I1 = imread('image1.jpg');
I2 = imread('image2.jpg');
Idif = uint8(abs(double(I1)-double(I2)))-40;
Idif = uint8(20*Idif);
imshow(Idif)   
hold on
himage = imshow(I1);
set(himage, 'AlphaData', 0.4);

如果有必要的话就加上圆圈。此代码将发现并突出显示这些差异。我希望有帮助。在

相关问题 更多 >