比较2个不同的图像

时间:2013-12-19 06:13:23

标签: matlab

我正在尝试使用corr2比较多个图像,以查看相关性的相似性。

for i=1:2
    first_img = imread(sprintf('%g.jpg',i));
    first_size = size(first_img);
    size_temp = size(first_size);
    max_size = max(size_temp);
    if max_size == 3
        first_img = rgb2gray(first_img);
        first_size = size(first_img);
    end
    for j=i+1:2
        second_img = imread(sprintf('%g.jpg',j));
        second_size = size(second_img);
        size_temp = size(second_size);
        max_size = max(size_temp);
        if max_size == 3
            second_img = rgb2gray(second_img);
            second_size = size(second_img);
        end
        if i == j
            continue;end
        if first_size ~= second_size
            continue;end
        if first_size == second_size
            correlation_fs = corr2(first_img,second_img);
            if correlation_fs == 1
                fprintf('%g is the same as %g\n',first_img,second_img);
            end
        end
    end
end

现在,当第一个图像与第三个虚拟图像相比时,问题就出现了,这与第一个图像完全相同。

219 is the same as 219
220 is the same as 220
221 is the same as 221
221 is the same as 222
224 is the same as 223
222 is the same as 221
221 is the same as 222
223 is the same as 224
218 is the same as 236
242 is the same as 232
217 is the same as 219
226 is the same as 228
220 is the same as 229
241 is the same as 251
254 is the same as 253
250 is the same as 247
253 is the same as 253
252 is the same as 248
237 is the same as 224
217 is the same as 218
225 is the same as 219
219 is the same as 223
219 is the same as 214
222 is the same as 237

我不知道为什么会出现这种情况,它应该打印图像1与图像3相同,至少是我想要的。

2 个答案:

答案 0 :(得分:0)

您正在打印整个图像矩阵而不是图像编号。尝试:

fprintf('%g is the same as %g\n',i,j)

想想first_img是什么,它是一个像素强度矩阵。所以你打印出所有的像素值。

答案 1 :(得分:0)

 fprintf('%g is the same as %g\n',first_img,second_img);

这里你传递两个图像作为参数。您应该已经传递了图像编号。

 fprintf('%g is the same as %g\n', i, j);