0
votes

How do I calculate the MSE with a RGB and grayscale image in MATLAB? I have written some code here:

I = imread('iris.jpg);
Gray = rgb2gray(I);
n = size(I);
M = n(1);
N = n(2);
MSE = sum(sum(I-Gray).^2))/(M*N);
fprintf('\nMSE:%7.2f',MSE);

However, when I run this code, I get this error:

Error using - Matrix dimension must agree.

How do I fix this error?

3
You are missing a close quote after the file name iris.jpg and you have an extra parenthesis in the line starting MSE=Dave Durbin

3 Answers

2
votes

I'm not sure why you'd want to do this.... Especially since the grayscale and colour images will be completely different from each other in terms of colour values.

The error you're getting is quite clear. The grayscale image only has one channel while the RGB image has three. You are trying to subtract images of incompatible dimensions and thus you get that error.

However, if this MSE calculation is really what you want, you need to make sure your grayscale image has three colour channels. Grayscale images have the red, green and blue components to all be equal, and so a simple fix with repmat should do the trick. We can use this to duplicate the grayscale image over multiple channels to simulate a colour image.

Do this to your Gray variable before computing the MSE:

Gray = repmat(Gray, [1 1 3]);

As such, you would really be doing this. Btw there is a typo in your first line. You forgot a closing quote mark. You also have an additional closing brace where the MSE calculation is happening.

I = imread('iris.jpg');
Gray = rgb2gray(I);

%// Change 
Gray = repmat(Gray, [1 1 3]);

n = size(I);
M = n(1);
N = n(2);
MSE = sum(sum(I-Gray).^2)/(M*N);
fprintf('\nMSE:%7.2f',MSE);

Be warned that you will probably get a high MSE because the grayscale values are completely different from the original RGB values.

As a side note, if you don't like repmat, you can achieve the same thing by bsxfun which automatically broadcasts the grayscale image over multiple channels for you:

I = imread('iris.jpg');
Gray = rgb2gray(I);

n = size(I);
M = n(1);
N = n(2);
MSE = sum(sum(bsxfun(@minus, I, Gray).^2))/(M*N); %// Change
fprintf('\nMSE:%7.2f',MSE);
0
votes

You are missing a close quote after the file name iris.jpg and you have an extra parenthesis in the line starting MSE=

The error is clear; the matrix dimensions do not agree.

Your input image is RGB, it's a 3D matrix Your greyscale image is a 2D matrix

You can;t subtract a 2D matrix from a 3D matrix

0
votes
E = imread('');
E = imz;
% choose your image directory
after apply some segmentation techniques to you image
that image save in img
imz = double(imz(:));
img=double(img(:));
ima=max(img(:));
imi=min(img(:));
m1=std(img(:));
 s1=20*log10((ima-imi)./m1);
 A = E.*E;
B = bgm.*bgm;
l2rat = sqrt(sum(B(:))/ sum(A(:)));
absd = abs(bgm);
AS = absd.^2;
maxerr = round(max(AS(:)));
% Measure Peak SNR
[peaksnr1, snr] = psnr(E, bgm);
fprintf('\n The Peak-SNR value is %0.4f', peaksnr1);
fprintf('\n The SNR value is %0.4f \n', s1);
fprintf('\n The MSE value is %0.4f \n', m1);
fprintf('\n The L2rat value is %0.4f \n', l2rat);
fprintf('\n The maxerr value is %0.4f \n', maxerr);**