【发布时间】:2017-12-14 23:23:38
【问题描述】:
我想在 Matlab 中比较两个图像(我了解到 Matlab 有更多用于比较图像和处理它们的功能)。任何人都可以提出一个好的和简单的方法来做同样的事情吗?并且图像需要完全相同。所以不需要考虑图像的亮度和位置。
我需要在两个月内完成我的项目,所以如果有人帮助我提供一个好的算法或方法,我会很高兴。
【问题讨论】:
标签: matlab
我想在 Matlab 中比较两个图像(我了解到 Matlab 有更多用于比较图像和处理它们的功能)。任何人都可以提出一个好的和简单的方法来做同样的事情吗?并且图像需要完全相同。所以不需要考虑图像的亮度和位置。
我需要在两个月内完成我的项目,所以如果有人帮助我提供一个好的算法或方法,我会很高兴。
【问题讨论】:
标签: matlab
当您将图像加载到 MATLAB 中时,它们会存储为矩阵。您可以用来比较矩阵的任何东西都可以比较图像(例如 ISEQUAL)。但是,如果您想在图像处理方面更多地比较图像,请查看图像处理工具箱的演示并查看 here 哪些演示(如果有)符合您对“比较”的定义。
【讨论】:
您可以使用 EMD 算法。它适用于直方图。下面是一些可能有帮助的代码:
function[d] = hcompare_EMD(h1,h2)
% This function calculates Earth Movers Distance between two normalized
% histograms h1 and h2. Normalized histogram is histogram h, that has at
% each i place in it, value:
% (number of picture pixels with gray level i-1) /
% (total num of pixels in picture).
% ALternative fast way:
d = sum(abs(cumsum(h1) - cumsum(h2)));
end
两个图像的直方图是这样计算的:
function[h] = histImage(img)
% This function calculates normalized histogram of image.
% Normalized histogram is histogram h, that has at
% each i place in it, value:
% (number of picture pixels with gray level i-1) /
% (total num of pixels in picture).
sum = 0;
[y,x] = size(img); % getting sizes of image
h = zeros(1,256); % creating output histogram array
for i = 1:1: y % runing on rows
for j = 1:1: x % running on colomns
% gray level is addtess to cell in output histogram array
% we add there 1 (bacause of current pixel (y,x) has this gray level
h(img(i,j)) = h(img(i,j)) + 1;
% pay attention to fact, that we use here pixel value as index!
end
end
h = h./(y*x);
end
计算两幅图像直方图之间的距离(histArray, histPattern):
function[dmap] = patDistMAp(histArray, histPattern)
% Given histograms of an image and pattern returns an array (image)
% of distance values between
% img windows and pattern. Distance values are computed between the histograms
% of the windows and the pattern using the histogram distance function
[y,x,z] = size(histArray);
dmap = zeros(y,x); % output array
for i = 1:1: y % runing on rows
for j = 1:1: x % running on colomns
hist = histArray(i,j,:);
% for k = 1:1:256 % making array 1x256 from 1x1x256
% h1(k) = hist(1,1,k); % there is a permute function,
% end % but we will use it next time)
h1 = permute(squeeze(hist),[2,1]);
% Using temp variable, as MATLAB7 wants it:
temp = hcompare_EMD(histPattern,h1);
dmap(i,j) = temp;
end
end
end
【讨论】:
a = imread('image1.jpg'); %reading images as array to variable 'a' & 'b'.
b = imread('image2.jpg');
c = corr2(a,b); %finding the correlation btwn two images
if c==1
disp('The images are same')%output display
else
disp('the images are not same')
end;
【讨论】:
图片需要完全相同?
a = imread('image1.jpg');
b = imread('image2.jpg');
result = all(size(a) == size(b));
if result
result = all(reshape(a,[],1)== reshape(b,[],1));
end
【讨论】: