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

Python 如何从两幅图像中检测差异并显示差异?,python,matlab,image-processing,feature-detection,Python,Matlab,Image Processing,Feature Detection,以下是我想做的: 我有两张相似的照片。图像的位置可以不同。 所以我使用了冲浪特征检测器。对两幅图像的特征进行匹配,得到变换矩阵。 我用那个变换矩阵扭曲了第一张图像。 在结果中,与第二个图像相比有微小的偏移。 所以我不能用减法来找出差异。 如何检测差异并通过在差异周围画圆圈来显示差异 我现在正在使用matlab和python 这是我的matlab代码 %% Step 1: Read Images % Read the reference image containing the object of

以下是我想做的:

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

我现在正在使用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);

匹配位置,然后运行:

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);

如果有必要,只需添加圆圈即可。此代码将查找并突出显示差异。我希望它能有所帮助。

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


,从中查找表观子集。从你的描述看来,你已经做了类似的事情,但仍然有一些类型的位置差异。如果我们谈论小的差异,考虑给予宽容和测试窗口中的所有平均差异。假设你的子集在位置i,j。在一个窗口[i-10,i+10],[y-10,y+10]中测试所有的平均差异,将为您提供一个精确的位置,其中该数字较小,并且很可能是您的正确位置(注意,这可能是计算机密集型的)。从这一点开始,只需按照您自己的建议来对比差异。

您需要放置一些代码来显示您所做的事情。我们可以看到源图像和差异图像吗?可能有不同的原因。你不能先注册图像-使用matlab。要进行注册,您需要在两幅图像上选择一些具有匹配对应关系的关键点。然后我相信你可以使用matlab中的RANSAC算法得到变换矩阵,这将帮助你恢复原始图像。然后按照JCKaz的建议去做。你可以像我做的那样,基本上注册rgb和深度图像。谢谢,JCKaz。在我的例子中,两个图像的位置不同。所以我不能只用减法。第二个图像已从第一个图像移动。您不可能首先匹配/同步位置吗?这就是我遇到的问题。你知道吗?你能把你的文件给我吗?发布链接或发送至:johannes。kazantzidis13@alumni.imperial.ac.ukSorry. 这些图像是私人的。你可以想象自己是这样的。谢谢你的回答。我认为解决办法似乎不错。我试试看@尼古拉斯