'image' behaviour for 'imwrite'
2 ビュー (過去 30 日間)
古いコメントを表示
I have these lines of code to display values of a matrix as an image:
x=linspace(-20, 20, 1000);
y=linspace(0, 115, 1482);
image(x, y, img)
colormap(gray(127))
axis('image')
axis([-20 20 30 90])
img is a 1482x1000 matrix. The specific numers don't matter. The code is from here if that's helpfull.
This code creates a figure with the correctly scaled and framed image. My goal now is not to open a figure, but to save the image to a file (preferably .png). If I just use
imwrite(img, save_path)
then the aspect ratio of the image is wrong.
So my question is basically: how can I use the syntax or behaviour of 'image' and don't open a figure but save the image. I don't want to save the figure that is created by 'image'. The resolution is lower and there is a white border.
0 件のコメント
回答 (1 件)
Image Analyst
2020 年 12 月 14 日
If you don't want to save img (perhaps it is already saved) but would rather save a screenshot, then you can use exportgraphics(). I presume you have a 2020 release but you blew by the field on the post submission and forgot to tell us your release. exportgraphics() came out this year. Otherwise try saveas().
4 件のコメント
Image Analyst
2020 年 12 月 16 日
iwrite just makes an image where each element is a square pixel. If you need a different aspect ratio, like one to match your x and y, then you need to computer the new size [rows, columns] based on the range of x and y, then call imresize().
How big is the original img?
[rows, columns, numberOfColorChannels] = size(img) % No semicolon
Now, you have
x=linspace(-20, 20, 1000);
y=linspace(0, 115, 1482);
so x is 1000 columns wide, and has values that range from -20 to +20, and y is 1482 rows tall and has values that range from 0 to 115.
Tell me how many rows tall and columns wide you'd want your output image? 1482 tall and 1000 wide? Or do you want to use the aspect ratio of x and y and one of their dimensions to compute the size? Like
% Compute the aspect ratio of width over height:
aspectRatio = (x(end)-x(1)) / (y(end)-y(1));
% Use that factor to scale the original image's rows and columns.
outputRows = rows
outputColumns = round(columns * aspectRatio)
outputImage = imresize(img, [outputRows, outputColumns]);
% outputImage will have the same number of rows as img but the aspect ratio of x and y
Alternatively you could have the original number of columns of img:
% Compute the aspect ratio of width over height:
aspectRatio = (x(end)-x(1)) / (y(end)-y(1));
% Use that factor to scale the original image's rows and columns.
outputRows = round(rows / aspectRatio)
outputColumns = columns
outputImage = imresize(img, [outputRows, outputColumns]);
% outputImage will have the same number of rows as img but the aspect ratio of x and y
Or you could have the same number of rows as y has elements
% Compute the aspect ratio of width over height:
aspectRatio = (x(end)-x(1)) / (y(end)-y(1));
% Use that factor to scale the original image's rows and columns.
outputRows = round(length(y) / aspectRatio)
outputColumns = length(x)
outputImage = imresize(img, [outputRows, outputColumns]);
% outputImage will have the same number of rows as img but the aspect ratio of x and y
The aspect ratio of x and y is 40/115 = 0.35, while the aspect ratio of the number of elements of them is 1000/1482 = 0.67. Why are these not the same? An image has to have square pixels (unless you plan on doing tons of math to account for non-square pixels).
There are a lot of possible combinations and I don't want to list them all. Why don't you just tell me how many pixels you expect the output image to be?
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!