1. 程式人生 > >MATLAB 標註 影象上擷取Rect區域影象

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