MATLAB Answers

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

11 views (last 30 days)
Arch25
Arch25 on 5 Jan 2014
Answered: Image Analyst on 2 Aug 2021
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.

Answers (1)

Image Analyst
Image Analyst on 2 Aug 2021
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);

Community Treasure Hunt

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

Start Hunting!

Translated by