Hello! I try to write an Otsu Threshold algorithm function to be used later along with the blockproc. Here is my code:
clc; clear; close all;function out = graythresh(im)if isstruct(im) im = im.data;endif ~isempty(im) I = im2uint8(im(:)); num_bins = 256; counts = imhist(I,num_bins); P = counts/sum(counts); omega = cumsum(p); mu = cusum(p.*(1:num_bins)'); mu_t = mu(end);previous_state = warning('off','MATLAB:divideBYZero');sigma_b_squared = (mu_t*omega-mu).^2./(omega.*(1-omega));warning(previous_state);maxval = max(sigma_b_squared);isfinite_maxval = isfinite(maxval);if isfinite_maxval idx = mean(find(sigma_b_squared == maxval)); level = (idx-1)/(num_bins-1); else level = 0.0;endif nargout > 1 if isfinite_maxval em = maxval/sum(p.*((1:num_bins).^2)')-mu_t^2); else em = 0; endendout = (im>level*max(im(:)));disp(['Threshold =',num2str(round(255*level))])
However, this code cannot be run successfully. The command window state that
Error: File: OTSUBLOCK.m Line: 3 Column: 1Function definitions are not permitted in this context.
Please kindly give me advices
Thankyou!
Best Answer