MATLAB: Saving images in JPG format, as grayscale, but i get white image in full

saving imagewhite image in full

I tried to save an output image in a file using imwrite(output_image , 'filename.jpg' , 'JPG'); but i get white image in full in the folder which confusing me because i did it in an example program but when i try it in the desired program i get the white image in full , i searched for solutions for this problem .. note / i'm using this line for reading image
original_image = double(imread('filename.jpg'));
is that gives any clue to the solution….

Best Answer

When you imread() a .jpg image, you will very likely get a uint8 array back. You then double() that uint8 array, getting a double array. You then do something with that double array. Finally you try to imwrite() the double array. But in MATLAB, the data type that you imwrite() determines what range of values are expected; for uint8() the range 0 to 255 is expected but for double() the range 0 to 1 is expected. Since your values are likely in the range 0 to 255 (because that what was read in) but are datatype double, MATLAB will think that nearly all of the values are maximum saturation.
The solution is to use uint8() on the data to be written:
imwrite(uint8(output_image), 'NewFile.jpg')
Related Question