Split color Image into 16 sub images and combine all of these images again to get color image

17 views (last 30 days)
how i can split Image into (16 sub-images) and combine all of these images again to get color image
  1 Comment
Mostfa Abd El-Aziz
Mostfa Abd El-Aziz on 20 Mar 2021
How To extend this code to split the input image into (16 sub-image)????????/
%read input image
J = imread(uigetfile('*.jpeg')); % placing the image file in the variable
J=imresize(J,[256 256]); % Resize Image
I=double(J);%%% convert Image to double
% dividing the image in 4 segment
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),:);
% size of image
[M N]=size(I(:,:,1));

Sign in to comment.

Answers (3)

DGM
DGM on 21 Mar 2021
Edited: DGM on 21 Mar 2021
You start with what appears to be a RGB image and then you split it into 4 RGB images. If the input is color, the output should still be color. Is the question simply how to split it into 16 instead of 4? I'm going to assume that.
You can split into 16 images just the same as you've split into 4, though you're counting on the image height and width being both integer-divisible by 4; otherwise, you'll tend to wind up with errors as you'll have subscripts that are either fractional or out of bounds. You will have to take that into consideration if you want your subscript range calulations to be robust.
If you want a simpler method for splitting, the MIMT (look on the file exchange) has a function imdetile() that can split flat images into 4D arrays (which can then be separated into individual images if desired). If the image needs to be reassembled after some processing, that can be done with imtile() from the same toolbox. For example:
inpict=imread('sources/RGB16Million.png');
wstack=imdetile(inpict,[4 4]);
[imsize(inpict); imsize(wstack)] % show the array sizes
This reveals that we've divided a flat image into multiframe image.
ans =
4096 4096 3 1
1024 1024 3 16
We can work on this multiframe image however we choose -- as a single array, or by splitting it up into 16 individual flat images. When we're done, we can rearrange it back into a 4D array and run imtile() to reassemble it into a single flat image.
outpict=imtile(wstack,[4 4]);
imsize(outpict)
And our image is the same size it used to be.
ans =
4096 4096 3 1
Of course, our image was integer-divisible by 4 to begin with. What if it wasn't? Let's divide it into 25 images.
ans =
4096 4096 3 1
819 819 3 25
ans =
4095 4095 3 1
By default, imdetile() either pads or crops edges (whichever is least) to make the image integer-divisible. As a consequence of using defaults, the output image is not the same size. This can be remedied by explicitly choosing an appropriate option and then adjusting the output accordingly.
inpict=imread('sources/RGB16Million.png');
wstack=imdetile(inpict,[5 5],'fittype','scale');
outpict=imresize(imtile(wstack,[5 5]),imsize(inpict,2));
In this case, we used a scaling method to make it integer-divisible and then we rescaled the output to match the input geometry. If you don't want to risk the interpolation error, you can use the 'grow' fit type and then crop off the excess edge vectors using cropborder() instead.
Of course, that's all using MIMT tools. Is there a purely Matlab/IPT way to do it? Probably, but you'll have to work out all the tilings manually.

Walter Roberson
Walter Roberson on 21 Mar 2021
J = imread(uigetfile('*.jpeg')); % placing the image file in the variable
J = imresize(J,[256 256]); % Resize Image
I = im2double(J);%%% convert Image to double
part_sizes = 256/4*ones(1,4);
parts = mat2cell(partsizes, partsizes, size(I,3));
Now you have parts is a 4 x 4 cell array with parts{3,4} would be
* * * *
* * * *
* * * H
* * * *
the tile marked.
  2 Comments
Mostfa Abd El-Aziz
Mostfa Abd El-Aziz on 21 Mar 2021
there is an error when try this code:
Error using mat2cell (line 89)
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [1 4].
How to solve ??
thanx for response

Sign in to comment.


Image Analyst
Image Analyst on 21 Mar 2021

Community Treasure Hunt

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

Start Hunting!