MATLAB: Image read and write problem

Image Processing Toolboximage read writeMATLAB and Simulink Student Suite

Sometimes imwrite and imread work OK, but often not. In that case in stead of numbers between 0 and 255 (RGB tif file) all numbers become 0 or 255. Nothing in between. I can't find a problem and I can't find a system in what's going wrong. Any hints? You can see in the picture waht happens. It's a very tiny tif-file, all code you can see and after write and read it's damaged. Only red shown, blue and green the same.

Best Answer

srijf2(:,:,1)=[173,173,173,173,173,173,173,173
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
38,38,38,138,38,38,38,38
255,255,255,255,255,255,255,255
255,255,255,255,255,255,255,255]
That creates double precisions numbers, class double(). When you imwrite() data that is class double() then it assumes the dynamic range is 0 (black) to 1 (full intensity), and scales and converts to uint8, like uint8(srijf2 * 255) . Because of saturation of the uint8 range, that is going to give you all 0 or 255.
Do not double() integer images unless you know exactly what you are doing. Use im2double() if you need to convert from integer images to double precision images.