Question

I'd like to label and call parts of the image in the following format as I've been using so far for 4 blocks on a 1024 square image. Although there are some paths that can be used, which involves lots of loops, I'm looking for an efficient way to generalize it for different sizes of images with different lengths of blocks without losing labels.

I also would like to merge them after doing some operations.

Including to that, the requested blocks will always be square, length will always be completely Proportional to the original image. In other words I won't try to divide a 1024x1024 image to 612x612 blocks. But I may want to divide it upto 512x512 blocks as with the following script, or 256,128,64,..

The ranges of the cuts are static and only serves for 4 blocks having 512 square sizes. I want to avoid loops and incremental searches.

Here is the script I'm using so far:

function y=img_divider(z,img_block)

%divides the input image into 4 blocks of the following composition for 1024x1024 inputs. Returns the block called with img_block. 
% | 1 | 2 |
% |---|---|
% | 3 | 4 |

if img_block==1
y=z(1:512,1:512,:);
end
if img_block==2
y=z(1:512,513:1024,:);
end
if img_block==3
y=z(513:1024,1:512,:);
end
if img_block==4
y=z(513:1024,513:1024,:);
end

return;

What I want to develop with the help of this question would be similar to:

function y=img_divider(z,img_block,n)

% | 1 | ... | sqrt(n) |
% |---|---|...|
% |...|...|...|
% |...| ... |n |

bs=size(z,1)/sqrt(n)
if img_block==1
y=z(1:bs,1:bs,:);
end
...
...
if img_block==n
y=z(size(z,1)-bs+1:size(z,1),:),size(z,1)-bs+1:size(z,1),:);
end
Was it helpful?

Solution

Try this -

function y=img_divider(z,img_block,n)

n2 = sqrt(numel(z)/n);
block_lens = n2*ones(1,size(z,1)/n2);
t1 = mat2cell(z,block_lens,block_lens)'; %%//'
y = cell2mat(t1(img_block));

return;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top