How to use imwrite with image object?
3 views (last 30 days)
Show older comments
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!
0 Comments
Answers (3)
DGM
on 20 Mar 2022
Edited: DGM
on 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.
Image Analyst
on 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 Comments
Image Analyst
on 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().
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!