How do I obtain a cell array containing the pixel coordinates of the area inside a circle?
1 view (last 30 days)
Show older comments
Hi, I know that a similar question has been asked here https://www.mathworks.com/matlabcentral/answers/167735-how-to-determine-the-position-of-points-which-belong-to-a-circle. I have implemented the code shown in the above link inside the program attached. I want to obtain a cell array that contains all the pixel coordinates that belong to a circle (where radius and centroid is known) but it returns an entirely empty cell array (logical error). I have attached the code below with hope that someone will tell me what I did wrong. I'm not very experienced with Matlab. I also have a feeling that there is a more elegant way of doing this other than what you see in my code. Any help would be appreciated.
1 Comment
KSSV
on 31 May 2018
Don't attach your code as an image snippet. YOu have to copy paste the code, so that users can read and help you.
Accepted Answer
KSSV
on 31 May 2018
You can use inbuilt function inpolygon to get the points lying inside the circle. Check the below example code:
I = imread('peppers.png') ;
[m,n,p] = size(I) ;
% circle
C = [100 200] ;
R = 50 ;
% plot circle
th = linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(2)+R*sin(th) ;
imshow(I)
hold on
plot(xc,yc)
% get points inside the circle
[X,Y] = meshgrid(1:n,1:m) ;
idx = inpolygon(X(:),Y(:),xc,yc) ;
plot(X(idx),Y(idx),'.k')
% get the pixels
iwant = zeros(nnz(idx),1,p) ;
for i = 1:p
T = I(:,:,i) ;
iwant(:,:,i) = T(idx) ;
end
0 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!