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

43 visualizaciones (últimos 30 días)
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.

Respuestas (1)

Image Analyst
Image Analyst el 2 de Ag. de 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);
  8 comentarios
DGM
DGM el 21 de Nov. de 2022
Editada: DGM el 21 de Nov. de 2022
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:
DGM
DGM el 21 de Nov. de 2022
Editada: DGM el 21 de Nov. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Red en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by