How to use imwrite with image object?

7 ビュー (過去 30 日間)
Luke Maymir
Luke Maymir 2022 年 3 月 20 日
コメント済み: Image Analyst 2022 年 3 月 21 日
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!

回答 (3 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 3 月 20 日
You can try this command, e.g.:
imwrite(double(im),filename)
  1 件のコメント
Luke Maymir
Luke Maymir 2022 年 3 月 21 日
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).

サインインしてコメントする。


DGM
DGM 2022 年 3 月 20 日
編集済み: DGM 2022 年 3 月 20 日
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 件のコメント
Luke Maymir
Luke Maymir 2022 年 3 月 21 日
Unfortunately, I am trying to create a gif by saving multiple images, so I am not sure if this would work.

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 3 月 20 日
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 件のコメント
Luke Maymir
Luke Maymir 2022 年 3 月 21 日
This does work! However, I am not sure how to use this with the imwrite function?
Image Analyst
Image Analyst 2022 年 3 月 21 日
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().

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by