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

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

New to MATLAB?

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

### JAYABHAVANI (view profile)

on 25 Aug 2012
Accepted Answer by Dishant Arora

### Dishant Arora (view profile)

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?

Sivakumaran Chandrasekaran

### Sivakumaran Chandrasekaran (view profile)

on 25 Aug 2012

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

```I1=I(1:127,1:127);figure,imshow(I1);
I2=I(1:127,128:256);figure,imshow(I2);
I3=I(127:256,1:127);figure,imshow(I3);
I4=I(127:256,127:256);figure,imshow(I4);
```
Image Analyst

### Image Analyst (view profile)

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.

## Tags

No tags are associated with this question.

## Products

Answer by Dishant Arora

### Dishant Arora (view profile)

on 25 Aug 2012
```I1=I(1:size(I,1)/2,1:size(I,2)/2,:);
I2=I(size(I,1)/2+1:size(I,1),1:size(I,2)/2,:);
I3=I(1:size(I,1)/2,size(I,2)/2+1:size(I,2),:);
I4=I(size(I,1)/2+1:size(I,1),size(I,2)/2+1:size(I,2),:);
```

try this out, it will do

Dishant Arora

### Dishant Arora (view profile)

on 25 Aug 2012

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

Sivakumaran Chandrasekaran

### Sivakumaran Chandrasekaran (view profile)

on 25 Aug 2012

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

### Dishant Arora (view profile)

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 (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

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);
uiwait(warndlg(errorMessage));
return;
end
end
% 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);
imshow(upperLeft);
subplot(2, 3, 3);
imshow(upperRight);
subplot(2, 3, 5);
imshow(lowerLeft);
subplot(2, 3, 6);
imshow(lowerRight);
```

safa

### safa (view profile)

on 1 May 2013

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

Walter Roberson

### Walter Roberson (view profile)

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

### Image Analyst (view profile)

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 (view profile)

Answer by Biza Ferreira

### Biza Ferreira (view profile)

on 1 May 2013
Edited by Walter Roberson

### Walter Roberson (view profile)

on 1 May 2013
```I=imread('images/fig1.tif');
[r c]= size(I);
A=I(1:r/2,1:c/2);
```
```B=I(1:r/2,c/2+1:c);
```
```C=I(r/2+1:r,1:c/2);
```
```D=I(r/2+1:r,c/2+1:c);
L=([B C;D A]);
figure, imshow(L), title('Image changed');
```