Root mean square error of two images

12 views (last 30 days)
Tey
Tey on 28 Apr 2020
Edited: Tey on 2 May 2022
What is the function to calculate RSME of two images in matlab? i use:
I = imread('C:\Users\teymo\Desktop\New folder\CPU\rgray8.bmp');
J = imread('C:\Users\teymo\Desktop\New folder\CPU\blur.bmp');
result = sqrt(mean((J-I).^2));
message = sprintf('The Root mean square error is %.3f.', result);
msgbox(message);
but i get a error string error computing that, but the message box variable does show the root square but there are more than 1 number ?
"'The Root mean square error is 0.000.The Root mean square error is 2.848.The Root mean square error is 3.040.The Root mean square error is 3.056.The Root mean square error is 3.183.The Root mean square error is 3.350." It goes on forever so im not sure how to do it correctly

Accepted Answer

Rik
Rik on 28 Apr 2020
Imagen in Matlab are either 2D or 3D. Assuming your images are already 2D, the subtraction will be element-wise, after which you have an element-wise square, followed by mean. The mean function only reduces by 1 dimension, so you end up with a vector. That means sqrt will be an element-wise operation, so pog is a vector.
If you want a single number, you need to convert your images to a vector:
I = imread('C:\Users\teymo\Desktop\New folder\CPU\rgray8.bmp');
J = imread('C:\Users\teymo\Desktop\New folder\CPU\blur.bmp');
pog = sqrt(mean((J(:)-I(:)).^2));
% ^^^ ^^^ add this
message = sprintf('The Root mean square error is %.3f.', pog);
msgbox(message);
  3 Comments
DGM
DGM on 2 May 2022
Edited: DGM on 2 May 2022
Calculating RMSE (or MSE or MAD) directly on imported image data will likely be incorrect unless precautions are taken.
Most images are going to be integer-class, so the limited range of the numeric class will result in truncation when taking the difference and when squaring.
A = imread('peppers.png'); % these are both uint8
B = imnoise(A);
rmse1 = sqrt(mean((A(:)-B(:)).^2)) % direct calculation with integers
rmse1 = 9.1003
rmse2 = sqrt(mean((double(A(:))-double(B(:))).^2)) % convert to floats
rmse2 = 24.2077
rmse3 = sqrt(immse(A,B)) % or if you have IPT, you can just use immse()
rmse3 = 24.2077
Tey
Tey on 2 May 2022
Thanks for the updates, that is correct. SSIM or IMMSE are good alternatives

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!