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

时间:2016-03-16 06:24:31

标签: python matlab image-processing feature-detection

这就是我想做的事情:

我有两个相似的图像。图像的位置可能不同。 所以我使用了冲浪功能探测器。并从两个图像中匹配这些特征并获得变换矩阵。 我用变换矩阵扭曲了第一张图像。 结果是第二张图像有轻微的偏移。 所以我不能用减法来找出差异。 如何通过围绕差异绘制圆圈来检测差异并显示差异?

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

2 个答案:

答案 0 :(得分:0)

匹配该位置,然后运行:

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

然后根据需要添加圈子。此代码将查找并突出显示差异。我希望它有所帮助。

答案 1 :(得分:0)

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

Template Matching

,从Scikit-Image找到明显的子集。从您的描述中可以看出,您已经完成了类似的操作,但仍然存在某种类型的位置差异。如果我们讨论的是小差异,请考虑给出容差并测试窗口中的所有平均差异。假设您的子集位于i,j位置。测试窗口[i-10,i + 10],[y-10,y + 10]中的所有平均差异将给出一个确切的位置,其中该数字较小并且赔率表示这将是您的正确位置(但请注意这可能是计算机密集型的)。从这一点开始,就像你自己建议对比差异一样。