Hough Circle detection - error ↑ Error: Function definition not supported in this context. Create functions in code file. Not sure how to fix this in the code

2 Ansichten (letzte 30 Tage)
I created a code to detect circles with radius range 30 - 40 and it is not working, error message: ↑
Error: Function definition not supported in this context. Create functions in code file.
I am new at MATLAB and not sure how to resolve this.
function [C]=circle_detection(I,R)
% convert to grayscale:
I = rgb2gray(imread(I));
% generate the edge image
BW= edge (I, 'canny');% binary output
% Parameterization
theta_sample_frequency= 0.01; % Quantization step size
theta=(0:theta_sample_frequency:2*pi);% gradient angle in radians
num_thetas=numel (theta);
[x, y] = size (BW);% input image dimension
x0 = (1:x);
y0 = (1:y);
acc = zeros(x,y);% accumulator 1.e. hough space
r=R;
% Hough transform
for xi=1:x
for yj=1:y
if BW(xi,yj)==1 % this is edge point
for theta_id= 1:num_thetas
th=theta(theta_id);
a=round(xi-r*cos(th));
b=round(yj-r*sin(th));
if(a>0&&a<=x&&b>0&&b<=y)
acc(a,b) = acc(a,b)+1;
end
end
end
end
end
% Extract the parameters
[M, I]= max(acc (:)); %#ok<ASGLU> % detecting the maxima
[a, b] = ind2sub (size (acc), I); % Its courdinate in HT space
C=[y0(b), x0(a)]; % Centre of the circle
end
I am getting following error:
function [C]=circle_detection(I,R)
Error: Function definition not supported in this context. Create functions in code file.
  1 Kommentar
DGM
DGM am 23 Jun. 2021
Two things:
1: Where are you writing this? Is this in a standalone file by itself? in another function file? In a script file?
2: What version are you using? IIRC, the ability to add local functions inside script files wasn't introduced until R2016b or so.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by