Reputation: 570
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 !!!
Upvotes: 2
Views: 964
Reputation: 125874
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.
Upvotes: 3
Reputation: 4255
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?
Upvotes: 2