Calculating the MSE with a RGB and grayscale image in MATLAB - Image Processing -
how calculate mse rgb , grayscale image in matlab? have written 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 run code, error:
error using - matrix dimension must agree.
how fix error?
i'm not sure why you'd want this.... since grayscale , colour images different each other in terms of colour values.
the error you're getting quite clear. grayscale image has 1 channel while rgb image has three. trying subtract images of incompatible dimensions , error.
however, if mse calculation want, need make sure grayscale image has 3 colour channels. grayscale images have red, green , blue components equal, , simple fix repmat
should trick. can use duplicate grayscale image on multiple channels simulate colour image.
do gray
variable before computing mse:
gray = repmat(gray, [1 1 3]);
as such, doing this. btw there typo in first line. forgot closing quote mark. have additional closing brace mse calculation 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 high mse because grayscale values different original rgb values.
as side note, if don't repmat
, can achieve same thing bsxfun
automatically broadcasts grayscale image on multiple channels 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);
Comments
Post a Comment