MATLAB Answers

JAYABHAVANI
0

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

Asked by JAYABHAVANI
on 25 Aug 2012
Latest activity Edited by Nandan M S
on 22 Apr 2016

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?

  3 Comments

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

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.

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

<<

 /matlabcentral/answers/uploaded_files/50473/d.jpg>>
clc;
clear all;
close all;
I=imread('d.jpg');
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');

Tags

No tags are associated with this question.

3 Answers

Answer by Dishant Arora
on 25 Aug 2012
 Accepted answer

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

  2 Comments

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


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

  3 Comments

safa
on 1 May 2013

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

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

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.


Answer by Biza Ferreira on 1 May 2013
Edited by Walter Roberson
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');

  0 Comments


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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!