matlab 将图像切分为N*N像素的小块
将数据数据按照N*N的方格切割,并将图像数据输出。
图像不足的部分全部补为0。
图像以cell单元的方式输出,其排列顺序与原图的排向顺序相同。
function Divided_image = Divide_into_N_block(N)
%Brian%2012-10-06% %将数据数据按照N*N的方格切割,并将图像数据输出。 %图像不足的部分全部补为0。 %图像以cell单元的方式输出,其排列顺序与原图的排向顺序相同。 Img = imread(\'im1.png\'); % Img = rgb2gray(Img); [Img_hang , Img_lie] = size(Img); Remainder_hang = rem(Img_hang,N); Remainder_lie = rem(Img_lie,N); Supplement_hang = N - Remainder_hang; Supplement_lie = N - Remainder_lie; if (Remainder_hang > 0 ) hang_flag = 0; else hang_flag =1; end if (Remainder_lie>0 ) lie_flag = 0; else lie_flag =1; end Divided_image = divide_Image(Img,hang_flag,lie_flag,N,Img_hang,Img_lie,Supplement_hang,Supplement_lie); end function Divided_image = divide_Image(Img,hang_flag,lie_flag,N,Img_hang,Img_lie,Supplement_hang,Supplement_lie) [Img_hang , Img_lie] = size(Img); Integer_hang = floor(Img_hang/N); Integer_lie = floor(Img_lie/N); if (hang_flag== 1 && lie_flag == 1) for i = 1 : Integer_hang for j = 1 : Integer_lie Divided_image(i,j) = {Img((i-1)*N+1 : i*N , (j-1)*N+1 : j * N)}; end end elseif (hang_flag== 1 && lie_flag == 0) Img_insect_hang = zeros(Img_hang,Supplement_lie); Img_New_hang = [Img Img_insect_hang]; for i = 1 : Integer_hang for j = 1 : Integer_lie+1 Divided_image(i,j) = {Img_New_hang((i-1)*N+1 : i*N , (j-1)*N+1 : j * N)}; end end elseif (hang_flag== 0 && lie_flag == 1) Img_insect_lie = zeros(Supplement_hang,Img_lie); Img_New_lie = [Img ; Img_insect_lie]; for i = 1 : Integer_hang + 1 for j = 1 : Integer_lie Divided_image(i,j) = {Img_New_lie((i-1)*N+1 : i*N , (j-1)*N+1 : j * N)}; end end elseif (hang_flag== 0 && lie_flag == 0) Img_insect_hang = zeros(Img_hang,Supplement_lie); Img_New_hang = [Img Img_insect_hang]; [~,New_lie] = size(Img_New_hang); Img_insect_lie = zeros(Supplement_hang,New_lie); Img_New = [Img_New_hang ; Img_insect_lie]; for i = 1 : Integer_hang + 1 for j = 1 : Integer_lie+1 Divided_image(i,j) = {Img_New((i-1)*N+1 : i*N , (j-1)*N+1 : j * N)}; end end end end
调用的话,如果需要将原图像分为5*5像素,即:Divided_image = Divide_into_N_block(5)
请发表评论