how can i divide an image into 2 equal parts.horizontaly,?

8 views (last 30 days)
this code works for 3 equal horizontal parts,how can i change it for 2 parts?
if true
[n,m]=size(im) % im is your image
id=fix(n/3)
im1=im(1:id,:);
im2=im(id+1:2*id,:);
im3=im(2*id+1:n,:);
end

Accepted Answer

Image Analyst
Image Analyst on 20 Apr 2015
You can use imcrop, which will work for color and gray scale images:
[rows, columns, numberOfColorChannels] = size(im);
topHalf = imcrop(im, [1, 1, columns, floor(rows/2)]);
bottomHalf = imcrop(im, [1, ceil(rows/2), columns, floor(rows/2)]);
Or if you know for a fact that it's a 2D monochrome image
[rows, columns, numberOfColorChannels] = size(im);
middleRow = floor(rows/2);
topHalf = im(1:middleRow, :);
bottomHalf = im(1+middleRow:end, :);

More Answers (1)

vahid rowghanian
vahid rowghanian on 4 Apr 2021
If you want to divide the image into squares of size gs ( e.g. 2,3,4,5,6,7,8,9,10,…), you can use the following code. This function extracts gs by gs patches from the input image and stores them into 'patch' variable. Notice that, if width or height of the input image is not a multiple of gs, then an offset equal to residual of the division won't be covered in patch extraction.
function [ patchim , npatchim ] = divideimage (im , gs)
imheight=size(im,1);
imwidth=size(im,2);
maxgsrow = floor( imheight / gs);
maxgscol = floor( imwidth / gs );
npatch = 1;
for i = 1 : maxgsrow
for j = 1 : maxgscol
rmin = ( i - 1 ) * gs + 1;
rmax = i * gs;
cmin = ( j - 1) * gs + 1;
cmax = j * gs;
%%%%% do processes
patch ( : , : , : , npatch) = im( rmin : rmax , cmin : cmax , : );
npatch = npatch + 1;
endfor
endfor
npatchim = npatch - 1;
patchim = patch;
end

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!