How a binary image can be divided into four equal parts using loop ?

I have a binary image . I want to divide this into 4 equal parts using a loop and want to store each part individually. later I want to find out the no of white pixels in each parts.

3 comentarios

How is the splitting going to be done? Along and perpendicular to the major axis with the 4 corner point at the centroid of the blob? Just splitting into 4 equal quadrants regardless of where where your binary blobs lie? Can you post your image and show where you want to do the splitting?
Image Analyst : its just splitting into four equal quadrants. for each of the quadrants I have to find out f=total no. of pixels/no of object pixels. and this will be done using loop.
OK, but does this mean that the center of the quadrants will be located at the center of the blob, or at the center of the image? And I presume that edges parallel with the edges of the image are okay? And that it's okay if each quadrant does not have the same number of pixels in it?

Iniciar sesión para comentar.

 Respuesta aceptada

John BG
John BG el 24 de Feb. de 2018
Editada: John BG el 24 de Feb. de 2018
Hi Zara Khan
I use a for loop to answer your question, as requested, and I have added the variable nq to count the amount of pixels in each quadrant, please have a look, attached script and start image that I have reshaped to 16:9 format, to be able to tell what size was vertical and what horizontal:
1.
start image
clear all;clc;close all
A=imread('im02.png');imshow(A)
.
2.
the for loop with the counting of pixels for each quadrant:
d2=size(A,2);
d1=size(A,1);
vert_bord=floor(d1/2)
horz_bord=floor(d2/2)
nq=[0 0 0 0]; % 1st: top left quadrant red
% 2nd: top right quadrant green
% 3rd: bottom left quadrant blue
% 4th: bottom right quadrant magenta
hold all
A1=A(:,:,1);
for k=1:1:d1*d2
[nk1 nk2]=ind2sub([d1 d2],k);
if nk1<vert_bord && nk2<horz_bord
if A1(nk1,nk2)==255
nq(1)=nq(1)+1;
plot(nk2,nk1,'r.');
end
end;
if nk1<vert_bord && nk2>horz_bord
if A1(nk1,nk2)==255
nq(2)=nq(2)+1;
plot(nk2,nk1,'g.');
end
end;
if nk1>vert_bord && nk2<horz_bord
if A1(nk1,nk2)==255
nq(3)=nq(3)+1;
plot(nk2,nk1,'b.');
end
end;
if nk1>vert_bord && nk2>horz_bord
if A1(nk1,nk2)==255
nq(4)=nq(4)+1;
plot(nk2,nk1,'m.');
end
end;
end
.
3.
resulting quadrants, coloring just to check the counting is correct
.
4.
the amount of white pixels in each quadrant is
nq
=
1007 1084 1235 1666
.
nq(1): top left quadrant, red.
nq(2): top right quadrant, green.
nq(3): bottom left quadrant, blue.
nq(4): bottom right quadrant, magenta.
.
Zara
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG

15 comentarios

John BG: why this image need to be re scaled? If I take one quadrant and want to repeat the same procedure then will it be effective? how to calculate f=area of each quadrants/no. of white pixels for each quadrants and using loop of course ?
John BG
John BG el 24 de Feb. de 2018
Editada: John BG el 25 de Feb. de 2018
Zara
Ok, let's calculate the areas.
1.
Replacing the image I used with the image you attached to the question the results are as follows:
.
Now
2.
Area of 1 quadrant, any quadrant, in pixels, all pixels:
area_quadrant=vert_bord*horz_bord
=
27224
2. Percentage of quadrant area against complete image area
pc_area_quadrant=100*round(area_quadrant/(d1*d2),2)
=
25.0000000000000
as expected.
3.
Areas of white pixels in each quadrant? it's nq, directly
nq =
468 421 2019 2403
4.
Percentage of quadrant area white pixels against quadrant area
pc_wp_per_quadrant=round(100*nq/area_quadrant,2)
=
1.720000000000000 1.550000000000000
7.420000000000000 8.830000000000000
.
  • Item oneThe 1st quadrant TOP LEFT, in RED, has 468 white pixels.
1.72% of this quadrant has white pixels.
  • Item twoThe 2nd quadrant TOP RIGHT, in GREEN, has 421 white pixels.
1.55% of this quadrant has white pixels.
  • Item threeThe 3rd quadrant BOTTOM LEFT, in BLUE, has 2019 white pixels.
7.42% of this quadrant has white pixels.
  • Item fourThe 4th quadrant BOTTOM RIGHT, in MAG, has 2403 white pixels.
8.83% of this quadrant has white pixels.
.
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
Hello John BG , this code is going good when i am considering the whole object thing but if am considering a part of the image object then this code is not working .its showing nothing. Attaching pictures to give you some idea about what I want actually. The image img1 is the actual image now am considering the img2 then your code is working. please help me in doing this.
Probably because you have a big white frame around these images like you didn't have before. Get rid of that.
Also, did you see my followup comment way up top near your initial question?
Image Analyst: I have already replied to your question. img2 is a square sized image that has taken from img1. Now I want to divide this img2 into four equal parts. for each of the part i want to calculate value f=area of that part/no. of white pixels and that will be using a loop. So basically we will get four values i.e f1,f2,f3 and f4
Image Analyst
Image Analyst el 25 de Feb. de 2018
Editada: Image Analyst el 25 de Feb. de 2018
No you didn't - there is nothing from you under this comment.
Basically I asked where do you want the center of the quadrants to be. At the center of the image:
OR at the centroid of the entire blob:
OR, at a place where the count of the pixels in the 4 quadrants will all be as close as possible to each other:
The drawings may not be accurate, but I hope you can understand that these 3 locations may not all point to the same crossing location. Do you understand that?
Hello again Zara
Ok you need
1.- to check whether input images need cropping
2.- image splitting into quadrants
1.- CROPPING
While the supplied image in the initial question has black pixels only wrapping the white pixels of interest, the 2 new images you have supplied have white pixels framing the black pixels that in turn surround the white pixels of interest.
Before applying the code supplied above, the input image has to be cropped, that can be easily done the following way:
clear all;clc;close all;
A=imread('img1.png');
imshow(A);
d1=size(A,1);
d2=size(A,2);
hold all;
A1=A(:,:,1);
p1=1; % start 1st sweep top left corner
while A1(p1)>250
p1=p1+1;
end
[n1p1,n2p1]=ind2sub(size(A1),p1);
plot(n2p1,n1p1,'r*');
p2=d1*d2; % start 2nd sweep bottom right corner
while A1(p2)>250
p2=p2-1;
end
[n1p2,n2p2]=ind2sub(size(A1),p2);
plot(n2p2,n1p2,'g*');
% cropping
B=A([n1p1:n1p2],[n2p1:n2p2],:);
figure;imshow(B)
.
.
on the right hand side BEFORE cropping, on the left hand side AFTER cropping.
.
.
2.- QUADRANTS SPLITTING
As requested
d01=floor(size(B,1)/2);
d02=floor(size(B,2)/2);
B1=B([1:d01],[1:d02],:); % 1st quadrant: top left
B2=B([1:d01],[d02+1:end],:); % 2nd quadrant: top right
B3=B([d01+1:end],[1:d02],:); % 1st quadrant:
B4=B([d01+1:end],[d02+1:end],:); % 1st quadrant:
% check
figure;imshow(B1);
figure;imshow(B2);
figure;imshow(B3);
figure;imshow(B4);
.
.
Now run the code answering the initial question on each B2 B3 B3 B4 and you will obtain how many white pixels in each sub-quadrant, and so on so forth, you can carry on splitting while and image is 2x2 or larger, but there may not may many white pixels of interest in too small split images, may it not?
Zara
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link.
thanks in advance for time and attention
To Image Analyst: would you please be so kind to post your prospective answer in a different answer thread?
Image Analyst : yaa it's clear now. Actually I need the second case after that I have to consider only the upper two quadrants. Then have to scan the image left,rifht and top to find out the first background pixel position.the max length among centroid to left ,centroid to right and centroid to top will be considered. I mean among these three length I have to consider the max one . Have to consider two square sized image of these length from both the quadrants. Again I have to part these two quadrants into four equal parts then your first case center of the image will be considered. I hope I have made this clear to you .
Very clear,
John BG
John BG el 25 de Feb. de 2018
Editada: John BG el 25 de Feb. de 2018
do you think that now it would be possible for you to click on the Accept Answer button on your side?
Zara, that is yet a fourth case. I assume you mean foreground rather than background because the upper left background pixel is at the upper left (1,1) pixel. But if you meant foreground, then what you're describing is the center of the bounding box. That is yet a fourth case that I forgot about. The center of the bounding box may not be at the centroid of the hand, nor at the center of the image, nor at the place where all four quadrants have roughly equal numbers of foreground pixels.
So we've narrowed it down to 2 cases: do you want the center of the quadrants crossing to be either at the centroid of the blob, or at the center of the bounding box. Now, which is it?
Image Analyst: It does not mean that all the quadrants need to have only foreground pixels only. The thing is that I have to scan from centroid column to right upto width to find out the first column which will consists off all black pixels,then from centroid column to left and finally from centroid row to top for the similar thing. now the distance from centroid to the right,left and top will be calculated. the max distance or length will be considered and have to take two square of sides equal to the length have to take from both sides means left side of centroid column and right side of centroid column. Again I need to divide these square to four equal quadrants. But these quadrants can be rectangular and you can divide by following any image spitting rules. After diving these two square will get 8 parts in total. now for these 8 parts I have to find out f=no. of white pixels/area of the part. These thing I have to repeat for a dataset of 1000.
I hope, I hope, I made it clear to you now.
Zara Khan
Zara Khan el 26 de Feb. de 2018
Editada: Zara Khan el 26 de Feb. de 2018
John BG: Thanks for your time and attention. You have tried to solve my problem. Please look above what I want actually is described in briefly. I have tried to make you all understand about my problem. In previous I already have posted this problem but have not got any answer.
https://in.mathworks.com/matlabcentral/answers/375332-how-to-divide-a-binary-image-into-equal-sized-squares
Here is the link. You can answer there too.
Zara,
With your new question, that's now 4 on the same subject. What your questions seem to show is that you don't appear to know much about image processing, nor how image pixels are stored and accessed. I would suggest you spend more time learning about that from a book.
Zara Khan
Zara Khan el 26 de Feb. de 2018
Editada: Zara Khan el 26 de Feb. de 2018
Yes am new to this. The fact is the solution you provided me for my previous problem was ok. But am unable to find out the upper right image. Moreover your code is good with some of my dataset but not for all.as I have 1000 of data so most of the cases am not getting top like left ,right and bottom. So am facing problem. So I asked again if anyone can provide me any other solution . I hope you can link to my previous problem that was the one I accepted your answer.

Iniciar sesión para comentar.

Más respuestas (2)

Guillaume
Guillaume el 23 de Feb. de 2018
Editada: Guillaume el 23 de Feb. de 2018
I'm not sure why or even how you'd use a loop.
[height, width, ncols] = size(yourimage);
splitimages = mat2cell(yourimage, [height height]/2, [width width]/2, ncols)
Your four images are splitimages{1}, splitimages{2}, splitimages{3}, and splitimages{4}.
To find the number of white pixels in each subimage:
numwhitepixels = cellfun(@(subimg) sum(sum(all(subimg == 1, 3))), splitimages); %assuming images of type double, where white == 1
edit: stupidly forgot the image argument to mat2cell!

4 comentarios

mat2cell is not working. its showing some errors.
Error using mat2cell (line 97) Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [1 2].'
Error in img_split(line 26) splitimages = mat2cell([height height]/2, [width width]/2, ncols)
Yep, sorry about that. I stupidly forgot to pass the image as the first argument to mat2cell. Fixed now.
Thanks for the help. Just have done it using a for loop. Then I have used your numwhitepixels. What will be the syntax to find the total no pixels in each part ?
Like no. of white pixels calculation , will I be able to find out area of each cell using a single syntax only ??

Iniciar sesión para comentar.

sumaiya khan
sumaiya khan el 7 de Dic. de 2018
How can I diagnolly divide the image into 4 quadrants using the centroid of the blob ?

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Feb. de 2018

Respondida:

el 7 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by