Segment the image into blocks

断了今生、忘了曾经 提交于 2019-11-26 23:42:06

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/20109360/segment-the-image-into-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!