MATLAB: How can I calculate number of pixel in each angle of a circle?

circlepolar histogram

hello, Can someone help me please I need to calculate number of pixel in each angle of a circle arround image like this

Best Answer

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