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 CenterFile Exchange에서 Low-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