title: Creating patches for Deeplearning using matlab
body: <p>I have a function which creates patches of 32x32 pixels for the given image. It returns a cell which contains all the patches. If the image is on format 350*350*3, it works well although if the image is of format 256*150 , it returns the cell with empty images. Funny thing is if i debug the code it create the patches inside the cell fine , but when its returning those patches inside the cell, the cell becomes empty.I am trying to save such images using the setimage code. Could anybody help with this ?</p>
  
  <pre><code>% Demo to divide a color image up into blocks.
  function [imageSet] = CreatePatches(imag)
  fontSize = 20;
  %rgbImage = imread(imag);
  rgbImage =imag;
  % imshow(rgbImage);
  % Enlarge figure to full screen.
  set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
  % drawnow;
  % Get the dimensions of the image.  numberOfColorBands should be = 3.
  [rows columns numberOfColorBands] = size(rgbImage)
  %==========================================================================
  % divide an image up into blocks is by using mat2cell().
  blockSizeR = 32; % Rows in block.
  blockSizeC = 32; % Columns in block.
  % Figure out the size of each block in rows. 
  % Most will be blockSizeR but there may be a remainder amount of less than that.
  wholeBlockRows = floor(rows / blockSizeR);
  blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
  % Figure out the size of each block in columns. 
  wholeBlockCols = floor(columns / blockSizeC);
  blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
  % Create the cell array, ca.  
  % Each cell (except for the remainder cells at the end of the image)
  % in the array contains a blockSizeR by blockSizeC by 3 color array.
  % This line is where the image is actually divided up into blocks.
  if numberOfColorBands &gt; 1
      % It's a color image.
      ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
  
  else
      ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
  end
  % Now display all the blocks.
  plotIndex = 1;
  numPlotsR = size(ca, 1);
  numPlotsC = size(ca, 2);
  
  for r = 1 : numPlotsR
      for c = 1 : numPlotsC
  %       fprintf('plotindex = %d,   c=%d, r=%d\n', plotIndex, c, r);
          % Specify the location for display of the image.
  %       subplot(numPlotsR, numPlotsC, plotIndex);
          % Extract the numerical array out of the cell
          % just for tutorial purposes.
  
          rgbBlock = ca{r,c};
          if mean2(rgbBlock) &lt; 15 % Or whatever value you want
              continue;
          end
  %       imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
          [rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
          %imwrite(ca{r,c},['image',num2str(plotIndex),'.jpeg']);
          % Make the caption the block number.
  %         caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
  %       plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
  %       title(caption);
  %       drawnow;
          % Increment the subplot to the next location.
          plotIndex = plotIndex + 1;
           imageSet ={};
           for x =1: plotIndex
               %imshow(rgbBlock);
              imageSet{end+1} = rgbBlock;
           end
      end
  
  
  end
  </code></pre>