[Grid] Crop a grid of images in Matlab

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. im=imread('testim.jpg');
  4. %The size of the grid
  5. grid_x=6;
  6. grid_y=4;
  7. %Defines the factor you want inside each grid (1=everything 0=nothing)
  8. width_fact=.5;
  9. height_fact=.5;
  10. %Define what to exclude in the border
  11. left_extra=70;
  12. right_extra=50;
  13. top_extra=40;
  14. bottom_extra=80;
  15. im_array=gridsplit(im,grid_x,grid_y,width_fact,height_fact,left_extra,right_extra,top_extra,bottom_extra);
  16. i=0;
  17. for y=[1:grid_y]
  18.     for x=[1:grid_x]
  19.         i=i+1;
  20.         subplot(grid_y,grid_x,i), imshow(im_array(:,:,:,i))
  21.     end
  22. 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. sz=size(im);
  54. %Reduce width for grid-division later
  55. sz(1)=sz(1)-(top_extra+bottom_extra);
  56. sz(2)=sz(2)-(left_extra+right_extra);
  57. %Calculate gridsizes and widths for later
  58. gridsize=round([sz(1)/grid_y sz(2)/grid_x]);
  59. gridsize_real=round([height_fact width_fact].*gridsize);
  60. gridskip_left=round(((1-[height_fact width_fact])/2).*gridsize);
  61. %Calculate start and end positions [x]
  62. x_start=round(([0:grid_x-1]*(sz(2)/grid_x))+1+left_extra);
  63. x_start=x_start+gridskip_left(2);
  64. x_end=x_start+gridsize_real(2);
  65. %Calculate start and end positions [y]
  66. y_start=round(([0:grid_y-1]*(sz(1)/grid_y))+1+right_extra);
  67. y_start=y_start+gridskip_left(1);
  68. y_end=y_start+gridsize_real(1);
  69. %Preallocation for speed
  70. i=0;
  71. if strcmp(class(im),'uint16')==1
  72.     im_array=uint16(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y));
  73. else
  74.     im_array=uint8(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y));
  75. end
  76. %Fills the array
  77. for y=1:grid_y
  78.     for x=1:grid_x
  79.         i=i+1;
  80.         im_array(:,:,:,i)=im(y_start(y):y_end(y),x_start(x):x_end(x),:);
  81.     end
  82. end
]]>

You may also like...

2 Responses

  1. Vibudh says:

    i wanted to crop those images in separate files.

  2. 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?