cutting a smaller potion of an image

1 Ansicht (letzte 30 Tage)
SHUBHAM AGARWAL
SHUBHAM AGARWAL am 7 Mär. 2022
Beantwortet: DGM am 7 Mär. 2022
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.

Antworten (3)

Matt J
Matt J am 7 Mär. 2022
Bearbeitet: Matt J am 7 Mär. 2022

Image Analyst
Image Analyst am 7 Mär. 2022
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.

DGM
DGM am 7 Mär. 2022
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]
A = 9×9
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 = 3×3 cell array
{3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double}
C{5} % the middle block
ans = 3×3
5 5 5 5 5 5 5 5 5
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)
ans = 3×3
5 5 5 5 5 5 5 5 5
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.

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by