Last active
December 22, 2015 00:28
-
-
Save erogol/6389248 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function im_array=gridsplit(im,grid_x,grid_y,width_fact,height_fact,left_extra,right_extra,top_extra,bottom_extra) | |
%Crops a grid of images out of one image | |
% im_array=gridsplit(im,...) returns an image array im_array that has | |
% size (MxNxPxR) in which M the width, N the height, P the channel and R | |
% the amount of images destilled from the grid. So it puts all the | |
% gridparts in R which has size (grid_x*grid_y) | |
% You get the idea. Otherwise more info on: www.timzaman.nl | |
% | |
% grid_x = Amount of horizontal patches | |
% grid_y = Amount of vertical patches | |
% width_fact = You can define a horiz. border for each grid here | |
% (1=no border 0=nothing) | |
% height_fact = You can define a vertical border for each grid here | |
% (1=no border 0=nothing) | |
% left_extra = The left border it should exclude from the orig. image | |
% right_extra = The right border it should exclude from the orig. image | |
% top_extra = The top border it should exclude from the orig. image | |
% bottom_extra = The bottom border it should exclude from the orig. image | |
% | |
% Examples | |
% -------- | |
% Gets a 4x6 grid from an image | |
% | |
% I = imread('test.jpg'); | |
% im_array=gridsplit(im,6,4,0.5,0.5,10,10,10,10); | |
% figure, imshow(I), figure, imshow(im_array(:,:,:,1)) | |
% | |
% Note | |
% ---- | |
% This is the first version without much support | |
% | |
% Class Support | |
% ------------- | |
% The input image; bitonal, grayscale, or rgb; and should be 8bit of 16bit unsigned | |
% | |
% See also makecform, applycform | |
% Written by Tim Zaman, TU Delft, 2011 | |
% This work, unless otherwise expressly stated, is licensed under a | |
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. | |
% | |
% THIS SOFTWARE IS PROVIDED BY TIM ZAMAN ''AS IS'' AND ANY | |
% EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
% DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | |
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
% | |
% | |
sz=size(im); | |
%Reduce width for grid-division later | |
sz(1)=sz(1)-(top_extra+bottom_extra); | |
sz(2)=sz(2)-(left_extra+right_extra); | |
%Calculate gridsizes and widths for later | |
gridsize=round([sz(1)/grid_y sz(2)/grid_x]); | |
gridsize_real=round([height_fact width_fact].*gridsize); | |
gridskip_left=round(((1-[height_fact width_fact])/2).*gridsize); | |
%Calculate start and end positions [x] | |
x_start=round(([0:grid_x-1]*(sz(2)/grid_x))+1+left_extra); | |
x_start=x_start+gridskip_left(2); | |
x_end=x_start+gridsize_real(2); | |
%Calculate start and end positions [y] | |
y_start=round(([0:grid_y-1]*(sz(1)/grid_y))+1+right_extra); | |
y_start=y_start+gridskip_left(1); | |
y_end=y_start+gridsize_real(1); | |
%Preallocation for speed | |
i=0; | |
if strcmp(class(im),'uint16')==1 | |
im_array=uint16(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y)); | |
else | |
im_array=uint8(zeros(gridsize_real(1)+1,gridsize_real(2)+1,sz(3),grid_x*grid_y)); | |
end | |
%Fills the array | |
for y=1:grid_y | |
for x=1:grid_x | |
i=i+1; | |
im_array(:,:,:,i)=im(y_start(y):y_end(y),x_start(x):x_end(x),:); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment