The use of Nargin in Otsu Image Processing
4 views (last 30 days)
Show older comments
I was actually given the full code to use from my professor and all I had to do was process it with a couple of photos. However I'm running into a few problems with the given code and I've tried to understand why it's not working but I think I could use some help. Here it is:
This is the function I think?
%Otsu Thresholding
% [ot,outim]=otsu_thresh(im)
% where im is the grey image (integer 0-255)
% Otsu Thresholding is performed on the image
% ot is the threshold determined
% outim is the image after thresholding
if nargin<1
im=mean(double(imread('10um.jpg')),3);
else
if max(max(imread('10um.jpg')))<=1
im=round(im.*255);
end
end
[height,width]=size(im);
pixels=height*width;
bins=[0:255]+0.5;
N=hist(reshape(im,pixels,1),bins);
Nnorm=N/sum(N); %normalising the bin frequencies to make probabilities
theta=cumsum(Nnorm); %cumulative probability
mu=cumsum(Nnorm.*[0:255]);
%Repeat on the image
sigB2=(mu-mu(256)*theta).^2./(theta.*(1-theta)); %evaluate sigB2 over the t range
[p ot]=max(sigB2); %find the maximum value and the index where it is (this is the Otsu threshold)
and this is the script im supposed to run:
x=(im>ot); %thresholding
figure(1)
imshow(x)
figure(2)
imshow(x)
hist(x);
(I believe there is 0 problems with the actual script just the function)
0 Comments
Answers (1)
Image Analyst
on 29 Jul 2016
You're not using the im that was passed in - you're just overwriting it with some predefined image. Try this:
if nargin < 1
im = mean(double(imread('10um.jpg')),3);
else
if max(im(:)) <= 1
% Scal to max possible gray level for its class if it's less than 1.
maxPossibleValue = double(intmax(class(im)))
im = round(maxPossibleValue * double(im));
end
end
3 Comments
Walter Roberson
on 29 Jul 2016
You need to put that code inside a "function" -- that is, a .m file that starts with the word "function". nargin is not permitted inside a script (that is, at the command line, or in a .m file that does not start with either "function" or "classdef")
I suggest you just use
if max(im(:)) <= 1
% Scal to max possible gray level for its class if it's less than 1.
maxPossibleValue = double(intmax(class(im)))
im = round(maxPossibleValue * double(im));
end
without the nargin test.
Image Analyst
on 30 Jul 2016
For some reason you commented out the function line. Put it back in:
function [ot,outim]=otsu_thresh(im)
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!