Reputation: 989
There are two formulae which I am finding it difficult to represent in MATLAB. Let there be two RGB images, A
and B
, of the same size with m
,n
representing rows and column and the third dimension d=3. Formula1
basically calculates the rate of change of pixels if A
is the original image and B
is 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 it, 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 dimensions. I don't know how to form the norm of a matrix.
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.
Upvotes: 3
Views: 3833
Reputation: 35088
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.
Upvotes: 5
Reputation: 272487
(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.
Upvotes: 0