MATLAB 标注 图像上截取Rect区域图像

Matlab里面根据鼠标的响应,截取rect区域图像,并且保存图像。

代码非常简单,里面没有做越界判断。

clc;
clear;
% label and rect 
Forder = [pwd '\images\'];
files = dir([Forder,'*.png']);
L = length(files);
num = 0; % 响应鼠标事件,文件夹里标注多少个rect_num
for i = 1 : L
  
    I = imread([Forder files(i).name]);  
    [m,n,k] = size(I);
    pro_img = I(1 : floor(m / 2),:,:);
    imshow( pro_img );
    while(1)
        
        k = waitforbuttonpress;              % 等待鼠标按下
        point1 = get(gca,'CurrentPoint');    % 鼠标按下了
        finalRect = rbbox;                   %
        point2 = get(gca,'CurrentPoint');    % 鼠标松开了
        point1 = point1(1,1:2);              % 提取出两个点(按着鼠标不动,Rect的左上角与右下角点)
        point2 = point2(1,1:2);

        if point1(1) == point2(1) | point1(2) == point2(2)
           break;
        end %结束终止条件
        
        img_rect = I(point1(2) : point2(2),point1(1) : point2(1),: );
        img_rect_path = [pwd '\images_label\' num2str(num) '.bmp'];      
        imwrite(img_rect,img_rect_path);
        num = num + 1;
        
        hold on;
    end
end


你可能感兴趣的:(MatLab)