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

198 views (last 30 days)
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?
you can use the function that I have posted on matlab exchange. It divides the image into MxN equal sized images, then you can display them in your desired way.

Dishant Arora 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
##### 2 CommentsShow 1 older commentHide 1 older comment
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 on 7 Mar 2021
Edited: Image Analyst on 7 Mar 2021
Since everyone seems to want a different number, I've created this general purpose demo where you can specify how many strips vertically and horizontally you want the image to be divided into.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
[rows, columns, numColorChannels] = size(grayImage)
imshow(grayImage);
axis on;
impixelinfo
numBandsVertically = 4;
numBandsHorizontally = 3;
topRows = round(linspace(1, rows+1, numBandsVertically + 1))
leftColumns = round(linspace(1, columns+1, numBandsHorizontally + 1))
% Draw lines over image
for k = 1 : length(topRows)
yline(topRows(k), 'Color', 'y', 'LineWidth', 2);
end
for k = 1 : length(leftColumns)
xline(leftColumns(k), 'Color', 'y', 'LineWidth', 2);
end
% Extract into subimages and display on a new figure.
hFig2 = figure();
plotCounter = 1;
for row = 1 : length(topRows) - 1
row1 = topRows(row);
row2 = topRows(row + 1) - 1;
for col = 1 : length(leftColumns) - 1
col1 = leftColumns(col);
col2 = leftColumns(col + 1) - 1;
subplot(numBandsVertically, numBandsHorizontally, plotCounter);
subImage = grayImage(row1 : row2, col1 : col2, :);
imshow(subImage);
caption = sprintf('Rows %d-%d, Columns %d-%d', row1, row2, col1, col2);
title(caption);
drawnow;
plotCounter = plotCounter + 1;
end
end
hFig2.WindowState = 'Maximized';
fprintf('Done running %s.m.\n', mfilename); Walter Roberson on 16 May 2021
Consider using mat2cell() to split the array apart, and using cell2mat() to put it back together.
For the case where the image is to be divided into a fixed number of blocks as evenly as practical, but the blocks not all being exactly the same size (e.g, you cannot divide 512 pixels into 3 equal partitions):
Nxblk = 3; Nyblk = 5;
yblksizes = diff(round(linspace(1, size(TheImage,1)+1, Nyblk+1)));
xblksizes = diff(round(linspace(1, size(TheImage,2)+1, Nxblk+1)));
ImageCell = mat2cell(TheImage, yblksizes, xblksizes, size(TheImage,3));
ImageReconstructed = cell2mat(ImageCell);

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
% 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);
Image Analyst on 29 Mar 2019
imshow(upperLeft, 'upperLeft.png');
imshow(upperRight, 'upperRight.png');
imshow(lowerLeft, 'lowerLeft.png');
imshow(lowerRight, 'lowerRight.png');

Biza Ferreira on 1 May 2013
Edited: Walter Roberson on 1 May 2013
[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');
Mustafa Ahmed on 7 Mar 2021
tried , only shows the four parts

Zaidi Shoaib on 17 Dec 2022
Moved: Image Analyst on 17 Dec 2022
clc;
clearvars;
close all;
[x, y, z]=size(a);
x1=x/2;
y1=y/2;
A=a(1:x1,y1:end,:);
subplot(321)
imshow(a);
title('Original Image')
subplot(322)
imshow(A);
B=a(1:x1,1:y1,:);
subplot(323)
imshow(B);
C=a(x1:end,1:y1,:);
subplot(324)
imshow(C);
D=a(x1:end,y1:end,:);
subplot(325)
imshow(D);
L=([B A;C D]);
subplot(326)
imshow(L)
title('Re-Construct Orig Image') DGM on 17 Dec 2022
If you have MIMT, this becomes incredibly simple. MIMT imdetile() requires no special consideration of geometry divisibility, number of channels, etc.
Note that in this example [384 512] is not integer-divisible by [5 6]. How that's resolved is a matter of the selected options. Here, I'm just using the defaults. The ordering of the tiles (the direction) can be specified. In this case, I'm using row-wise detiling, since that's all that montage() supports.
% split the image into 30 tiles in a 5x6 pattern
tiling = [5 6]; % [y x]
subimages = imdetile(inpict,tiling,'direction','row'); % 77x85x3x30
montage(subimages,'size',tiling,'bordersize',[5 5],'backgroundcolor','w') The subimages all have the same geometry and are returned as a 4D image instead of an unmanageable pile of named variables. If it's preferred to handle the output as a cell array, that can easily be done by using num2cell() on the output of imdetile().
subimages = imdetile(inpict,tiling,'direction','col'); % detile columnwise for simplification
subimages = squeeze(num2cell(subimages,[1 2 3])); % convert to 30x1 cell
subimages = reshape(subimages,tiling); % reshape into 5x6 cell for easy indexing

### Categories

Find more on Get Started with Image Processing Toolbox in Help Center and File Exchange

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!