How to use imwrite with image object?

7 vues (au cours des 30 derniers jours)
Luke Maymir
Luke Maymir le 20 Mar 2022
Commenté : Image Analyst le 21 Mar 2022
I have an 1100x128 uint8 matrix (img) with values for an image. However, the pixels represented by these values are not square, so using imshow distorts the image. To fix this, I used the following command to properly scale the x and y axis.
im = image(x,y,img)
I would now like to save the image using the following command, but I receive an error for incorrect data type.
imwrite(im,filename)
Is there a way for me to use imwrite to save this image with the modified axes? Or do I need to somehow create a new matrix that accounts for the non-square pixels? Let me know if there are any more details I can provide!

Réponses (3)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 20 Mar 2022
You can try this command, e.g.:
imwrite(double(im),filename)
  1 commentaire
Luke Maymir
Luke Maymir le 21 Mar 2022
There is no longer an error when I use this, althought the saved image is blank. When evaluating double(im), the output is a single value (20.0002).

Connectez-vous pour commenter.


DGM
DGM le 20 Mar 2022
Modifié(e) : DGM le 20 Mar 2022
Unless you can find somewhere (e.g. in metadata) to cram the x,y extent vectors, then you may have to store them somewhere separate from the image. This might depend on what file format you're using and what your expectations are. If all you want is to specify a pixel aspect ratio, some formats might support that, but if you want to specify absolute coordinate ranges, that might be a different story.
Alternatively, you can just store the x,y vectors and the image array together in a .mat file.
  1 commentaire
Luke Maymir
Luke Maymir le 21 Mar 2022
Unfortunately, I am trying to create a gif by saving multiple images, so I am not sure if this would work.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 20 Mar 2022
You can use YData and XData in imshow() to do a linear stretch: For example if your pixels were twice as tall as wide:
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage, 'YData', [1, 2*rows], 'XData', [1, columns]);
axis on image
  2 commentaires
Luke Maymir
Luke Maymir le 21 Mar 2022
This does work! However, I am not sure how to use this with the imwrite function?
Image Analyst
Image Analyst le 21 Mar 2022
That just affects the display, not the underlying image variable. If you want to scale the image variable so that you will have square pixels you can use imresize(). For example if your pixels are tall rectangular blocks, so that for a given optical image landing on your sensor will have half as many y samples as x samples, you can change that variable with tall pixels into a standard one with square pixels like this
[rows, columns, numberOfColorChannels] = size(yourImage);
yourImage = imresize(yourImage, [rows*2, columns]);
Now your image will have the same field of view but instead of N tall pixels rows you will have 2*N square pixel rows. Then you can use imwrite().

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by