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

1 回表示 (過去 30 日間)
vetri veeran
vetri veeran 2014 年 10 月 20 日
コメント済み: Jan 2014 年 10 月 20 日
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 件のコメント
Jan
Jan 2014 年 10 月 20 日
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.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2014 年 10 月 20 日
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".

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by