Can anyone say how to divide an image in 6 parts?

2 views (last 30 days)
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),:);
This is for dividing a image into four equal parts. Can anyone help me in writing the image into 6 parts (2 row-wise and 3 column- wise) and another for 2 row- wise and 2 column wise. Thank you in advance

Accepted Answer

Guillaume
Guillaume on 22 Jan 2018
This is for dividing a image into four equal parts
Yes, and it is horrible! If you start numbering variables you are doing it wrong. A loop or a vectorised method is always better. In your case:
%img: input image
nrows = 2; %can be whatever you want
ncols = 3; %can be whatever you want
%some checks first
assert(mod(size(img, 1), nrows) == 0, 'number of rows of image not divisibly by nrows');
assert(mod(size(img, 2), ncols) == 0, 'number of columns of image not divisibly by ncols');
%now divide the image
subimages = mat2cell(img, ones(1, nrows) * size(img, 1)/nrows, ones(1, ncols) * size(img, 2)/ncols, 1)
%all done!
subimages{n} is the same as your original In with the advantage that you can loop over it if needed.
  5 Comments
Zara Khan
Zara Khan on 16 Apr 2019
Guillaume:
After getting four different and equal parts how can again divide each of these parts to 4 equal parts and so on. This process will continue as long as we can get 4 equal size parts?
Guillaume
Guillaume on 18 Apr 2019
Instead of an iterative process, you can figure out straight away how many times you can divide the image into equal parts. It's how many times you can divide the width and height by 2. So:
%img: input image. To be divided in blocks of equal size
nrows = nnz(factor(size(img, 1)) == 2);
ncols = nnz(factor(size(img, 2)) == 2);
subimages = mat2cell(img, ones(1, nrows) * size(img, 1)/nrows, ones(1, ncols) * size(img, 2)/ncols, 1);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 22 Jan 2018

Community Treasure Hunt

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

Start Hunting!