如何在 MATLAB 中创建相似度矩阵? [英] How do I create a simliarity matrix in MATLAB?

查看:94
本文介绍了如何在 MATLAB 中创建相似度矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力比较多个图像.我将这些图像数据作为称为图像"的矩阵的列向量.我想通过首先计算它们的欧几里德距离来评估图像的相似性.然后我想创建一个矩阵,我可以在该矩阵上执行多次随机游走.现在,我的代码如下:

% 清除% 循环% 关闭所有%% 加载茶垫;图像 = Input.X;M = zeros(size(images, 2), size (images, 2));对于 i = 1:size(images, 2)对于 j = 1:size(images, 2)normImageTemp = sqrt((sum((images(:, i) - images(:, j))./256).^2));%需要准确选择gamma_i的值伽玛_i = 1/10;M(i, j) = exp(-gamma_i.*normImageTemp);结尾结尾

然而,我的矩阵 M 最终沿其主对角线的值为 1,其他地方的值为 0.我期待每行的前几个元素的大"值和列索引 > 4 的元素的小"值.有人可以解释一下有什么问题吗?任何建议表示赞赏.

解决方案

由于您正在尝试计算 欧几里得距离,看起来您在计算normImageTemp 时括号的位置有误.你有这个:

normImageTemp = sqrt((sum((...)./256).^2));%# ^--- 注意这个括号...

但您实际上想要这样做:

normImageTemp = sqrt(sum(((...)./256).^2));%# ^--- ...应该在这里

换句话说,您需要执行逐元素平方,然后求和,然后平方根.您现在所做的是先对元素求和,然后平方并取求和的平方根,这基本上是相互抵消的(或者实际上相当于只是取绝对值).>

顺便说一句,您实际上可以使用函数 NORM 来执行此操作为您操作,如下所示:

normImageTemp = norm((images(:, i) - images(:, j))./256);

I am working towards comparing multiple images. I have these image data as column vectors of a matrix called "images." I want to assess the similarity of images by first computing their Eucledian distance. I then want to create a matrix over which I can execute multiple random walks. Right now, my code is as follows:

% clear
% clc
% close all
% 
% load tea.mat;

images = Input.X;

M = zeros(size(images, 2), size (images, 2));

for i = 1:size(images, 2)
    for j = 1:size(images, 2)
        normImageTemp = sqrt((sum((images(:, i) - images(:, j))./256).^2));

        %Need to accurately select the value of gamma_i
        gamma_i = 1/10;

        M(i, j) = exp(-gamma_i.*normImageTemp);
    end 
end

My matrix M however, ends up having a value of 1 along its main diagonal and zeros elsewhere. I'm expecting "large" values for the first few elements of each row and "small" values for elements with column index > 4. Could someone please explain what is wrong? Any advice is appreciated.

解决方案

Since you're trying to compute a Euclidean distance, it looks like you have an error in where your parentheses are placed when you compute normImageTemp. You have this:

normImageTemp = sqrt((sum((...)./256).^2));
                  %# ^--- Note that this parenthesis...

But you actually want to do this:

normImageTemp = sqrt(sum(((...)./256).^2));
                  %#    ^--- ...should be here

In other words, you need to perform the element-wise squaring, then the summation, then the square root. What you are doing now is summing elements first, then squaring and taking the square root of the summation, which essentially cancel each other out (or are actually the equivalent of just taking the absolute value).

Incidentally, you can actually use the function NORM to perform this operation for you, like so:

normImageTemp = norm((images(:, i) - images(:, j))./256);

这篇关于如何在 MATLAB 中创建相似度矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆