Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
sara
sara am 20 Apr. 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

Akzeptierte Antwort

Image Analyst
Image Analyst am 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, :);
  3 Kommentare
Sudeep Albal
Sudeep Albal am 25 Jan. 2017
How can i obtain original image from those two parts?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

vahid rowghanian
vahid rowghanian am 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

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by