cutting a smaller potion of an image
3 次查看(过去 30 天)
显示 更早的评论
I want to cut two or one portions from an image of exact size either 256*256 or 512*512 square matrices of size powers of two. how to do it? My original image is 2048*2048.
0 个评论
回答(3 个)
Image Analyst
2022-3-7
If you know the starting row and starting column of the upper left of where the one or two subimages is to come from, then you can simply use indexing:
subImageSize = 256; % or 512
subImage = originalImage(row : row + subImageSize - 1, column : column + subImageSize - 1); % If Gray scale
If the image is color, you can add in the third dimension:
subImage = originalImage(row : row + subImageSize - 1, column : column + subImageSize - 1, :); % If color
Using indexing gives you just one subimage, not all possible subimages that will fit into the original image. And the image is just a normal image, not a cell array.
0 个评论
DGM
2022-3-7
Consider the following simplified matrix:
A = [1 1 1 4 4 4 7 7 7;1 1 1 4 4 4 7 7 7;1 1 1 4 4 4 7 7 7; ...
2 2 2 5 5 5 8 8 8;2 2 2 5 5 5 8 8 8;2 2 2 5 5 5 8 8 8; ...
3 3 3 6 6 6 9 9 9;3 3 3 6 6 6 9 9 9;3 3 3 6 6 6 9 9 9]
If you want to split the array into blocks, and your array is already integer-divisible by the intended blocksize (it should be in your case), then one way would be to split it into a cell array:
% split the array into blocks, all stored in a cell array
C = mat2cell(A,[3 3 3],[3 3 3]) % the cell array of blocks
C{5} % the middle block
Another way would be to simply split the array by basic indexing operations. This has an advantage in that if you only want one or two blocks, you don't need to split the enitre array:
% simply address a given block directly
bsz = [3 3]; % blocksize [y x]
bidx = [2 2]; % block index
rowsubs = bsz(1)*(bidx(1)-1)+1:bsz(1)*bidx(1);
colsubs = bsz(2)*(bidx(2)-1)+1:bsz(2)*bidx(2);
A(rowsubs,colsubs)
Otherwise, if you do want to split the entire array, you could continue the process in a loop, incrementing the block index as you go.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!