Otsu Threshold algorithm function for blockproc
Show older comments
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;
end
if ~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;
end
if nargout > 1
if isfinite_maxval
em = maxval/sum(p.*((1:num_bins).^2)')-mu_t^2);
else
em = 0;
end
end
out = (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: 1
Function definitions are not permitted in this context.
Please kindly give me advices
Thankyou!
Accepted Answer
More Answers (0)
Categories
Find more on Image Thresholding 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!