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