how to segment (divide) an image into 4 equal halves?
조회 수: 90 (최근 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!