views:

66

answers:

2

I wish to compute this formula in matlab

[m,n,d]=size(img1);
matrix1=sum(abs(img1-img2));
a= matrix1/ m*n ;
b=a*100;

where img1,img2 are two images of dimension 512*512*3

The objective is to obtain a single numeral value, but i am getting a matrix. The actaul formula in case of a matrix A,B with i rows and j columns is = (summation [abs(A(i,j)- B(i,j))] / m*n ) * 100 to obtain a percentile I know its very simple, but i think i am missing something !!!

+2  A: 

The sum function is returning a matrix. http://faculty.petra.ac.id/resmana/private/matlab-help/techdoc/ref/sum.html Your matrix1 is divided by a scalar (equal to 512*512) - so this also results in a matrix. I believe you wanted to divide by an m by n matrix instead.

Also you need parenthesis around m*n if you want to divide by a scalar.

Edit Matrix1 is a 1x512 row vector so you should divide it by a 512x1 column vector to get a scalar. Based on your formula, I think you're looking for a column vector where each index contains the scalar equal to 512*512, but please clarify what you're aiming to solve for - i.e. what do you mean by percentile?

gary comtois
I went through the link provided by you. Matrix1 will be a row vector containing sum of the differences. But still, how do i obtain a percentile in the form of a single numerical value. A modification of the code,if any,from the formula provided would be appreciated.
sumona
Are you looking for the percent difference of the two images?
gary comtois
@gary comtois. Yes, the numeral value will be a percentage difference of the two images
sumona
The output of the code from variable b should be something like this 99.5 or 99.2, a real number. Not exactly in this range though.
sumona
+1  A: 

The fact that your image matrices are 3-D indicates that they are truecolor RGB images. For matrix M, the first color plane M(:,:,1) is the red component, the second color plane M(:,:,2) is the green component, and the third color plane M(:,:,3) is the blue component. Since you only discuss summing over the first two dimensions in your formula, you're going to have to figure out how you want to deal with the third dimension. Here are a couple of options:

  • Apply your formula to each color plane: You can do this by calling the function SUM twice, once to sum across the columns then again to sum that result across the rows. The result matrix1 will be a 1-by-1-by-3 matrix, which can be reshaped to a 3-element column vector using the function SQUEEZE. Each element in the vector will be a summation across each color plane:

    matrix1 = squeeze(sum(sum(abs(img1-img2),1),2));
    

    Now you can use the element-wise multiply and divide operators .* and ./ to compute the final results:

    a = matrix1./(m*n);
    b = a.*100;
    
  • Convert the images to grayscale: If you only care about the color intensity, you can convert the truecolor RGB images to 2-D grayscale intensity images using the function RGB2GRAY from the Image Processing Toolbox:

    img1 = rgb2gray(img1);
    img2 = rgb2gray(img2);
    

    Then you can either call SUM twice to sum across the rows and columns, or use single-colon indexing to reshape each image to a column vector and call SUM once:

    matrix1 = sum(sum(abs(img1-img2)));
    %# OR...
    matrix1 = sum(abs(img1(:)-img2(:)));
    

One additional note...

If your image data is stored as an integer type, you will probably want to convert the image data to double precision floating point first by doing the following:

img1 = double(img1);
img2 = double(img2);

This will ensure that the result from the summation step doesn't saturate at the maximum value that the integer type can hold.

gnovice
@gnvoice. Just a clarification, when working with the first option for each color plane, the summation over third color band 3 is not specified. Still the result is shown for the third as well. How? Also, there is logical error on dividing the summation result with m*n and multiplying it with 100 so as to get a percentage. The approx answer should be something like 99.56 or 99.8(not exactly,but numeral value should be a real number).
sumona
@sumona: I updated my answer to address the issues you mentioned. The summation steps are performed first over the columns, then the rows. This results in three sums, one for each color plane in the third dimension.
gnovice
@gnovice...it was my mistake,your code works fine. Accidentally, I posted the wrong comment here. Thank you for your help.
sumona
@sumona: No problem. Also, I added a note to my answer pointing out one issue you should be aware of related to the data type of the image data.
gnovice