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

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.
Thanks for the reply. 100 is just an approximate value not an exact value. My question is i need to break the original image from right to left and top to bottom for every 25 pixels and to save them together.

Sign in to comment.

 Accepted Answer

Using mat2tiles (Download)
mat2tiles(rand(256),[25,25])
ans = 11×11 cell array
{25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×25 double} {25×6 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×25 double} { 6×6 double}

9 Comments

what is the matlab version for this?, mat2tiles is not supporting for my version r2020b
R2011b onwards.
Notice that Matt provided a download link. It is not part of MATLAB itself. However you can install it using the Add-On Explorer in any reasonably recent MATLAB version.
I gave you a link from which to download it.
Hello, Your code is worked fine, but thats not an expected result, its generating 11 image as you said, but i need to create 100 image like moving this 25x25 kenrel in the stride value of 1. I attached the reference img
Here you see red box which is 25x25 window which moves 1 stride from left to right and 1 stride from top to down. If am doing this it will create more images. Consider stride as 1 so it will move only 1 pixel left and 1 pixel down and save that image array, and again it move 1 pixel and save that image array. your code is working fine but i need to add strides in that.
Matt J
Matt J on 7 Aug 2021
Edited: Matt J on 7 Aug 2021
What I gave you generates 11^2=121 sub-images. Walter has already explained to you that with a stride of 1 you will get 232^2 sub-images, not 100.
Its working like you suggesting, its showing 11x11 blocks, I got it thank you very much for the help. Is there any possibility to view all the subimages in one plot? And how did you calculated that it will be 232^2 sub-images, is there any formula to caluclate that.
For example,
A=imread('coins.png');
B=mat2tiles(A,[25,25]);
montage(B.','Size',size(B),'BorderSize',[5,5])
Suppose you have a 5 x 6 array and you are extracting 3 x 3 sections. Then valid coordinates for the corner of the 3 x 3 are: (1,1), (1,2), (1,3), (1,4), (2,1), (2,2), (2,3), (2,4), (3,1), (3,2), (3,3), (3,4) .
So horizontally you proceeded from column 1 to the point where column number + patch width - 1 is the number of columns. Vertically you proceeded from row 1 to the point where row number + patch height - 1 is the number of rows.
This gives (columns - patch_size + 1) * (rows - patch_size + 1) patches when the patches are square.
For 256 and patch size 25, that would be (256 - 25 + 1) * (256 - 25 + 1) which is 232 * 232 patches.
Thank you very much bro understood.

Sign in to comment.

More Answers (2)

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.

4 Comments

Thank you bro, it worked
Hello, Your code is worked fine, but thats not an expected result, its generating 11 image as you said, but i need to create 100 image like moving this 25x25 kenrel in the stride value of 1. I attached the reference img
Here you see red box which is 25x25 window which moves 1 stride from left to right and 1 stride from top to down. If am doing this it will create more images. Consider stride as 1 so it will move only 1 pixel left and 1 pixel down. your code is working fine but i need to add strides in that.
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.
Walter Roberson, it worked thank you

Sign in to comment.

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

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!