Im2col()函数和col2im()函数
clear all;clc;
im=imread('lena.bmp');%512*512
[height,weight]=size(im);
im1=im2col(im,[16,16],'distinct');%256*1024正好能整除的情况
figure(1),imshow(im);
im2=col2im(im1,[16,16],[height,weight],'distinct');
figure(2),imshow(im2);
imm=imread('barbara103128.gif');
[height1,weight1]=size(imm); %103*128
im3=im2col(imm,[16,16],'distinct'); %256*56(56=7*8;7=ceil(103/16))
figure(3),imshow(imm);
im4=col2im(im3,[16,16],[height1,weight1],'distinct');
figure(4),imshow(im4);
“distinct”按列进行划分,如果不是整数块,则在填充0。图像分块和复原都没有问题,不管是不是能整除,最后都能恢复成功。“”sliding“”不填充0,小块block在原矩阵上按列滑动,使用col2im函数,不能恢复原矩阵,需要自己编写程序实现,因为有重叠,所以相对要复杂一些,查了源代码,现在还是不能成功写出来恢复程序。
查看MATLAB源代码
function a = col2im(b,block,mat,kind)
mpad = rem(mat(1),block(1)); %求余数,为的是计算需要填充几行0向量
if mpad>0, mpad = block(1)-mpad; end
npad = rem(mat(2),block(2)); %求余数,为的是计算需要填充几列0向量
if npad>0, npad = block(2)-npad; end
mpad = mat(1)+mpad; npad = mat(2)+npad;
if mpad*npad/prod(block)~=n,
error('Images:col2im:inconsistentSize','The column size of b not consistent with BLK2COL size.');
end
mblocks = mpad/block(1);
nblocks = npad/block(2);
aa = mkconstarray(class(b), 0, [mpad npad]);
x = mkconstarray(class(b), 0, block);
rows = 1:block(1); cols = 1:block(2);
for i=0:mblocks-1,
for j=0:nblocks-1,
x(:) = b(:,i+j*mblocks+1);
aa(i*block(1)+rows,j*block(2)+cols) = x;
end
end
a = aa(1:mat(1),1:mat(2));
elseif kind(1)=='s', % sliding
a = reshape(b,mat(1)-block(1)+1,mat(2)-block(2)+1);
else
error('Images:col2im:unknownBlockType',[deblank(kind),' is a unknown block type']);
end
-