im2col and col2im

Function im2col formats image blocks to columns and col2im does the inverse operation.

When you independently apply some operation on each image patches and then merge the patches, these functions are useful. The code is like
  img = im2double(imread(‘lena.png’));
  p  = im2col(img , [y, x], ‘sliding’);
For formatting Y-by-X size image into y-by-x size patches, im2col outputs (Y-y+1)-by-(X-x+1) matrix. To recover the same image, some website says like
  img2 = col2im(mean(p), [y, x], [Y, X], ‘sliding’);
What we obtain is (Y-y+1)-by-(X-x+1) blurry image.

If you want to same size and non-blurry image, you should carefully format the patches as following,
function [img] = mergePatch(p, y, x, Y, X)
  img = zeros(Y, X);
  coeff = zeros(Y, X);
  p_idx = 1;
  for xx=1:x
      for yy=1:y
          pp = col2im(p(p_idx,:), [y x], [Y X], ‘sliding’);
          img(yy:yy+Y-y,xx:xx+X-x) = img(yy:yy+Y-y,xx:xx+X-x)+pp;
          coeff(yy:yy+Y-y,xx:xx+X-x) = coeff(yy:yy+Y-y,xx:xx+X-x)+1;
          p_idx = p_idx+1;
  img = img ./ coeff;


4 thoughts on “im2col and col2im

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s