简体   繁体   中英

How to store and read a gray scale image - MATLAB

I am currently working on some image processing algorithm, with MATLAB, which requires that an image from the directory is loaded and converted to grayscale. After some other processing, all the pixels in the image have either value 0 or 255. I then store the image using:

// temp is of type double
imwrite(temp,'FAST_tree_test_image_1_gray_matlab.jpg');

After that, I reload the image using:

// image is not defined before hand; its automatically set to type UINT8
image = imread('FAST_tree_test_image_1_gray_matlab.jpg');

when I view the values of image with Variable editor, the values are different.. almost all 255 values became 252 and many pixels which before storing the image were 0, became 2..

Why is this so? Is it something about formatting? How can I solve this? What if I had to load this image with OpenCv in Visual Studio, how will the values change?

That is normal because you're saving in JPEG format, which uses lossy compression, so you get a quite similar image but with some noise added (that's why color count is a bit more than the 2 original colors).

I'd recommend you to save your images in PNG format (it uses lossless compression and can get good compression ratios).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM