how can I run function in file exchange

I download this file exchange
but I couldnt run it , in command window get
Error: File: sauvola.m Line: 55 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "sauvola".)
Error in filterbri (line 20)
S=sauvola(img,[150 150]);
how to run this file exchange?

2 Comments

Stephen23
Stephen23 on 10 Jul 2023
Edited: Stephen23 on 10 Jul 2023
" (It follows the END that terminates the definition of the function "sauvola".)"
The two functions provided in that FEX submission do not use END to mark the end of those functions.
So you must have modified the functions or be referring to some other functions. It looks as if you might be trying to save them inside a script... which is not required: just download those functions and call them. Or perhaps you have nemd your own script/function with the same name: best avoided.
I am biggener in matlab, thus can you expalin more how to do it
for example If i have the attached image, how can I use function to get the threshold for it?

Sign in to comment.

 Accepted Answer

MarKf
MarKf on 10 Jul 2023
You're right, that sauvola.m that you linked has only 45 lines (but there's no actual end that terminates the function definition, unless you meant the end of file).
So maybe there is another sauvola.m in your path. you can try which sauvola to check if it's the same one.
Also you did not copy and paste the whole error so we arevactually unsure what the issue is.

8 Comments

%SAUVOLA local thresholding.
% BW = SAUVOLA(IMAGE) performs local thresholding of a two-dimensional
% array IMAGE with Sauvola algorithm.
%
% BW = SAUVOLA(IMAGE, [M N], THRESHOLD, PADDING) performs local
% thresholding with M-by-N neighbourhood (default is 3-by-3) and
% threshold THRESHOLD between 0 and 1 (default is 0.34).
% To deal with border pixels the image is padded with one of
% PADARRAY options (default is 'replicate').
%
% Example
% -------
% imshow(sauvola(imread('eight.tif'), [150 150]));
%
% See also PADARRAY, RGB2GRAY.
% For method description see:
% http://www.dfki.uni-kl.de/~shafait/papers/Shafait-efficient-binarization-SPIE08.pdf
% Contributed by Jan Motl (jan@motl.us)
% $Revision: 1.1 $ $Date: 2013/03/09 16:58:01 $
function output=sauvola(image, varargin)
% Initialization
numvarargs = length(varargin); % only want 3 optional inputs at most
if numvarargs > 3
error('myfuns:somefun2Alt:TooManyInputs', ...
'Possible parameters are: (image, [m n], threshold, padding)');
end
optargs = {[3 3] 0.34 'replicate'}; % set defaults
optargs(1:numvarargs) = varargin; % use memorable variable names
[window, k, padding] = optargs{:};
if ndims(image) ~= 2
error('The input image must be a two-dimensional array.');
end
% Convert to double
image = double(image);
% Mean value
mean = averagefilter(image, window, padding);
% Standard deviation
meanSquare = averagefilter(image.^2, window, padding);
deviation = (meanSquare - mean.^2).^0.5;
% Sauvola
R = max(deviation(:));
threshold = mean.*(1 + k * (deviation / R-1));
output = (image > threshold);
end
% Mean value
mean = averagefilter(image, window, padding);
% Standard deviation
meanSquare = averagefilter(image.^2, window, padding);
deviation = (meanSquare - mean.^2).^0.5;
% Sauvola
R = max(deviation(:));
threshold = mean.*(1 + k * (deviation / R-1));
output = (image > threshold);
The error is:
Error: File: sauvola.m Line: 55 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "sauvola".)
what i have to or to add change in the upper code,and where I have to put my image?
unfortunatly i am biggener in matlab
Since you said you are a beginner maybe answering your actual question "how can I run function in file exchange" may actually be useful.
You can download the files and add them in you path. Just having the files in your current folder (where you run your script/functions from) will let Matlab access them when you call them from your script/functions. They are ensured to run then. You can't run them by themselves, unless they are meant to (like if they have an openfile gui), but most are not, they are just helpers for script/functions you have to write yourself.
If you want you could make changes to the code if you know what you are doing. So I suggest against that until you have more confidence with this. Do have a look at beginner matlab lessons. You don't even have to download them in that case you can just copy and paste, tho that's bad form. Just download in your path and leave the files as they are.
Open a new script and refer to the image file you want to apply the function to, open it (since from the description this function needs an image array apparently), then call the FE function (again unedited downloaded files should be in your path/folder). Something like this maybe:
filepath = 'CC1-3.jpg'; % or fullfile(folder,'CC1-3.jpg');
img = imread(filepath);
bwimg = sauvola(img); % local thresholding with standard values
%% eg parameters
thresh = 0.5;
mnei = [4,4];
bwimg_2 = sauvola(img,mnei,thresh); % define your parameters
%% plot or save
figure, image(bwimg)
figure, image(bwimg_2)
saveas(bwimg,'bwimg.png') %also see print
Consider accepting the answer if it helped.
@MarKf first of all I appreciate your helps
I down oaded the file as shown in the attactment(T) (screenshot)
My manuscript name is filterbri.m in the same folder of savoula, when I run the upper your written code aagin error appear
>> filterbri
Error: File: sauvola.m Line: 55 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "sauvola".)
Error in filterbri (line 24)
bwimg = sauvola(filepath); % local thresholding with standard values
"Error: File: sauvola.m Line: 55 Column: 1"
As MarkF already wrote, the function SAUVOLA does not have 55 lines (it ends at 52 with a trailing newline), so that error message tells us that you must have made some modifications or are calling another function.
The solution is to not modify the Mfile and to call the correct function (e.g. no identical function names).
Oh so "(It follows the END that terminates the definition of the function "sauvola".)" was an actual line from a matlab error? That's strange and there must something extra that you are doing (or your OS or Matlab is doing) that is not among the steps below.
Ok so I created a folder, download the image file CC1-3.jpg above, download the 2 FE files in the same folder, extract them there, open matlab (to discover I have an issue with the licence after a change in security with my university and needed a one time password, solve this, but you can igore this one thing), copy and paste the code above in a script, cd into the folder with the image and the FE functions, run it and get this error instead:
Error using sauvola (line 36)
The input image must be a two-dimensional array.
Error in sauvola_script (line 3)
bwimg = sauvola(img); % local thresholding with standard values
because yes the FE function needs a simple mat with grayscale apparently (or just a single color channel), alright whatever, I thought this answer would take a minute. So after img = imread(filepath); I add img = rgb2gray(img); and the script works (except saveas, which works with figure handles not matrices, should have actually used imwrite or print). So anyway here is the updated script and attached the resultin image:
filepath = 'CC1-3.jpg'; % or fullfile(folder,'CC1-3.jpg');
img = imread(filepath);
imgray = rgb2gray(img); % or chose one of the rgb channels (3rd dimension)
% figure, image(imgray) % visualise grayscale image
bwimg = sauvola(imgray); % local thresholding with standard values
%% plot or save
figure, imagesc(bwimg) % imagesc %instead of image() because it's a logical and not a 256 values color
imwrite(bwimg,'bwimg.png') %also see print
@MarKf Thanks alot for your help,its working now
I have another issue in using niblack file exchange for thresholding
I used same way in sauvola in niblack as following
filepath = 'Stre.jpg';
img = imread(filepath);
imgray = rgb2gray(img); % or chose one of the rgb channels (3rd dimension)
% figure, image(imgray) % visualise grayscale image
bwimg = niblack(imgray); % local thresholding with standard values
imshow(bwimg)
I got the following error:
Index in position 2 exceeds array bounds (must not exceed 942).
Error in averagefilter (line 90)
imageI = t(1+m:rows+m, 1+n:columns+n) + t(1:rows, 1:columns)...
Error in niblack (line 42)
mean = averagefilter(image, window, padding);
Error in check1 (line 5)
bwimg = niblack(imgray); % local thresholding with standard values
can you help, please?
it is working well but when I take the result as input to to get threshold by savoul the result appear with outerframe as shown in attachment
how can I remove this frame or not allowed it to appear?

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type 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!