Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

matlab - Segment the image into blocks

Let us consider an image Y of size 512x512.

The code below serves to segment the image Y into blocks where each block take the size 8x8.

Matlab Code:

for m = 1:64
    for n = 1:64
        subX = Y(8*(m-1)+1:8*m,8*(n-1)+1:8*n);
    end
end

What i need in this question is to resolve my two problems below:

1) to segment the image X into 8 x 8 number of blocks (not the size is 8x8 but the number of blocks must be 8x8). In this case the image will become segmented into 64 blocks where each block being contain 512/64 pixels =8 pixels.

2) it is the same concept of 1), but in this case, i want to segment the image into 10x10 number of blocks. therefore the image will become segmented into 100 blocks. But we can now notice that each block being containing 512/100 = 5.12 pixels!! so it's float!

PLEASE help me to write a unique code which can be resolve my two problems at the same time.

Best Regards,

Christina.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using mat2cell to break the image up into blocks:

bsX = 10; bsY = 10;
[m,n] = size(Y);
numFullBlocksX = floor(n/bsX); numFullBlocksY = floor(m/bsY);
xBlocks = [repmat(bsX,numFullBlocksX,1); mod(n,bsX)*ones(mod(n,bsX)>0)];
yBlocks = [repmat(bsY,numFullBlocksY,1); mod(m,bsY)*ones(mod(m,bsY)>0)];
blockCell = mat2cell(Y,yBlocks,xBlocks)

To instead go from number of blocks to block size, lead with these two lines instead of bsX = 10; bsY = 10;:

numBlocksX = 10; numBlocksY = 10;
bsX = ceil(n/numBlocksX); bsY = ceil(m/numBlocksY);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...