画像から任意の範囲を2カ所以上切り出す
Afficher commentaires plus anciens
一枚の画像からマウスを使って任意の長方形の範囲を二カ所以上切り出したいです。
加えて、同様の操作を一つのフォルダ内に入っている全ての画像に対しても処理したいと考えています。
現在、以下のような形にしているのですがうまくいきません、ご教授いただけると幸いです。
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 commentaires
Atsushi Ueno
le 5 Déc 2021
imcrop関数を使っていますが、Image Processing Toolboxを使いますか?
YT
le 5 Déc 2021
Atsushi Ueno
le 5 Déc 2021
動かない原因は取り除きました。意図しない動作をコメント頂ければフォロー致します。
file_path=pwd;
img_path_list=dir(strcat(file_path,filesep,'*.jpg'));
img_num=length(img_path_list);
image_save_path=pwd;
image=imread(img_path_list(1).name);
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(image_save_path, filesep, picture_name));
imshow(picture);
picture_1=picture(y1(1):y1(2),x1(1):x1(2));
imwrite(picture_1,[image_save_path, filesep, num2str(x), '.jpg']);
x=x+1;
picture_2=picture(y2(1):y2(2),x2(1):x2(2));
imwrite(picture_2,[image_save_path, filesep, num2str(x), '.jpg']);
x=x+1;
end
YT
le 5 Déc 2021
Atsushi Ueno
le 5 Déc 2021
ファイルパスや画像保存パスをカレントフォルダ固定にしたので意図が伝わっていないかもしれません。
回答にてその点(他にも)を修正しました。
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!