how to segment (divide) an image into 4 equal halves?
71 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2017-3-18
SATISH, to reassemble, do this:
fullImage = [A, C; B, D];
mehrdad bahadori
2023-2-15
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
2012-8-25
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 个评论
Sivakumaran Chandrasekaran
2012-8-25
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
更多回答(5 个)
Image Analyst
2021-3-7
编辑:Image Analyst
2021-3-7
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 个评论
Walter Roberson
2021-5-16
This demonstration does not store the subImage(), so they cannot be recombined later.
Walter Roberson
2021-5-16
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
2012-8-25
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 个评论
Image Analyst
2019-3-29
imshow(upperLeft, 'upperLeft.png');
imshow(upperRight, 'upperRight.png');
imshow(lowerLeft, 'lowerLeft.png');
imshow(lowerRight, 'lowerRight.png');
Biza Ferreira
2013-5-1
编辑:Walter Roberson
2013-5-1
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 个评论
Image Analyst
2021-3-6
Just make the obvious modifications. It's not that hard is it, even for a beginner?
Zaidi Shoaib
2022-12-17
移动:Image Analyst
2022-12-17
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')
0 个评论
DGM
2022-12-17
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!