how to segment (divide) an image into 4 equal halves?
Mostrar comentarios más antiguos
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?
9 comentarios
Sivakumaran Chandrasekaran
el 25 de Ag. de 2012
Editada: Walter Roberson
el 1 de Mayo de 2013
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
el 25 de Ag. de 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
el 22 de Abr. de 2016
Editada: Nandan M S
el 22 de Abr. de 2016
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');
Asma Askri
el 21 de Ag. de 2016
thanks a lot :) :) , the code is so useful
kautsar rusydi
el 14 de En. de 2017
hey Nanda, may i ask about your code? what meaning of 'p' variable, tell me more about p/plane please? Nandan's code
Image Analyst
el 14 de En. de 2017
kautsar, p is the number of planes in the 3D array. For example in a color image it's the number of color channels, like p=3 for an RGB image. Or for a volumetric image, like for CT or MRI, p is the number of slices through the body or subject. It's always good to give at least 3 outputs of size because sometimes people might think an image is gray scale (because it looks like it) but they saved it to a BMP or JPG format file and it got converted into color. If you only give two numbers, like
[rows, columns] = size(imageArray);
then columns will be the number of color channels times the actual number of columns, which would be 3 times the number of columns if imageArray is an RGB color image. See Steve's blog for more information.
SATISH KUMAR
el 18 de Mzo. de 2017
Hi, Nandan, your code is working so nicely, i need one small help can you please merge those divided images into original size. thanks in advance
Image Analyst
el 18 de Mzo. de 2017
SATISH, to reassemble, do this:
fullImage = [A, C; B, D];
mehrdad bahadori
el 15 de Feb. de 2023
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.
Respuesta aceptada
Más respuestas (5)
Image Analyst
el 7 de Mzo. de 2021
Editada: Image Analyst
el 7 de Mzo. de 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);
% Read in image
grayImage = imread('pout.tif');
[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);

3 comentarios
Quoc-Bao Ta
el 16 de Mayo de 2021
Editada: Quoc-Bao Ta
el 16 de Mayo de 2021
Hi Image Analyst,
After implementing the algorithm for some purposes, then i want to know how to recombine all those to a original image of actual size.
Please help me with code.
Thank you so much!
Walter Roberson
el 16 de Mayo de 2021
This demonstration does not store the subImage(), so they cannot be recombined later.
Walter Roberson
el 16 de Mayo de 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
el 25 de Ag. de 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);
5 comentarios
safa
el 1 de Mayo de 2013
how to divide an image into 9 (3X3) equal halves too?((2X2) or (3X3)or (5X3))
Walter Roberson
el 1 de Mayo de 2013
Is the size of the image promised to be an exact multiple of 3 in both x and y directions?
Image Analyst
el 1 de Mayo de 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.
NAGARAJA JUJARE
el 29 de Mzo. de 2019
how do i save the individual croped image separately?
Image Analyst
el 29 de Mzo. de 2019
imshow(upperLeft, 'upperLeft.png');
imshow(upperRight, 'upperRight.png');
imshow(lowerLeft, 'lowerLeft.png');
imshow(lowerRight, 'lowerRight.png');
Biza Ferreira
el 1 de Mayo de 2013
Editada: Walter Roberson
el 1 de Mayo de 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');
7 comentarios
Ahmet Ipek
el 8 de Ag. de 2016
Can we again combine these pieces?
Walter Roberson
el 25 de Ag. de 2016
The line
L=([B C;D A]);
is recombining the pieces, in a different order.
shafaa abdullah
el 7 de Feb. de 2017
Editada: shafaa abdullah
el 7 de Feb. de 2017
thank you but why the orginal image converted to grayscale i want C piece remain colored piece please help me i have an exam
Walter Roberson
el 7 de Feb. de 2017
I = imread('images/fig1.tif');
[r c p]= 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
el 6 de Mzo. de 2021
i need to divide the image into 8 sections
Image Analyst
el 6 de Mzo. de 2021
Just make the obvious modifications. It's not that hard is it, even for a beginner?
Mustafa Ahmed
el 7 de Mzo. de 2021
tried , only shows the four parts
Zaidi Shoaib
el 17 de Dic. de 2022
Movida: Image Analyst
el 17 de Dic. de 2022
clc;
clearvars;
close all;
a=imread('peppers.png');
[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);
title('Cropped Img of 1st quad')
B=a(1:x1,1:y1,:);
subplot(323)
imshow(B);
title('Cropped Img of 2nd quad')
C=a(x1:end,1:y1,:);
subplot(324)
imshow(C);
title('Cropped Img of 3rd quad')
D=a(x1:end,y1:end,:);
subplot(325)
imshow(D);
title('Cropped Img of 4th quad')
L=([B A;C D]);
subplot(326)
imshow(L)
title('Re-Construct Orig Image')
DGM
el 17 de Dic. de 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.
% read an image
inpict = imread('peppers.png'); % 384x512x3
% split the image into 30 tiles in a 5x6 pattern
tiling = [5 6]; % [y x]
subimages = imdetile(inpict,tiling,'direction','row'); % 77x85x3x30
% show the result, adding padding for display clarity
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
Categorías
Más información sobre Convert Image Type en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
