Converting image to subimages
조회 수: 17 (최근 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!