机器学习快速截图工具matlab版本——文件夹批量处理,原创

简要说明:

1、打开文件夹后,遍历所有JPG格式图片,在同目录下新建一个CROP的文件夹存放裁剪的图片。

2、对每张图片,

(1)初步框选你要裁剪的矩形框,会自动以你框选的左上点为起点,裁剪大小为长宽自动扩展,结果会自动保存值..\Crop文件夹

(2)图片show出来后,也可以不用框选,鼠标点击你要截图的中心位置即可,然后点一下,就会自动save。

 1 %% use mouse to rect picture,and auto change to next picture
 2 %  《机器学习快速截图》 by 亦行之 20190124 15:26:30
 3 %以鼠标点击点位实现截图,文件夹批量处理
 4 clear;clc;clear all;
 5 file_path = uigetdir(\'*.*\',\'Please Select Folder\');
 6 img_path_list = dir(strcat(file_path,\'\\',\'*.jpg\'));
 7 mkdir(strcat(file_path,\'\Crop\'));
 8 img_num = length(img_path_list);
 9 I=cell(1,img_num);
10 if img_num > 0 
11     for j = 1:img_num 
12        image_name = img_path_list(j).name;
13        image = imread(strcat(file_path,\'\\',image_name)); 
14        I{j}=image; 
15        %检查当前图片大小
16        width=size(I{j},2);
17        length=size(I{j},1);
18        %显示图像,
19        imshow(image);
20        % 实现鼠标框选并记录选框的坐标
21        pos = getPosition(imrect);        
22        %设定截图大小为224*224pixel,以鼠标点击的位置作为截图的中心
23        x_crop = 223;
24        y_crop = 223;
25        %计算截图的起始和终点位置
26        col=round(pos(1)-x_crop/2) : round(pos(1) + x_crop/2); 
27        row=round(pos(2)-y_crop/2) : round(pos(2) + y_crop/2);   
28        %检出是否越界
29        if (pos(1)+x_crop/2) > width
30            col = round(width-x_crop) : round(width); 
31        end
32        if (pos(2)+y_crop) > length
33            row = round(length-y_crop) : round(length); 
34        end
35        %生成裁剪后的图片并显示(延时0.3s)
36        subwin=image(row,col,:);
37        figure;
38 %        imshow(subwin);
39 %        pause(0.3);
40        %保存图片到对应的文件夹下,并关闭当前文件的图片
41        imwrite(subwin,strcat(file_path,\'\Crop\\',image_name));
42        close all;    
43        %当图片都检测完毕,提示截图结束。
44        if j == img_num
45           h = msgbox(\'All picture have been cropped!\')
46        end
47     end
48 end

机器学习快速截图工具matlab版本——文件夹批量处理(原创)