views:

115

answers:

2

Hi,there are 2 formulae which i am finding it difficult to represent in Matlab. Let there be two RGB images A and B of same size with m,n representing rows and column and the third dimension d=3. The formula1 basically calculates the rate of change of pixels if A be the original image and B be the distorted version. Formula2 calculates the average rate of change of pixels.

1. Formula1= { sum(C(m,n,d)) / (m * n)} * 100

  where C(m,n)=0, if A(m,n)=B(m,n)
                =1, if A(m,n) != B(m,n)

Sum over all rows and column including the third dimension.

I have tried something like this : Formula1= sum(sum(abs(double(A)-double(B))./(m*n),1),2);

But this does not give any error. However,this is not the correct way to represent,since the if conditions are not incorporated.The problem area is how to incorporate the condition by checking if A==B or not and if A!=B.

2. Formula2 ={ 1/ (m*n)} * sum { norm (A - B) / 255} * 100 Again, here also it will be summation over all the dimension. I dont know how to form the norm of a matrix. I had posted the same question earlier,but i guess there were too many issues in representation for which it went unanswered for many days. I have deleted that question and asked afresh.Please help.

EDIT

  1. Formula3 is ={ 1/ (m*n)} * sum {(A - B) / 255} * 100 I tried out like this

    C = double(sum(A-B,3)); r=reshape(100*(C/255)/(m*n),[1 3])

But there is an error saying dimension should be same and reshape does not work.

A: 

(A ~= B) will produce a logical array, equal to true where the elements differ. You can then cast this to e.g. double, so that it will equal 1 where the elements differ.

Oli Charlesworth
@Oli, thank you for the input.
SKM
+2  A: 

For Formula1:

function r = Formula1(A,B)
[m,n,d] = size(A); %# A and B must have the same dimension
C = A ~= B; %# C has a 1 in every pixel that's different
r = double(sum(C(:)))/(m*n);
r = r/d; %# normalize by number of color planes

The ~= operator checks for inequality. (:) vectorizes the matrix, allowing us to calculate the sum over all dimensions.

For Formula2:

function r = Formula2(A,B)
[m,n,d] = size(A);
C = double(sum(A-B, 3)); %# sum over the color planes
C = C/d; %# normalize by number of color planes
K = norm(C); %# or norm(C,1), norm(C,inf) etc.
r = 100*(K/255)/(m*n);

Here, sum(A-B, 3) sums over the color planes, leaving a 2D matrix with the dimensions of the original image. There are several matrix norms, your choices are available in the documentation for NORM.

mtrw
@mtrw,These formula are to be put in a script(m file) and so there is error "Function definitions are not permitted at the prompt or in scripts." Also,just invoking function r = Formula1(A,B) will I get the output from r?
SKM
Matlab allows only one function definition per m-file. So either (a) put each of them in their own m-file, and call them from your main script or from the prompt, or (b) remove the line `r = Formula...` and copy them directly to your script or at the prompt. For your second question, yes, the result will be in `r`. See http://www.mathworks.com/help/techdoc/ref/function.html for details on function files.
mtrw
If you're analyzing RGB images, there are three color planes. If `A` and `B` are completely different, `sum(C(:))` will be `3*m*n`, not `m*n`. You have two choices: you can divide by the number of color planes, or you can use `any(C,3)` before summing to make a new matrix where the logical 1's indicate a pixel with a difference in at least one color plane. See the documentation for any: http://www.mathworks.com/help/techdoc/ref/any.html
mtrw
@It will be helpful if u can update your answer. It is true that there will be three such values,one for each color plane.
SKM
(ot: recalc as requested)
Marc Gravell
@mtrw. Thank you for the answers and explanation. Last thing,the result in formula1 and 2 gives the consolidated answer for all the 3 color planes. How to view separately the values for each color plane by modifying r = double(sum(C(:)))/(m*n);
SKM
I think the easiest way to get the result for each color plane independently is to do `squeeze(sum(sum(C)))`. The innermost sum operates across columns to make a `(1,n,d)` matrix, the next sum operates across the resulting rows to make a `(1,1,d)` matrix, and `squeeze` gets rid of the leading singleton dimensions. See http://www.mathworks.com/help/techdoc/ref/squeeze.html.
mtrw
@mtrw. Squeeze works fine for the formula1 r= squeeze(double(sum(sum(C))))/ (m*n); But the same logic does not work for formula2.Where to put squeeze in formula2 so as to get result for each color component?
SKM
As I noted in the answer, in formula 2, the statement `sum(A-B, 3)` sums over the color planes. You need to change that to `C = A-B`. Then you need to loop over the color planes, calling `norm` on each one - something like 'r = zeros(1,d); for k = 1:d, r(k) = norm(C(:,:,d));`
mtrw
I am afraid this gives error. The code for formula2 has been modified like 'C = A-B;r = zeros(1,d);for k = 1:d r(k) = norm(C(:,:,d)); red =(100*(r(k)/255))/(m*n)green=(100*(r(k)/255))/(m*n)blue=(100*(r(k)/255))/(m*n)end'
SKM
Kindly update. Thank you
SKM
What's the error?
mtrw
The error is Function 'norm' is not defined for values of class 'uint8'.Error in ==> norm at 28 [varargout{1:nargout}] = builtin('norm', varargin{:});Error in ==> test at 294 r(k) = norm(C(:,:,d));
SKM
Did you try casting `C` to `double`? See http://www.mathworks.com/help/techdoc/ref/double.html. Also, you should probably move the `red = ...; green = ...; blue = ...;` lines out of the for loop.
mtrw
Yes, now i casted C to double and moved the variables red-blue out of the loop doing which the error is no longer there. But the result for each component is the same!! Will it be same since Formula1 gives different result for each channel and hence logically formula2 should also yield different values for each band.Am i doing it correctly?
SKM
(1) `norm` returns the largest singular value of the input matrix. Are you sure that each color band has a different SVD? (2) Are the answers exactly equal or just very very close (to within the displayed precision)? You can set `format long` to get more displayed precision (see http://www.mathworks.com/help/techdoc/ref/format.html). (3) In your comment, you calculate `red`, `green` and `blue` identically. Make sure that you fix that bug.
mtrw
(2) The answers are exactly same (3) r = zeros(1,d) is calculated once for all the 3 bands, same for the For loop. Could you please elaborate how to separately calculate r(k) = norm(C(:,:,d)) and perform red =(100*(r(k)/255))/(m*n) for the other two components.
SKM
What about (1)? Do the different matrices have different SVDs? (3) You should use r(1) for red, r(2) for green, and r(3) for blue (I assume RGB images order the color planes that way, you might want to verify).
mtrw
Yes, different SVD. If i use r(1) for red then will the statement change to r(1)=norm(C(:,:,1)),similarly for the other two. So, there is no use of the for loop?
SKM
Also, r=zeros(1,d) is calculated once but it is 1*d dimension matrix (1 row 3 column). So,when r(1) is issued, then does it mean directly accessing the first color band?
SKM
Just as `C(:,:,k)` accesses the k'th matrix of the image `C`, `r(k)` accesses the k'th member of the vector `r`. Each member of `r` is a scalar, so I don't understand what you mean by "directly accessing the first color band." Each member of `r` is a scalar resulting from a calculation over each color band, if that helps. As for whether to do things in loops or line by line, that's really a matter of taste. Try it both ways if you like, make sure the answer is what you expect, and let experience guide you in your favored implementation.
mtrw
@mtr,thank you for making me learn!! But, Even on multiplying with 100 in red =100*r(1)/255/(m*n) , the result is in fraction. So should it be 100 *.r(1)/255/(m*n) ??
SKM
I think, though I'm not comfortable enough with linear algebra to say for sure, that the Singular Value Decomposition is not dependent on the size of the matrix. Therefore you probably shouldn't normalize by `m*n`. I only put the normalization in because you had it in your original formulation. At this point, you should make sure that you understand the mathematics (or physics) behind your calculation, rather than just trying to get the number into the form you expect.
mtrw
Will, the same logic go for a problem where say we dont want the norm but just the absolute difference between A,B in formula2,then eliminating norm from r(1) = norm(C(:,:,1));and following the same for each channel,we can do matrix arithmetic operations?Thank you anyways for all your help and detailed explanation.
SKM
I'm not sure I understand your question. If you're asking if `C(:,:,x)` returns a matrix for the x'th color plane, then yes, it does.
mtrw
Please see the EDIT question formula3.
SKM
Squeeze and reshape,i believe imply the same meaning.So,instead of getting three values one for each band, i am getting a matrix.How to go about this?
SKM
`sum(C,3)` sums the color planes together, resulting in an `m x n` matrix. If you want to sum each color plane, see the seventh comment.
mtrw
@okk...i think I got it.Thanx.
SKM