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

3 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2015-4-20
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, :);

更多回答(1 个)

vahid rowghanian
vahid rowghanian 2021-4-4
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

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by