Segment the image into blocks
Asked Answered
S

1

1

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.

Shadowgraph answered 20/11, 2013 at 23:29 Comment(2)
Sorry, I can't see the actual problem, though. In both cases it looks to me that it is sufficient to substitute the hard-coded constants you have with the appropriate ones (maybe after a floor operation).Sicard
related question: https://mcmap.net/q/247396/-is-there-a-substitute-for-blockproc-in-matlab/97160Diplomat
C
4

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);
Culvert answered 20/11, 2013 at 23:38 Comment(17)
Firstly thank you very much for your answer. OK i tested your code and it works well. your code serves to segment the image into 10x10 blocks right ? So each block is like a sub-matrix right ? blockcell is the image segmented into 10x10 blocks??how can i set each sub-matrix into a cell of array ? So how can i select a specific block to apply some transformations in it ?Shadowgraph
@Shadowgraph To access say block row number 2 and column number 8, blockCell{2,8} (note the curly braces) gives you that 10x10 block. The blocks in the last row and column of blockCell will of course be less than or equal to the size of the other blocks depending on bs.Culvert
ah so blockCell is the complete result of the image segmentation. In other words, blockCell is the segmented image into 10x10 blocks and to access a specific block, we must write blockCell {...,...} right ? thank's in advance! :)Shadowgraph
@Shadowgraph That is correct. After computing the number of full size blocks in each direction with floor, a vector of block sizes (xBlocks) is created by appending an additional smaller block of size mod(n,bs) if necessary (ones(mod(n,bs)>0) avoids an extra smaller block when one is not necessary).Culvert
but i think that your code segment the image into blocks where each block take the size 10x10. But what i need is to segment the image into 10x10 blocks (the number of blocks is 10x10) not each block is 10x10 of sizeShadowgraph
I need to segment the image into 10x10 blocks. 10x10 is the number of blocks not the size of each block. can you help me please :) So i need to segment the image into 10x10 number of blocksShadowgraph
@Shadowgraph Actually, it seems straightforward (see updated answer). Is that the output you want?Culvert
when i wrote size(blockCell), i get 10 15 , why ? I think that it must be 10 10 not 10 15 right ?Shadowgraph
I rewrote some parts, try again. I get [10 10]. Be sure not to run the first line bsX = 10; bsY = 10; when doing it the second way.Culvert
Ok i got the 10 10 :)Shadowgraph
Yeah, just set the number of blocks in each dimension. The last row and column will have blocks equal or smaller to all other blocks.Culvert
and to access in a specific block, we must write blockCell{...,...} ?Shadowgraph
You got it, blockCell{row,col}.Culvert
I don't know how to thank you Dear! you are the best. I am the honor to vote and accept your answer :) thank you again for all your explanations and details and especially your genius matlab code :)Shadowgraph
I accepted your answer but désolé i can't vote up because i have only 3 reputations :(Shadowgraph
@Shadowgraph No biggie.Culvert
@chappjc, is it possible to extend your method, in such a way, that we can obtain overlapping blocks? for instance, i want to extract blocks of size 10x10, each 5 pixels.Tried

© 2022 - 2024 — McMap. All rights reserved.