Converting image to subimages
15 次查看(过去 30 天)
显示 更早的评论
Aravind Prabhu Gopala Krishnan
2021-8-7
评论: Aravind Prabhu Gopala Krishnan
2021-8-9
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 个评论
Walter Roberson
2021-8-7
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.
采纳的回答
Matt J
2021-8-7
mat2tiles(rand(256),[25,25])
9 个评论
Walter Roberson
2021-8-9
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.
更多回答(2 个)
Walter Roberson
2021-8-7
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 个评论
Walter Roberson
2021-8-7
编辑:Walter Roberson
2021-8-7
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.
Image Analyst
2021-8-8
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!