How do I split an Image into 4 equal sub-images without using inbuilt functions?

I = imread('C:\Users\TOSHIBA\Desktop\samplepic.jpg');
[X Y]=size(I);
for i=1:X/2
for j=1:Y/2
A(i,j)=I(i,j);
end
end
for i=((X/2)+1):X
for j=1:Y/2
B(i,j)=I(i,j);
end
end
for i=1:X/2
for j=((Y/2)+1):Y
C(i,j)=I(i,j);
end
end
for i=((X/2)+1):X
for j=((Y/2)+1):Y
D(i,j)=I(i,j);
end
end
subplot(2,2,1),imshow(A);
subplot(2,2,2),imshow(B);
subplot(2,2,3),imshow(C);
subplot(2,2,4),imshow(D);
I tried out this code, but the output I got was not the 4 equal halves of the image. Please let me know how should I change this code to get 4 equal sub-images.

Réponses (1)

Try this:
rgbImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage);
middleRow = round(rows/2)
middleColumn = round(columns/2)
upperLeft = rgbImage(1:middleRow, 1:middleColumn, :);
upperRight = rgbImage(1:middleRow, middleColumn + 1 : end, :);
lowerLeft = rgbImage(middleRow + 1 : end, 1:middleColumn, :);
lowerRight = rgbImage(middleRow + 1 : end, middleColumn + 1 : end, :);
subplot(2,2,1)
imshow(upperLeft);
subplot(2,2,2)
imshow(upperRight);
subplot(2,2,3)
imshow(lowerLeft);
subplot(2,2,4)
imshow(lowerRight);

8 commentaires

and how to split an image like this?
I don't know. Do you know what they did to each quadrant?
Theodoros
Theodoros le 21 Nov 2022
Déplacé(e) : Rena Berman le 21 Nov 2022
They split the image in 4 equal pieces and used the rgb2lab command for every one of the 3 pieces. Only the upper left remains original.
Image Analyst
Image Analyst le 21 Nov 2022
Déplacé(e) : Rena Berman le 21 Nov 2022
That could be part of it. However the L, A, and B images are gray scale images so they must have also applied a colormap to the quadrant images before combining them into a gray scale image.But it sounds like you already know how they did it, so why are you asking?
Theodoros
Theodoros le 21 Nov 2022
Déplacé(e) : DGM le 21 Nov 2022
no i dont..this is an example. i have to do similar edit on another picture and the problem for me is to split the imgae in 4 pieces like the one above.
Image Analyst
Image Analyst le 21 Nov 2022
Déplacé(e) : Rena Berman le 21 Nov 2022
Why? Is it your homework for you do to? If so, see this:
I don't want to prevent you from doing your homework by giving you a full solution which you can't ethically turn in as your own. If it's not your homework then what is the use case? Why do you need to do this? Start a new discussion thread for this -- don't answer here.
Like so.
RGB = imread('peppers.png');
% convert to LAB, create channel fills
[L A B] = imsplit(rgb2lab(RGB));
Lfill = 50*ones(size(L)); % pick some L level for representing A,B
Zfill = zeros(size(L));
% create copies that are colorized/expanded for visualization
Lvis = im2uint8(repmat(L/100,[1 1 3]));
Avis = im2uint8(lab2rgb(cat(3,Lfill,A,Zfill)));
Bvis = im2uint8(lab2rgb(cat(3,Lfill,Zfill,B)));
% split and compose
outpict = [RGB(1:192,1:256,:) Lvis(1:192,257:end,:); ...
Avis(193:end,1:256,:) Bvis(193:end,257:end,:)];
% display
imshow(outpict)
% SAVE THE IMAGE INSTEAD OF SAVING A DANG SCREENSHOT
imwrite(outpict,'omgpeppers.png')
You could also split the image first and then process each block.
See also:
and for more explanation and related links:
Tangent aside, there's something to be said about the task at hand and the original answer.
The question asks to subdivide the image into equal parts, but the original answer only does that if the image geometry is even.
rgbImage = imread('cell.tif'); % odd geometry
[rows, columns, numberOfColorChannels] = size(rgbImage);
middleRow = round(rows/2);
middleColumn = round(columns/2);
upperLeft = rgbImage(1:middleRow, 1:middleColumn, :);
upperRight = rgbImage(1:middleRow, middleColumn + 1 : end, :);
lowerLeft = rgbImage(middleRow + 1 : end, 1:middleColumn, :);
lowerRight = rgbImage(middleRow + 1 : end, middleColumn + 1 : end, :);
% subimages do not have equal geometry
[size(upperLeft); size(upperRight); size(lowerLeft); size(lowerRight)]
ans = 4×2
80 96 80 95 79 96 79 95
The simple way to fix this is by discarding trailing rows/columns.
rgbImage = imread('cell.tif'); % odd geometry
[rows, columns, ~] = size(rgbImage);
% use floor() instead of round()
middleRow = floor(rows/2);
middleColumn = floor(columns/2);
upperLeft = rgbImage(1:middleRow, 1:middleColumn, :);
upperRight = rgbImage(1:middleRow, middleColumn + 1 : 2*middleColumn, :);
lowerLeft = rgbImage(middleRow + 1 : 2*middleRow, 1:middleColumn, :);
lowerRight = rgbImage(middleRow + 1 : 2*middleRow, middleColumn + 1 : 2*middleColumn, :);
% now all the subimages have equal geometry
[size(upperLeft); size(upperRight); size(lowerLeft); size(lowerRight)]
ans = 4×2
79 95 79 95 79 95 79 95
While this meets the "equal parts" requirement, there are often times where the original answer is the better way to do it. The case (above) of splitting a LAB image is a case where splitting into equal parts is not preferable. Instead, the goal is to divide the image into approximately equal parts which can be assembled back into an image of the original size. It's unimportant that the block geometries differ by one pixel.
There are other ways of dealing with the issue of geometry divisibility, but this is a simple example for a simple (probably homework) question.

Connectez-vous pour commenter.

Question posée :

le 5 Jan 2014

Modifié(e) :

DGM
le 21 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by