how can I calculate number of pixel in each angle of a circle??

15 views (last 30 days)
hello, Can someone help me please I need to calculate number of pixel in each angle of a circle arround image like this

Accepted Answer

John BG
John BG on 19 Jul 2016
Dear Zaafouri
the previous script works, the result when applied to the second image you have supplied does not have identical bars, have a look:
Explanation:
the centring and framing of the image are both of capital importance.
These 2 parameters are as important as the image itself.
When I commented to work without the polar grid, I left the outer circle, precisely because if you do not define one CENTRE and one outer circle (let me call it the SCOPE WINDOW) then one image has many different counts as combinations of centres and scope windows are guessed.
So, depending upon where you place the centre and how big you define the scope window you most certainly will have different counts, so the measurement is only valid of you define 3 things:
1.- IMAGE
2.- CENTRE
3.- SCOPE WINDOW
Now let's have a look to the second image you have supplied in your comment:
1.- the 1st cell of the previous script inverts the black background 2nd image you have supplied.
C=imread('round2_start_image3.jpg');imshow(C);
C(find(C>125))=255; C(find(C<=125))=0; % simplify pixels range to binary
C(:,:,2)=and(C(:,:,2),C(:,:,3));C(:,:,3)=[];
C=and(C(:,:,1),C(:,:,2));
C=logical(~C);
f1=figure(1);imshow(C)
f1.Position=[100 100 1300 1300];
Let's start with same type of image as in the question, with white background.
C=imread('12.jpg');imshow(C);
C(find(C>125))=255; C(find(C<=125))=0; % simplify pixels range to binary
C(:,:,2)=and(C(:,:,2),C(:,:,3));C(:,:,3)=[];
C=and(C(:,:,1),C(:,:,2));
C=logical(~C);
f1=figure(1);imshow(C)
f1.Position=[100 100 1300 1300];
2.- take reference points and see whether it works on 1st sector:
p_center=ginput(1); % manual input centre PPI
p_edge=ginput(1); % manual input any point of PPI edge
prompt1={'key in degrees sector: '}; % input degrees per sector
dlg_title='sector degrees ';n_lines=1;
default={'30'};sctr=inputdlg(prompt1,dlg_title,n_lines,default);
sector_angle= str2num(cell2mat(sctr));
% make sure chosen sector is multiple of 360
% if rem(360,sector)>0 break, or ask to input again
r=abs((p_center(1)-p_edge(1))^2-(p_center(2)-p_edge(2))^2)^.5; % calculate PPI radius in pixels
k=[0:sector_angle:360]; % split 360 angle into equal sector angles
rc1=r*exp(j*pi*k./180);
x_arc1=real(rc1)+p_center(1); y_arc1=imag(rc1)+p_center(2); % get points sectors around outer PPI circle
hold all;
plot(x_arc1,y_arc1,'go')
sector_scan=[p_center(1) x_arc1(1) x_arc1(2) p_center(1);p_center(2) y_arc1(1) y_arc1(2) p_center(2)];
plot(sector_scan(1,:),sector_scan(2,:),'b','LineWidth',1.5);
D=double(C);
[yq,xq]=find(D);
plot(xq,yq,'*r'); % initiial test, just to visualise inpolygon input points are correct
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
plot(xq(in),yq(in),'*g')
plot(xq(on),yq(on),'*y')
3.- It seems it works, all we have to do now it to count the green dots for each sector, repeating the for loop of the
count=zeros(1,length(k)-1);
for s=1:1:length(k)-1
sector_scan=[p_center(1) x_arc1(s) x_arc1(s+1) p_center(1);p_center(2) y_arc1(s) y_arc1(s+1) p_center(2)];
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
count(s)=numel(xq(in))
end
4.- voila,
uint64(count')
ans =
5864
3869
21177
21238
5239
5071
3723
5127
5637
5021
5573
5646
max(count)
min(count)
=
21238.00
=
3723.00
5.- and the bars graph:
figure(2);bar(count);grid on
Summary:
So, with clearly defined IMAGE, CENTER and SCOPE WINDOW, you get a measurement. Without aiming, it doesn't work.
so dear Zaafouri,
would you please be so kind to mark my answer as ACCEPTED ANSWER, thanks in advance.
Feel free to contact me by email for additional improvements.
Awaiting your answer
Regards
John Bofarull Guix
  1 Comment
zaafouri rabaa
zaafouri rabaa on 20 Jul 2016
Hi John BG, Attached is the article that I want to make the implementation of the method proposed in it. and please do not forget my question:calcul of the histogram according first to sections and secondly by sections(as presented in the article) my input picture is

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 16 Jul 2016
What is the resolution of your digital image? You have to know that first. Otherwise you could have a thousand pixels, or a million, or a billion. Who's to say until you tell us how many pixels wide and tall your image is?
Then use the FAQ http://matlab.wikia.com/wiki/FAQ#How_do_I_create_an_arc.3F to create a mask for your image, then just count the pixels:
numPixels = sum(mask(:));
  9 Comments
zaafouri rabaa
zaafouri rabaa on 17 Jul 2016
yes I know I have only white and black in my image. (binary image) I want to have how many white pixels in each sector.
Image Analyst
Image Analyst on 18 Jul 2016
Then, just add up the number of white pixels in your binary image that are in the sector-shaped mask. Change the last few lines of my code like this:
% Sum up the number of white pixels in your
% binary image that are in the sector-shaped mask
numPixelsInMask = sum(binaryImage(mask))
message = sprintf('The number of white pixels in the sector is %d', numPixelsInMask);

Sign in to comment.


John BG
John BG on 17 Jul 2016
Hi Zaafouri
You don't really need to go down to counting the pixels of the arcs closing each sector.
1.- acquire image
C=imread('original_without_grid.jpg');imshow(C);
C(find(C>125))=255; C(find(C<=125))=0; % simplify pixels range to binary
C(:,:,2)=and(C(:,:,2),C(:,:,3));C(:,:,3)=[];
C=and(C(:,:,1),C(:,:,2));
C=logical(~C);
f1=figure(1);imshow(C)
f1.Position=[100 100 1300 1300];
on the left your input image, without the polar grid, you will see in point 3 of this answer why you need to remove the grid, and on the left, the acquired image as binary map, whites=1 blacks=0.
2.- get the centre point, and just a reference point on the right hand side of the PPI outer circle
p_center=ginput(1); % manual input centre PPI
p_edge=ginput(1); % manual input any point of PPI edge
prompt1={'key in degrees sector: '}; % input degrees per sector
dlg_title='sector degrees ';n_lines=1;
default={'30'};sctr=inputdlg(prompt1,dlg_title,n_lines,default);
sector_angle= str2num(cell2mat(sctr));
% make sure chosen sector is multiple of 360
% if rem(360,sector)>0 break, or ask to input again
note that there is no check whether the sector angle is multiple of 360º, let's leave this as possible improvement.
3.- calculating sector parameters
r=abs((p_center(1)-p_edge(1))^2-(p_center(2)-p_edge(2))^2)^.5; % calculate PPI radius in pixels
k=[0:sector_angle:360]; % split 360 angle into equal sector angles
rc1=r*exp(j*pi*k./180);
x_arc1=real(rc1)+p_center(1); y_arc1=imag(rc1)+p_center(2); % get points sectors around outer PPI circle
% hold all;
% plot(x_arc1,y_arc1,'go')
sector_scan=[p_center(1) x_arc1(1) x_arc1(2) p_center(1);p_center(2) y_arc1(1) y_arc1(2) p_center(2)];
plot(sector_scan(1,:),sector_scan(2,:),'b','LineWidth',1.5);
D=double(C);
[yq,xq]=find(D);
plot(xq,yq,'*r'); % initiial test, just to visualise inpolygon input points are correct
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
plot(xq(in),yq(in),'*g')
plot(xq(on),yq(on),'*y')
on the left, the cluttered result when loading the input image WITH polar grid, on the right, the points of interest in green colour, when loading the input image WITHOUT the polar grid. Note there is still a bit of clutter caught on the outer corners of the sector. Don't worry, I repeat, there is no need for the pixels of the arc you have been asked for earlier on, I will show you how to draw arcs to close the sectors, it's fairly easy, but first let's get the result.
4.- so, the loop you may be most interested on
count=zeros(1,length(k)-1);
for s=1:1:length(k)-1
sector_scan=[p_center(1) x_arc1(s) x_arc1(s+1) p_center(1);p_center(2) y_arc1(s) y_arc1(s+1) p_center(2)];
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
count(s)=numel(xq(in))
end
and the result:
count'
=
124.00
615.00
1882.00
2112.00
829.00
114.00
111.00
110.00
260.00
1186.00
178.00
38.00
max(count)
=
2112.00
min(count)
=
38.00
a bit of graphing
figure(2);bar(count);grid on
I wonder if you would not mind, Zaafori, if you find this answer useful olving your question, please mark it as ACCEPTED ANSWER.
to any other reader, if you find if of any help, please click on the thumbs-up vote link,
thanks in advance
John BG
  10 Comments
Image Analyst
Image Analyst on 18 Jul 2016
So you're aiming a video camera at a paper printout of a polar graph? If you don't like my solution, then have you tried John or Thorsten's solution yet?
John BG
John BG on 19 Jul 2016
Dear Zaafouri
the previous script works, the result when applied to the second image you have supplied does not have identical bars, have a look:
Explanation:
the centring and framing of the image are both of capital importance.
These 2 parameters are as important as the image itself.
When I commented to work without the polar grid, I left the outer circle, precisely because if you do not define one CENTRE and one outer circle (let me call it the SCOPE WINDOW) then one image has many different counts as combinations of centres and scope windows are guessed.
So, depending upon where you place the centre and how big you define the scope window you most certainly will have different counts, so the measurement is only valid of you define 3 things:
1.- IMAGE 2.- CENTRE 3.- SCOPE WINDOW
Now let's have a look to the second image you have supplied in your comment:
1.- the 1st cell of the previous script inverts the black background 2nd image you have supplied.
C=imread('round2_start_image3.jpg');imshow(C);
C(find(C>125))=255; C(find(C<=125))=0; % simplify pixels range to binary
C(:,:,2)=and(C(:,:,2),C(:,:,3));C(:,:,3)=[];
C=and(C(:,:,1),C(:,:,2));
C=logical(~C);
f1=figure(1);imshow(C)
f1.Position=[100 100 1300 1300];
Let's start with same type of image as in the question, with white background.
C=imread('12.jpg');imshow(C);
C(find(C>125))=255; C(find(C<=125))=0; % simplify pixels range to binary
C(:,:,2)=and(C(:,:,2),C(:,:,3));C(:,:,3)=[];
C=and(C(:,:,1),C(:,:,2));
C=logical(~C);
f1=figure(1);imshow(C)
f1.Position=[100 100 1300 1300];
2.- take reference points and see whether it works on 1st sector:
p_center=ginput(1); % manual input centre PPI
p_edge=ginput(1); % manual input any point of PPI edge
prompt1={'key in degrees sector: '}; % input degrees per sector
dlg_title='sector degrees ';n_lines=1;
default={'30'};sctr=inputdlg(prompt1,dlg_title,n_lines,default);
sector_angle= str2num(cell2mat(sctr));
% make sure chosen sector is multiple of 360
% if rem(360,sector)>0 break, or ask to input again
r=abs((p_center(1)-p_edge(1))^2-(p_center(2)-p_edge(2))^2)^.5; % calculate PPI radius in pixels
k=[0:sector_angle:360]; % split 360 angle into equal sector angles
rc1=r*exp(j*pi*k./180);
x_arc1=real(rc1)+p_center(1); y_arc1=imag(rc1)+p_center(2); % get points sectors around outer PPI circle
hold all;
plot(x_arc1,y_arc1,'go')
sector_scan=[p_center(1) x_arc1(1) x_arc1(2) p_center(1);p_center(2) y_arc1(1) y_arc1(2) p_center(2)];
plot(sector_scan(1,:),sector_scan(2,:),'b','LineWidth',1.5);
D=double(C);
[yq,xq]=find(D);
plot(xq,yq,'*r'); % initiial test, just to visualise inpolygon input points are correct
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
plot(xq(in),yq(in),'*g')
plot(xq(on),yq(on),'*y')
3.- It seems it works, all we have to do now it to count the green dots for each sector, repeating the for loop of the
count=zeros(1,length(k)-1);
for s=1:1:length(k)-1
sector_scan=[p_center(1) x_arc1(s) x_arc1(s+1) p_center(1);p_center(2) y_arc1(s) y_arc1(s+1) p_center(2)];
[in,on]=inpolygon(xq,yq,sector_scan(1,:)',sector_scan(2,:)');
count(s)=numel(xq(in))
end
4.- voila,
uint64(count')
ans =
5864
3869
21177
21238
5239
5071
3723
5127
5637
5021
5573
5646
max(count)
min(count)
=
21238.00
=
3723.00
5.- and the bars graph:
figure(2);bar(count);grid on
Summary:
So, with clearly defined IMAGE, CENTER and SCOPE WINDOW, you get a coherent measurement. Without aiming, it doesn't work.
so dear Zaafouri,
would you please be so kind to mark my answer as ACCEPTED ANSWER, thanks in advance.
Feel free to contact me by email for additional improvements.
Awaiting your answer
Regards
John Bofarull Guix

Sign in to comment.


Thorsten
Thorsten on 18 Jul 2016
Edited: Thorsten on 18 Jul 2016
I = imread('../../Downloads/testimage.jpg');
% use ginput to determine the center: click where the center should be,
% then hit return
% [cx cy] = ginput
cx = 71.0823;
cy = 74.9057;
cx = round(cx);
cy = round(cy);
% idea: determine an angle for each pixel A and then select those pixels
% AI for which the image is > 0
x = (1:size(I, 2)) - cx;
y = (1:size(I,1)) - cy;
[X, Y] = meshgrid(x, y);
A = atan2(X, Y);
AI = A(I>0);
hc = deg2rad((-180:30:150)+15); % center of sectors
hist(AI(:), hc) % x runs from -pi to pi, the angles returned by atan2

Community Treasure Hunt

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

Start Hunting!