I got an error as Function definitions are not permitted in this context.

2 views (last 30 days)
This is my code, I want to compute threshold of an image.I copied this code from internet.I am getting some problem in function definition. How to overcome this. Thanks in advance.
function level=isodata(I)
I = im2uint8(I(:));
% STEP 1: Compute mean intensity of image from histogram, set T=mean(I) [counts,N]=imhist(I); i=1; mu=cumsum(counts); T(i)=(sum(N.*counts))/mu(end); T(i)=round(T(i));
% STEP 2: compute Mean above T (MAT) and Mean below T (MBT) using T from % step 1 mu2=cumsum(counts(1:T(i))); MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end);
mu3=cumsum(counts(T(i):end)); MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end); i=i+1; % new T = (MAT+MBT)/2 T(i)=round((MAT+MBT)/2);
% STEP 3 to n: repeat step 2 if T(i)~=T(i-1) while abs(T(i)-T(i-1))>=1 mu2=cumsum(counts(1:T(i))); MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end);
mu3=cumsum(counts(T(i):end));
MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end);
i=i+1;
T(i)=round((MAT+MBT)/2);
Threshold=T(i);
end
% Normalize the threshold to the range [i, 1].
level = (Threshold - 1) / (N(end) - 1);
  1 Comment
Jan
Jan on 20 Oct 2014
The most important details are not explained directly:
  1. Where did you insert this code.
  2. A copy of the complete error message is important usually.

Sign in to comment.

Answers (1)

Jan
Jan on 20 Oct 2014
Function definitions are not allowed in the command line and not inside scripts. This topic has been discussed frequently in this forum and searching for the error message will reveal, how function files are created: They start with the keyword "function".

Categories

Find more on Scope Variables and Generate Names 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!

Translated by