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

19 views (last 30 days)
NAOAKI MIYAMOTO
NAOAKI MIYAMOTO on 5 Oct 2022
Commented: NAOAKI MIYAMOTO on 6 Oct 2022
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

Answers (1)

Hernia Baby
Hernia Baby on 5 Oct 2022
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 Comment
NAOAKI MIYAMOTO
NAOAKI MIYAMOTO on 6 Oct 2022
ありがとうございます。
無事にできました!

Sign in to comment.

Categories

Find more on イメージ in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!