To save an image using imwrite

8 views (last 30 days)
Upasana Baishya
Upasana Baishya on 18 Jan 2022
Edited: DGM on 31 Jan 2022
I'm trying to save an image using imwrite as
I've two intensity images Ix and Iy which I got, using two electric field Ex and Ey. The value of Ey is lower than the value of Ex (10^-3 times) . Now if I use imwrite to save Ix image, it gives me an image with intensity but when i try exactly same with Iy, it doesn't give me any intensity image. The whole image appears to be black. when I use contourf then I can see the image pattern for Iy. Can I use any other comand to save the image except from saveas.
  1 Comment
Voss on 19 Jan 2022
Can you attach two example images, one for Ix and one for Iy?

Sign in to comment.

Accepted Answer

DGM on 19 Jan 2022
Edited: DGM on 31 Jan 2022
You'll need to decide whether you want to scale the data. Toward that end, you'll probably need to decide whether these images are intended only for visualization, or whether they are to be stores of data to be used for further analysis.
Images are represented by imshow() and imwrite() as relative to the black and white levels implied by the numeric class of the data. For floating-point data, these limits are [0 1]. For integer classes, the limits are the minimal and maximal values representable in the particular class.
If you have a floating point image where the entire data range is near zero (e.g. [0 1E-3]), then it is essentially all black. When this image gets quantized (e.g. to uint8) when writing to a PNG, all the detail will be lost. If there are any nonzero pixels left, the image may only be represented in a handful of discrete gray levels. Similarly, if the data lies outside [0 1], those pixels will be represented by either black or white. Subminimal and supramaximal values are simply truncated.
If the goal is simply to produce an image for visualization, you can normalize the image using mat2gray() or normalize(). Alternatively, you could apply a colormap. Either way, the pixel values are then only a relative indicator of the original data. You would need to decide how you want to handle this rescaling.
Consider the case of normalization with mat2gray(). The easy way would be to normalize to data extrema:
B = mat2gray(A);
That would allow your image to make full use of the nominal range expected by imwrite(), but if you ever wanted to reconstruct your original data, you would need to know what those extrema were. Alternatively, you could normalize with respect to some preselected limits:
B = mat2gray(A,[lowerlim upperlim]);
Doing that might allow you to more easily reconstruct the original data from the images (so long as you keep track of these limits). It might also allow you to represent two images with a similar data range on a common scale for purposes of visual comparison.
On the other hand, if your goal is to save data for further processing, it would probably be better to just use a .mat file. If you want to do so via image formats instead, perhaps pay close consideration to the numeric class you're using and the data range with respect to what that class can represent. Imwrite() can do PNG in integer widths up to uint16. TIFF can support floating-point data with some frustration, but I doubt there's much advantage compared to using a .mat file at that point, as a floating-point TIFF doesn't exactly have advantages in terms of portability or compactness.

More Answers (0)


Find more on Images 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!