Converting image to subimages
Show older comments
Hi everyone
I have an image of size (256,256) and i need to create approximately 100 subimages of size(25x25) i.e I have to move sliding window of step size 1 in x and 1 in y and at the end i need to create approximately 100 subimages by moving this kernel(25x25) all over my origninal image. Thus final results should be like my subimage array will contain 100 subimages of original image i.e including all parts of the image.
2 Comments
Walter Roberson
on 7 Aug 2021
If you are looking for 100 outputs, then you want 10 steps per dimension, and that would give you a step of floor(256/10) = 25. Effectively non-overlapping windows.
If you do use non-overlapping windows, then 25*10 = 250 in each direction would be covered... not the original image size of 256.
You cannot cover "all parts" of 256 x 256 using only one hundred 25 x 25 windows.
Aravind Prabhu Gopala Krishnan
on 7 Aug 2021
Accepted Answer
More Answers (2)
Walter Roberson
on 7 Aug 2021
SZ = 25;
R = size(YourImage,1);
C = size(YourImage,2);
Rc = floor(R/SZ);
Cc = floor(C/SZ);
Rextra = R - Rc*SZ;
Cextra = C - Cc*SZ;
blocks = mat2cell(YourImage, [SZ * ones(1,Rc), Rextra], [SZ * ones(1,Cc), Cextra], size(YourImage,3));
This will give a cell array. If needed, the edge blocks will be smaller -- for example for a 256 x 256 image, you would get edges that were 6 x 25 and edges that were 25 x 6 and a single 6 x 6 block.
However... you should consider using blockproc() https://www.mathworks.com/help/images/ref/blockproc.html or nlfilter() https://www.mathworks.com/help/images/ref/nlfilter.html
4 Comments
Aravind Prabhu Gopala Krishnan
on 7 Aug 2021
Aravind Prabhu Gopala Krishnan
on 7 Aug 2021
Walter Roberson
on 7 Aug 2021
Edited: Walter Roberson
on 7 Aug 2021
Use for loops.
SZ = 25;
N = 100;
blocks = cell(N,1);
for idx = 1 : N
blocks{idx} = YourImage(idx:idx+SZ-1, idx:idx+SZ-1, :);
end
In one place in your description you say to move from left to right, but in another place in your description, you say it should move one pixel left, which would result in it moving from right to left instead of left to right. In the code, I implemented left to right.
Aravind Prabhu Gopala Krishnan
on 8 Aug 2021
Edited: Aravind Prabhu Gopala Krishnan
on 8 Aug 2021
Image Analyst
on 8 Aug 2021
Try a simple double for loop:
grayImage = imread('cameraman.tif');
windowSize = 25;
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
for col1 = 1 : columns-1
col2 = col1 + windowSize - 1;
if col2 > columns
col2 = columns;
end
for row1 = 1 : rows-1
row2 = row1 + windowSize - 1;
if row2 > rows
row2 = rows;
end
% Get the subimage.
subImage = grayImage(row1 : row2, col1 : col2);
% Now call imwrite(), or process it however you want.
end
end
1 Comment
Aravind Prabhu Gopala Krishnan
on 9 Aug 2021
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

