Imwrite outputs white image

15 views (last 30 days)
Sean
Sean on 19 Aug 2014
Commented: DGM on 9 Nov 2022
Hi,
I am trying to save a slice from an image stack using imwrite but it writes blank (white) images. I suspect it is an issue with imwrite assuming a dynamic range of [0 1] while I would like to use []. Is there a way to get around this?
Example code:
for a=1:10
imshow(Ibf(:,:,a),[0 1]) %Image displays blank (completely white)
imshow(Ibf(:,:,a),[]) %Image displays correctly
imwrite(Ibf(:,:,a),'filename.jpg')); %Output is white images
end
Note: Ibf is an image stack in grayscale
  1 Comment
DGM
DGM on 9 Nov 2022
The problem is an issue of the expected scale. Regardless of class, if the image is properly scaled for its class, then it will both display and write as expected. If you have a floating point image that's improperly-scaled (i.e. not unit-scale), then it won't be written or displayed correctly.
This is almost certainly a consequence of casting an integer-class image as 'double' without scaling it accordingly. Either rescale the floating-point image to unit-scale, or cast it to the class that corresponds to its current scale. If you're going to work with intermediate images in an improperly-scaled state, that's fine, but you'll have to account for it.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 19 Aug 2014
If Ibf is a normal uint8 RGB image in the range 0-255 then of course saying it should display [0,1] as 0-255 will send 0 to 0 and 1 and anything more than that to 255. What is the class of Ibf? Is it double or uint8? If it's uint8, the imwrite should write out correctly as you have it.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!