Is this how to calculate Mean Square Error for two images?
Show older comments
I have two images: lena (the original) and image_new (the reconstructed image). I would like to calculate the MSE. My code below is not working - any idea why? Matlab keeps saying there are not enough input arguments.
function MSE= MSE(lena, image_new);
[M, N] = size(lena);
error = lena - (image_new);
MSE = sum(sum(error .* error)) / (M * N);
disp(MSE);
end
2 Comments
santosh akon
on 14 Feb 2018
err = immse(lena,image_new ); fprintf('\n The mean-squared error is %0.4f\n', err);
Image Analyst
on 14 Feb 2018
You can put this down in the Answer section, along with the identical answer, rather than up here in comments (which are meant to be questions of the poster rather than answers for the poster).
Accepted Answer
More Answers (3)
Image Analyst
on 31 Jul 2015
That code won't give the right answer for uint8 images - the most common type. You need to cast to double before subtraction. But why do that at all when you can just use the built-in function immse():
MSE = immse(lena, image_new);
And you should never use size like that with images. Why not? It will give wrong number of columns if the image is RGB, which it can be even if you think it's not, if you saved the image as a JPG file. See Steve's blog: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
13 Comments
Mohammed Shoaib
on 18 Apr 2020
i need to calculate MSE to compare my dehazed image to others dehazed image. but i don't have original ground truth image. how can i calculate MSE, PSNR?... please help me
Image Analyst
on 18 Apr 2020
You can't. You can compare them to each other but if all the algorithms are incorrect, just by different amounts, then what good does it do to compare them to each other with immse()? I suggest you look to other metrics like contrast (width of the histogram).
Shubha B.
on 6 Feb 2021
how to compare two interpolated images. like bilinear and bicubic. bcoz original image size is small and interpolated image is large. I can't compare these two. is it valid of finding the MSE of two interpolated images.
Walter Roberson
on 6 Feb 2021
It is not valid to find the MSE of two images that are different sizes.
If you used bilinear and bicubic to independently interpolate one image to be larger, then you could calculate MSE between the two interpolated images. What that would tell you is an indication of how similar the two interpolated images are.
Note that MSE cannot tell the difference between a lot of small differences, compared to being mostly the same but having a few large differences. So MSE cannot tell you whether an image is "good".
Shubha B.
on 8 Feb 2021
Thank you sir.
One more question:
when I find the MSE of two interpolated image, to find error, that is subtract bicubic form bilenear . Then the MSE which I get is bicubic's or bilinear's.
Image Analyst
on 8 Feb 2021
I don't understand the question, if there even is one. You resize them to make the sizes match and then you call the built-in immse() function. What you get is what you get. If you used binlinear interpolation, you get the MSE for bilinearly interpolated images. If you used cubic interpolation, then you get the MSE for cubic interpolated images. But that is so obvious that I must not be understanding your question.
Shubha B.
on 8 Feb 2021
Sorry sir, In my case original image size smaller than interpolated image. As per your comment, I came to know that I need to resize the original image to match with with interpolated image. then I can find the MSE. is my understanding is correct sir.?
Image Analyst
on 8 Feb 2021
Yes. Hopefully they're both of the same scene with the same exposure and with the camera settings the same (angle of view, orientation, focal length, exposure time, etc.).
Walter Roberson
on 8 Feb 2021
No, resizing images upwards always involves interpolation. imresize() involves interpolation (and you have some control over what kind of interpolation.)
Resizing images downwards involves interpolation as well.
If you want to compare original images and interpolated images by using MSE then you would need to have done the interpolation keeping the same size. Which is something that would normally be talked about as "filtering" rather than interpolation.
My understanding is that you have a small image that you are making larger by using different interpolation methods, and you want to somehow compare the quality of the different interpolated images. You can compare the different interpolated images between each other using MSE, but that does not give you information about how their "quality" relates to the original image. You would need to use a different quality measure to compare the resized images and the original images.
Shubha B.
on 9 Feb 2021
Yes Walter ,ok thank you. i need to use different measure to compare resized and original image. Do you have any idea about any quality measures for this.
Image Analyst
on 10 Feb 2021
Look up "image quality metrics" in the help. You'll find functions such as brisque, niqe, piqe, ssim, psnr, immse, multissim, and multissim3. We don't know which would work best for you. Read about them and decide which you think would work best for you.
hafidz
on 6 Jul 2023
cara membandingkan 2 gambar menggunakan MSE apa berdasarkan nilai rgb dari gambar
tersebut?
DGM
on 6 Jul 2023
immse() works just the same for RGB.
Sebano
on 19 May 2020
0 votes
Hi, I am trying to quantify the symmetry of logo images and have used the "immse" (mean square error) function and the "fliplr" from left-to-right code to compare the differences in mean square error (MSE) between the orignial logo and the flipped version of the logo to quantify the symmetry. We are experiencing one issue atm with this approach and I need your help... When analysing logos that are 100% symmetric, the MSE can sometimes be super high when it should be extremely close to zero, since theres no differences between the original and flipped version. What may the issue be here? and any ideas on how to solve it?
2 Comments
Image Analyst
on 19 May 2020
You'd have to provide your m-file and the logo that you're having a question about. Use the paper clip icon. You might need to call imregister() to align them first in case the line of symmetry is not right at the middle column.
Sebano
on 20 May 2020
Image Analyst. So here is the codes that I been using and the logo you find in the attachments. As you can see, the MSE is 298 for Starbucks logo, when its super symmetric. Please advise.
Claudio Ignacio Fernandez
on 20 May 2020
Hello Everyone
I'm dealing with similar question.
I'm working with the bands of the Micasense camera, it has 5 bands (red, green, blue, NIR, red-edge). The images are 960x1280 uint16.
I'm registering the images in relation to the lens in the middle of the camera, to keep same distance from all moving images to the fixed one. Using this code I got the best visualization.
imregister(Moving1, Fixed, 'translation', optimizer, metric);
However, is there a way to compute the alignment accuracy or registration error using the RMSE expresed as number of pixels?
I'm using
error1 = immse(Moving1 , Fixed);
and my error is 3.142929187276042e+07 (??). Seems pretty far away from zero but images looks good.
However I would like to :
create a bar plot showing the RMSE and standar deviation between the pair of images.
It is possible to create a map of the error?
2 Comments
Image Analyst
on 21 May 2020
Claudio, you could add
imageSize = size(moving1); % Should be [960, 1280, 5] for your Micasense camera image.
numPixels = imageSize(1) * imageSize(2); % Rows * columns for the 3-D, 5 channel image.
RMSE = sqrt(error1) / numPixels;
Claudio Ignacio Fernandez
on 22 May 2020
Sir.
Thank for your reply.
I will work following this instructions.
Regards.
Categories
Find more on Image Quality in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!