1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| save_path='D:\picture\'; %获取图片所在位置 img_path_list = dir(strcat(save_path,'*.jpg')); img_num=length(img_path_list); for i = 1:img_num picture_name = img_path_list(i).name; picture_1 = imread(strcat(save_path,picture_name)); picture_2=rgb2gray(picture_1); imshow(picture_2) if i==1 [x,y]=ginput(4); imwrite(picture_2,[num2str(i),'.jpg']) picture_3 = picture_2; for m = 1:size(picture_3,1) for n = 1:size(picture_3,2) if n < x(1) || n > x(4) || m < y(1) || m > y(4) picture_3(m,n,1) = 0; end end end imwrite(picture_3,[num2str(i+10),'.jpg']); picture_4 = imcrop(picture_3,[x(1),y(1),abs(x(1)-x(4)),abs(y(1)-y(4))]); imwrite(picture_4,[num2str(i+20),'.jpg']); else imwrite(picture_2,[num2str(i),'.jpg']); picture_3 = picture_2; for m = 1:size(picture_3,1) for n = 1:size(picture_3,2) if n < x(1) || n > x(4) || m < y(1) || m > y(4) picture_3(m,n,1) = 0; end end end imwrite(picture_3,[num2str(i+10),'.jpg']); picture_4 = imcrop(picture_3,[x(1),y(1),abs(x(1)-x(4)),abs(y(1)-y(4))]); imwrite(picture_4,[num2str(i+20),'.jpg']); end end [x,y]
|