Converting image to subimages

15 views (last 30 days)
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
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
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

Matt J
Matt J on 7 Aug 2021
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
Walter Roberson
Walter Roberson on 9 Aug 2021
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.
Aravind Prabhu Gopala Krishnan
Thank you very much bro understood.

Sign in to comment.

More Answers (2)

Walter Roberson
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.
  4 Comments
Walter Roberson
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
Edited: Aravind Prabhu Gopala Krishnan on 8 Aug 2021
Walter Roberson, it worked thank you

Sign in to comment.


Image Analyst
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

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!