画像から任意の範囲を2カ所以上切り出す

5 views (last 30 days)
YT
YT on 5 Dec 2021
Commented: YT on 6 Dec 2021
一枚の画像からマウスを使って任意の長方形の範囲を二カ所以上切り出したいです。
加えて、同様の操作を一つのフォルダ内に入っている全ての画像に対しても処理したいと考えています。
現在、以下のような形にしているのですがうまくいきません、ご教授いただけると幸いです。
file_path='A';   
img_path_list=dir(strcat(file_path,'*.jpg'));
img_num= length(img_path_list);
image_save_path='image=imread('0001.jpg');
imshow(image);
[x1,y1]=ginput(2);
[x2,y2]=ginput(2);
x=0;
for i=1:img_num
picture_name=img_path_list(i).name;
picture=imread(strcat(save_path,picture_name));
imshow(picture);
picture_1=imcrop(image,[x1(1),y1(1),abs(x1(1)-x1(2)),abs(y1(1)-y1(2))]);
imwrite(picture_1,[image_save_path,num2str(x),'.jpg']);
x=x+1;
picture_2=imcrop(image,[x2(1),y2(1),abs(x2(1)-x2(2)),abs(y2(1)-y2(2))]);
imwrite(picture_2,[image_save_path,num2str(x),'.jpg']);
x=x+1;
end
  5 Comments
YT
YT on 5 Dec 2021
ご回答いただきありがとうございます。
実行すると画像が表示され十字のポイントで切り出す範囲を選択するところまでは動作するのですが、その後反応がなく、切り出した画像が表示されたり保存用ファイルに画像が保存されていることも確認できないです。
何かほかに原因が考えられるのでしょうか?
当方初心者で、操作に不慣れな点があります、度重なる質問すみません。
Atsushi Ueno
Atsushi Ueno on 5 Dec 2021
ファイルパスや画像保存パスをカレントフォルダ固定にしたので意図が伝わっていないかもしれません。
回答にてその点(他にも)を修正しました。

Sign in to comment.

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 5 Dec 2021
file_path = uigetdir(pwd,'画像が入ったフォルダを選択');
img_path_list = dir(strcat(file_path,filesep,'*.jpg'));
img_num = length(img_path_list);
image_save_path = file_path; % 画像保存場所は選択した場所と同じとする
image = imread(strcat(file_path,filesep,img_path_list(1).name));
imshow(image); % 1枚目の画像を表示
[x1,y1] = ginput(2); % トリミング矩形(1か所目)を選択
[x2,y2] = ginput(2); % トリミング矩形(2か所目)を選択
x = 0;
for i = 1:img_num
picture = imread(strcat(image_save_path, filesep, img_path_list(i).name));
imshow(picture); % フォルダ内の画像を順に表示
picture_1 = picture(y1(1):y1(2),x1(1):x1(2),:); % トリミング(1か所目)
imwrite(picture_1,[image_save_path, filesep, num2str(x), '.jpg']); % 保存
x = x + 1;
picture_2 = picture(y2(1):y2(2),x2(1):x2(2),:); % トリミング(1か所目)
imwrite(picture_2,[image_save_path, filesep, num2str(x), '.jpg']); % 保存
x = x + 1;
end
  1 Comment
YT
YT on 6 Dec 2021
うまくいきました!
大変助かりました!ありがとうございます!!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!