why the imread output result is not same as the input of the imwrite?

3 views (last 30 days)
if true % code data_test= [1 2 3 4; 2 5 0 6; 4 10 0 3; 5 8 6 14]; imwrite((data_test),'clown.png'); s=imread('clown.png') end run the code above,i get that s =
255 255 255 255
255 255 0 255
255 255 0 255
255 255 255 255
If i turn the data type of data_test to uint8,(imwrite(uint8(data_test),'clown.png');) the result of s is same as data_test. why the input of imwrite must turn to uint8? thank you very much.

Accepted Answer

Guillaume
Guillaume on 8 Oct 2014
When pixels are of type double, as is the cas for your data_test, intensities are assumed to be in the range 0-1. Anything below 0 is considered the same as 0, anything above 1 is considered to be 1.
Thus, when you save your data_test as double, it is just converted to: [1 1 1 1; 1 1 0 1;1 1 0 1; 1 1 1 1]. As PNG images don't support double pixels, it is then converted to uint8. Hence why, you get the result back as uint8.
Note that im2uint8 produces the same result:
im2uint8(data_test)
Now, the uint8 function is not designed for images but just for plain number, and will convert any number in the range 0-255 to unsigned 8 bit integer with the same value. Therefore there is no clipping. Compare the above with:
im2uint8(uint8(data_test))
In conclusion, if your pixels are indeed uint8, declare them as so, otherwise matlab will consider them to range from 0 to 1.

More Answers (0)

Categories

Find more on Convert Image Type 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!