# How can I loop over a binary image to get 4 equal quadrants always?

3 views (last 30 days)
Zara Khan on 17 Apr 2019
Commented: Walter Roberson on 17 Apr 2019
I am following this steps to get 4 equal quadrants from a binary image:
r1=q1(1:size(q1,1)/2,1:size(q1,2)/2,:);
r2=q1(size(q1,1)/2+1:size(q1,1),1:size(q1,2)/2,:);
r3=q1(1:size(q1,1)/2,size(q1,2)/2+1:size(q1,2),:);
r4=q1(size(q1,1)/2+1:size(q1,1),size(q1,2)/2+1:size(q1,2),:);
After this, I want to get 4 equal quadrants from r1,r2,r3,r4 indivisually. This process will continue as long as we can get 4 equal quadrants . How can I simply do this with the help of any loop.
##### 3 CommentsShow 1 older commentHide 1 older comment
Walter Roberson on 17 Apr 2019
To confirm: if the original image happened to be a power of 2 on each side, such as 512 x 512, then you would want the division into equal quadrants to continue right down to the point where the quadrant sizes were 1 x 1 ? But for a 513 x 513 matrix, then no sub-division would be done, since you cannot make equal quardrants ?
Zara Khan on 17 Apr 2019
Edited: Zara Khan on 17 Apr 2019
Walter Roberson: I am working on set of images.images are of different sizes. Now what I am doing I am taking two square sized boxes from both side of centroid. After that I want to do this division work individually on both the square sized blocks as long as I can get 4 equal quadrants.

Jan on 17 Apr 2019
Edited: Jan on 17 Apr 2019
Using 4 distinct variables is less convenient than reshaping the array:
s = size(q1);
q2 = reshape(q1, s(1)/2, 2, s(2)/2, 2);
Now you have e.g. your r4 stored in q2(:, 2, :, 2). In general:
s = size(q1); % Assuming that q1 is a binary image ==> 2D!
% How often can the size be divided by 2:
n = min(sum(factor(s(1)) == 2), sum(factor(s(2)) == 2));
twos = repmat(2, 1, n);
div = 2^n;
qq = reshape(q1, [s(1) / div, twos, s(2) / div, twos]);
Perhaps you want to permute the array (you did not mention, what you need as output).
qq = permute(qq, [1, n+2, 2:n+1, n+3:2*n+2]);
% And maybe:
qq = reshape(qq, s(1)/div, s(2)/div, []);
Now qq(:, :, i) contains the i.th quadrant.
##### 3 CommentsShow 1 older commentHide 1 older comment
Jan on 17 Apr 2019
What is "no of white pixels count" and what is the relation to the original question?
Zara Khan on 17 Apr 2019
Edited: Zara Khan on 17 Apr 2019
Number of white pixels in each blocks. You asked me about the output

Walter Roberson on 17 Apr 2019
function splitted = rsplit4(img)
[r, c, p] = size(img);
if mod(r,2) || mod(c,2)
splitted = img;
else
splitted = {rsplit4(img(1:end/2,1:end/2, :)), rsplit4(img(1:end/2,end/2+1:end, :));
rsplit4(img(end/2+1:end,1:end/2,:)), rsplit4(img(end/2+1:end,end/2+1:end,:))};
end
end
Zara Khan on 17 Apr 2019
Sorry,I have attached now.
by changing
splitted = nnz(img);
and {} to [] not getting white pixels count of one block. but i want to get for all the blocks like i have attached the picture
Note that if your image happens to be a power of 2 in each direction, then the output will happen to exactly equal the input when the input is a binary matrix, because you keep subdividing into quadrants until you get down to 1 x 1, and the number of whitespace elements in that is the same as the question of whether the pixel is true or not.
I was doing this :
rowMid = ceil(rows / 2);
colMid = ceil(columns / 2);
Walter Roberson on 17 Apr 2019
So you have, in this example, an 80 x 160 image, and you want to sub-divide down to 10 x 10 ? It is not clear why you did not continue on to 5 x 5.
The centroid of the 80 x 160 would be at (40, 80), and the maximum horizontal or vertical distance from the centroid to the edge would be 80. Your previous requirements say that we must take an 80 x 80 square on each side of the centroid. However, with the centroid being at (40,80), we cannot take an 80x80 square to either side of it.