【问题标题】:How can I detect differences from two images and show differences?如何检测两个图像的差异并显示差异?
【发布时间】:2016-03-16 21:13:47
【问题描述】:

这是我想做的:

我有两张相似的图片。图像的位置可以不同。 所以我使用了冲浪特征检测器。并从两幅图像中匹配这些特征并获得变换矩阵。 我用那个变换矩阵扭曲了第一张图像。 结果与第二张图像略有不同。 所以我不能用减法来找出差异。 如何检测差异并通过在差异周围画圈来显示?

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

【问题讨论】:

  • 你需要放一些代码来显示你做了什么
  • 我们可以看到源图像和差异图像吗?可能有不同的原因。
  • 不能先注册图像 - 使用 matlab。要进行注册,您需要在两个图像上选择一些具有匹配对应关系的关键点。那么我相信你可以在matlab中使用RANSAC算法得到变换矩阵,这将帮助你恢复原始图像。然后听从 JCKaz 的建议。
  • 您可以按照我在stackoverflow.com/questions/35909833/… 中所做的操作,它基本上注册了 rgb 和深度图像。

标签: python matlab image-processing feature-detection


【解决方案1】:

匹配位置,然后运行:

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

然后根据需要添加圆圈。此代码将找到并突出显示差异。希望对你有帮助。

【讨论】:

  • 谢谢,JCKaz。就我而言,两个图像的位置不同。所以我不能只使用减法。第二张图片与第一张图片不同。
  • 你不能先匹配/同步位置吗?
  • 这就是我遇到的问题。你有什么想法吗?
  • 您可以将您的文件提供给我吗?发布链接或发送至:johannes.kazantzidis13@alumni.imperial.ac.uk
  • 对不起。图片是私密的。你可以这样想象自己。
【解决方案2】:

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

Template Matching

,从Scikit-Image 定位明显的子集。从您的描述看来,您已经做了类似的事情,但仍然存在某种类型的位置差异。如果我们谈论的是小的差异,请考虑给出一个容差并在一个窗口中测试所有平均差异。假设您的子集位于 i,j 位置。在窗口 [i-10,i+10],[y-10,y+10] 中测试所有平均差异将为您提供一个该数字较小的确切位置,并且很有可能这将是您的正确位置(但请注意这可能是计算机密集型的)。从这一点开始,按照自己的建议来对比差异。

【讨论】:

  • 感谢您的回答。我认为解决方案似乎不错。我会试试看。 @尼古拉斯
猜你喜欢
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 2013-06-18
  • 2021-04-27
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多