MATLAB Answers

0

What is the problem with my function to compute the cosine similarity of two images?

Asked by David Sidhu on 4 Apr 2017
Latest activity Answered by David Sidhu on 5 Apr 2017
I am using a script to compute the cosine similarity of two images.
I first convert the images to vectors:
Img1 = imread('test1.jpg')
Img2 = imread('test2.jpg')
ImgVector1 = Img1(:)
ImgVector2 = Img2(:)
I then use the script to compute the cosine similarity between the vectors:
cossimnew(ImgVector1,ImgVector2)
That function is as follows:
function s=cossimnew(actual,estimate)
[m,n]=size(actual);
if n==1
s=((estimate'*actual)/(norm(estimate)*norm(actual)))';
else
for i=1:n,
x=norm(estimate(:,i))*norm(actual(:,i));
if x ~= 0
s(i)=(estimate(:,i)'*actual(:,i))/x;
else
s(i)=NaN;
end
end
end
s=s';
However I have been getting the following error:
Error using *
MTIMES is not fully supported for integer classes. At least one input must be
scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
Error in cossimnew (line 8)
Can anyone point me in the right direction?
Thanks so much.

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by David Sidhu on 5 Apr 2017
 Accepted Answer

I had to convert the vector to floating point numbers.
Doing the following led to the function working (added the double function):
I1 = imread('test1.jpg');
I2 = imread('test2.jpg');
IV1 = double(I1(:));
IV2 = double(I2(:));
cossimnew(IV1,IV2)

  0 Comments

Sign in to comment.


Answer by Spencer Chen on 4 Apr 2017

Please read Matlab documentation on the differences between the "*" and ".*" notations. Also please read more about vector operations and vectorizing operations in Matlab.

  0 Comments

Sign in to comment.