필터 지우기
필터 지우기

cutting a smaller potion of an image

조회 수: 3 (최근 30일)
SHUBHAM AGARWAL
SHUBHAM AGARWAL 2022년 3월 7일
답변: DGM 2022년 3월 7일
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.

답변 (3개)

Matt J
Matt J 2022년 3월 7일
편집: Matt J 2022년 3월 7일

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

DGM
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]
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.

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by