画像から任意の範囲を2カ所以上切り出す
4 views (last 30 days)
Show older comments
一枚の画像からマウスを使って任意の長方形の範囲を二カ所以上切り出したいです。
加えて、同様の操作を一つのフォルダ内に入っている全ての画像に対しても処理したいと考えています。
現在、以下のような形にしているのですがうまくいきません、ご教授いただけると幸いです。
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
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on イメージ算術 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!