画像回転後も回転前と​同じ画像サイズにする​にはどうしたらよいで​すか

5 次查看(过去 30 天)
NAOAKI MIYAMOTO
NAOAKI MIYAMOTO 2022-10-5
1280×960の画像を回転させ、画像表示しその画像を保存するプログラムをつくりました。
画像サイズを変えたくないのですが、保存した画像が1334×1000になる為、
サイズ変更して画像を保存するようにしました。
結果、1281×961のファイルサイズになり元の画像サイズに戻りません。
どすればよいでしょうか
以下ソースコードです
% 画像選択
file = uigetfile('*.jpg');
% 画像読み込み
img = imread(file);
% 拡張子の置き換え
name = replace(file,'.','_');
% 180度ずつ360度まで回す
for i= 1:2
angle = 180*i;
figure();
% ファイル名
str = [name,'_',num2str(i)];
name_jpg = join(str);
% looseは中心ずれない、cropはズレる
imgr_loose = imrotate(img,angle,'crop');
%サイズ変更トライ(1280/1334)
imgr_size = imresize(imgr_loose,0.95952023);
% 余白なくして表示
imshow(imgr_size,'Border','tight');
saveas(gcf,name_jpg,'jpg');
close(gcf)
end

回答(1 个)

Hernia Baby
Hernia Baby 2022-10-5
saveasが問題だと思います。imwriteを使用すればサイズ問題は解決しました。
ついでにサイズ変更の部分も行と列に指定しました。
file = 'ngc6543a.jpg';
img = imread(file);
numrows = height(img);
numcols = width(img);
% 拡張子の置き換え
name = replace(file,'.','_');
% 180度ずつ360度まで回す
for i= 1:2
angle = 180*i;
figure();
% ファイル名
str = [name,'_',num2str(i)];
name_jpg = join(str);
% looseは中心ずれない、cropはズレる
imgr_loose = imrotate(img,angle,'crop');
%サイズ変更トライ(1280/1334) -> 行と列でサイズ変更
imgr_size = imresize(imgr_loose,[numrows numcols]);
% 余白なくして表示
imshow(imgr_size,'Border','tight');
% 書き換えたもの
% saveas(gcf,name_jpg,'jpg');
% close(gcf)
imwrite(imgr_size,[name_jpg,'.jpg']);
end
  1 个评论
NAOAKI MIYAMOTO
NAOAKI MIYAMOTO 2022-10-6
ありがとうございます。
無事にできました!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!