Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to segment (divide) an image into 4 equal halves?

Asked by JAYABHAVANI on 25 Aug 2012

Am working on medical imaging. i have to segment an image into 4 equal parts like 4 quadrants and each segmented image should get displayed separately. can anyone help me with the code?


if size(I)=256 * 256, follow this..

Image Analyst on 25 Aug 2012

That is not the definition of image segmentation (it's cropping), so I removed that tag. Image segmentation is when you try to identify individual regions, usually of some irregular shape that represents some object (such as a person or a care). For example, find the lungs in a CT image.



No tags are associated with this question.

3 Answers

Answer by Dishant Arora on 25 Aug 2012
Accepted answer

try this out, it will do


Dishant Arora on 25 Aug 2012

I am assuming the image u want to segment is color image(3-D)

Hi Dishant, can u explain the line...The input image is divided into many sub images . all are 2D images.Does the line indicates the same which i typed above

Dishant Arora on 25 Aug 2012

Hey Siva, I am a newbie to matlab and image processing but as much as i know the code u wrote above works well for gray scale images but if the input image is a colored(which are 3-D in nature) one it will result in error. u first need to convert the input image into gray scale using rgb2gray().

And what i am doing here is taking care of the 3rd dimension also.

Dishant Arora
Answer by Image Analyst on 25 Aug 2012

You can simply call imcrop 4 times to get the 4 images. Here, try this full demo. Just copy, paste, and run:

clc;    % Clear the command window.  
workspace;  % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get the rows and columns to split at,
% Taking care to handle odd-size dimensions:
col1 = 1;
col2 = floor(columns/2);
col3 = col2 + 1;
row1 = 1;
row2 = floor(rows/2);
row3 = row2 + 1;
% Now crop
upperLeft = imcrop(rgbImage, [col1 row1 col2 row2]);
upperRight = imcrop(rgbImage, [col3 row1 columns - col2 row2]);
lowerLeft = imcrop(rgbImage, [col1 row3 col2 row2]);
lowerRight = imcrop(rgbImage, [col3 row3 columns - col2 rows - row2]);
% Display the images.
subplot(2, 3, 2);
subplot(2, 3, 3);
subplot(2, 3, 5);
subplot(2, 3, 6);


safa on 1 May 2013

how to divide an image into 9 (3X3) equal halves too?((2X2) or (3X3)or (5X3))

Walter Roberson on 1 May 2013

Is the size of the image promised to be an exact multiple of 3 in both x and y directions?

Image Analyst on 1 May 2013

You wouldn't call them "halves", which by definition is 50% of the pixels. But anyway, to divide them into other rectangles is the same concept. Did you see how I figured out the starting and ending row and starting and ending column and then used imcrop()? Do you think you could do that? That you could figure out the rows and columns by yourself if you wanted to divide the image by 3 or 5? Just make sure you use floor(), ceil(), round(), or int32() to get rid of any fractional part.

Image Analyst
Answer by Biza Ferreira on 1 May 2013
Edited by Walter Roberson on 1 May 2013
[r c]= size(I);
L=([B C;D A]);
figure, imshow(L), title('Image changed');


Biza Ferreira

Contact us