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

7 vues (au cours des 30 derniers jours)
sara
sara le 20 Avr 2015
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

Réponse acceptée

Image Analyst
Image Analyst le 20 Avr 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, :);
  3 commentaires
Sudeep Albal
Sudeep Albal le 25 Jan 2017
How can i obtain original image from those two parts?
Image Analyst
Image Analyst le 25 Jan 2017
fullImage = [topHalf; bottomHalf];

Connectez-vous pour commenter.

Plus de réponses (1)

vahid rowghanian
vahid rowghanian le 4 Avr 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

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by