How do I create a simliarity matrix in MATLAB?

后端 未结 4 1651
温柔的废话
温柔的废话 2020-12-12 01:13

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 c

相关标签:
4条回答
  • 2020-12-12 01:38

    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);
    
    0 讨论(0)
  • 2020-12-12 01:41

    The results you're getting seem reasonable. Recall the behavior of the exp(-x). When x is zero, exp(-x) is 1. When x is large exp(-x) is zero.

    Perhaps if you make M(i,j) = normImageTemp; you'd see what you expect to see.

    0 讨论(0)
  • 2020-12-12 01:47

    there is an already implemented function pdist, if you have a matrix A, you can directly do

    Sim= squareform(pdist(A))

    0 讨论(0)
  • 2020-12-12 01:56

    Consider this solution:

    I = Input.X;
    
    D = squareform( pdist(I') );       %'# euclidean distance between columns of I
    M = exp(-(1/10) * D);              %# similarity matrix between columns of I
    

    PDIST and SQUAREFORM are functions from the Statistics Toolbox.

    Otherwise consider this equivalent vectorized code (using only built-in functions):

    %# we know that: ||u-v||^2 = ||u||^2 + ||v||^2 - 2*u.v
    X = sum(I.^2,1);
    D = real( sqrt(bsxfun(@plus,X,X')-2*(I'*I)) );
    M = exp(-(1/10) * D);
    

    As was explained in the other answers, D is the distance matrix, while exp(-D) is the similarity matrix (which is why you get ones on the diagonal)

    0 讨论(0)
提交回复
热议问题