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

1 view (last 30 days)
I am following this steps to get 4 equal quadrants from a binary image:
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.
Zara Khan
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.

Sign in to comment.

Answers (2)

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 quadrant.
Zara Khan
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

Sign in to comment.

Walter Roberson
Walter Roberson on 17 Apr 2019
function splitted = rsplit4(img)
[r, c, p] = size(img);
if mod(r,2) || mod(c,2)
splitted = img;
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,:))};
Walter Roberson
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!