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);