1. 程式人生 > >用matlab給影象分塊並儲存子圖

用matlab給影象分塊並儲存子圖

[FileName,PathName] = uigetfile('*.*','Select the image');  
Im=imread([PathName FileName]);
imshow(Im)
hold on
L = size(Im);
height=160;
width=160;
max_row = floor(L(1)/height);%實驗圖片為800*1280,則max_row=5,max_col=8
max_col = floor(L(2)/width);
seg = cell(max_row,max_col);
%分塊
for row = 1:max_row      
    for col = 1:max_col        
    seg(row,col)= {Im((row-1)*height+1:row*height,(col-1)*width+1:col*width,:)};   
    end
end 
for i=1:max_row*max_col
imwrite(seg{i},strcat('m',int2str(i),'.bmp'));   %把第i幀的圖片寫為'mi.bmp'儲存
end
%畫出分塊的邊界
for row = 1:max_row      
    for col = 1:max_col  
 rectangle('Position',[160*(col-1),160*(row-1),160,160],...
         'LineWidth',2,'LineStyle','-','EdgeColor','r');
        end
end 
hold off