[Grid] Crop a grid of images in Matlab

This function makes you get a crop a grid in Matlab. So it crops multiple images out of one image and puts it in an image array. You can define the bordersize of each gridpatch, and you can also define the border for the entire grid you put in. There is an image file included. Also, you can download this as a zipfile or see the code below.

Download

Gridsplit.zip (includes code+example)

Example

  1. %This is an example of how to use the gridsplit Matlab function
  2. clc;clear all;close all
  3.  
  4.  
  5. im=imread('testim.jpg');
  6.  
  7. %The size of the grid
  8. grid_x=6;
  9. grid_y=4;
  10.  
  11. %Defines the factor you want inside each grid (1=everything 0=nothing)
  12. width_fact=.5;
  13. height_fact=.5;
  14.  
  15. %Define what to exclude in the border
  16. left_extra=70;
  17. right_extra=50;
  18. top_extra=40;
  19. bottom_extra=80;
  20.  
  21. im_array=gridsplit(im,grid_x,grid_y,width_fact,height_fact,left_extra,right_extra,top_extra,bottom_extra);
  22.  
  23. i=0;
  24. for y=[1:grid_y]
  25.     for x=[1:grid_x]
  26.         i=i+1;
  27.         subplot(grid_y,grid_x,i), imshow(im_array(:,:,:,i))
  28.     end
  29. end

Function Code

  1. function im_array=gridsplit(im,grid_x,grid_y,width_fact,height_fact,left_extra,right_extra,top_extra,bottom_extra)
  2. %Crops a grid of images out of one image
  3. %   im_array=gridsplit(im,...) returns an image array im_array that has
  4. %   size (MxNxPxR) in which M the width, N the height, P the channel and R
  5. %   the amount of images destilled from the grid. So it puts all the
  6. %   gridparts in R which has size (grid_x*grid_y)
  7. %   You get the idea. Otherwise more info on: www.timzaman.nl
  8. %
  9. %   grid_x       = Amount of horizontal patches
  10. %   grid_y       = Amount of vertical patches
  11. %   width_fact   = You can define a horiz. border for each grid here
  12. %                  (1=no border 0=nothing)
  13. %   height_fact  = You can define a vertical border for each grid here
  14. %                  (1=no border 0=nothing)
  15. %   left_extra   = The left border it should exclude from the orig. image
  16. %   right_extra  = The right border it should exclude from the orig. image
  17. %   top_extra    = The top border it should exclude from the orig. image
  18. %   bottom_extra = The bottom border it should exclude from the orig. image
  19. %
  20. %   Examples
  21. %   --------
  22. %   Gets a 4x6 grid from an image
  23. %
  24. %       I = imread('test.jpg');
  25. %       im_array=gridsplit(im,6,4,0.5,0.5,10,10,10,10);
  26. %       figure, imshow(I), figure, imshow(im_array(:,:,:,1))
  27. %
  28. %   Note
  29. %   ----
  30. %   This is the first version without much support
  31. %
  32. %   Class Support
  33. %   -------------
  34. %   The input image; bitonal, grayscale, or rgb; and should be 8bit of 16bit unsigned
  35. %
  36. %   See also makecform, applycform
  37. %   Written by Tim Zaman, TU Delft, 2011
  38. %   This work, unless otherwise expressly stated, is licensed under a
  39. %   Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  40. %
  41. %   THIS SOFTWARE IS PROVIDED BY TIM ZAMAN ''AS IS'' AND ANY
  42. %   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. %   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. %   DISCLAIMED. IN NO EVENT SHALL <copyright HOLDER> BE LIABLE FOR ANY
  45. %   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. %   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. %   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. %   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. %   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. %   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. %
  52. %
  53.  
  54. sz=size(im);
  55.  
  56. %Reduce width for grid-division later
  57. sz(1)=sz(1)-(top_extra+bottom_extra);
  58. sz(2)=sz(2)-(left_extra+right_extra);
  59.  
  60. %Calculate gridsizes and widths for later
  61. gridsize=round([sz(1)/grid_y sz(2)/grid_x]);
  62. gridsize_real=round([height_fact width_fact].*gridsize);
  63. gridskip_left=round(((1-[height_fact width_fact])/2).*gridsize);
  64.  
  65. %Calculate start and end positions [x]
  66. x_start=round(([0:grid_x-1]*(sz(2)/grid_x))+1+left_extra);
  67. x_start=x_start+gridskip_left(2);
  68. x_end=x_start+gridsize_real(2);
  69.  
  70. %Calculate start and end positions [y]
  71. y_start=round(([0:grid_y-1]*(sz(1)/grid_y))+1+right_extra);
  72. y_start=y_start+gridskip_left(1);
  73. y_end=y_start+gridsize_real(1);
  74.  
  75. %Preallocation for speed
  76. i=0;
  77. if strcmp(class(im),'uint16')==1
  78.     im_array=uint16(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y));
  79. else
  80.     im_array=uint8(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y));
  81. end
  82.  
  83. %Fills the array
  84. for y=1:grid_y
  85.     for x=1:grid_x
  86.         i=i+1;
  87.         im_array(:,:,:,i)=im(y_start(y):y_end(y),x_start(x):x_end(x),:);
  88.     end
  89. end

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...

2 Responses

  1. Kiyo says:

    Hi, thank you so much for sharing your code.
    May I ask how can I apply your code if I want to incorporate with rgb2gray? I.e. im = rgb2gray(imread(‘x.jpg’));
    I’ve tried various methods but I keep getting the error that says “Attempted to access sz(3); index out of bounds because numel(sz)=2”. Any idea how I can resolve this error?

  2. Vibudh says:

    i wanted to crop those images in separate files.