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

JAYABHAVANI (view profile)

on 25 Aug 2012
Latest activity Commented on by Walter Roberson

Walter Roberson (view profile)

on 25 Aug 2016 at 0:51

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?

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.

Nandan M S

Nandan M S (view profile)

on 22 Apr 2016

Hey, try this ...just copy, paste & run..

<<

``` /matlabcentral/answers/uploaded_files/50473/d.jpg>>
clc;
clear all;
close all;
subplot 334
imshow(I);
[r c p]= size(I);%r-rows,c-columns,p-planes
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,:);
subplot 332
imshow(A);
title('Image part 1');
subplot 333
imshow(B);
title('Image part 2');
subplot 335
imshow(C);
title('Image part 3');
subplot 336
imshow(D);
title('Image part 4');```

on 21 Aug 2016 at 13:54

thanks a lot :) :) , the code is so useful

Tags

No tags are associated with this question.

Products

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

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.

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');
```

Ahmet Ipek

Ahmet Ipek (view profile)

on 8 Aug 2016 at 8:36

Can we again combine these pieces?

Walter Roberson

Walter Roberson (view profile)

on 25 Aug 2016 at 0:51

The line

```L=([B C;D A]);
```

is recombining the pieces, in a different order.

Join the 15-year community celebration.

Play games and win prizes!

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