0

I am getting the following error message in Matlab "Unrecognized function or variable 'adaptivethreshold'" when every time I want to use Adaptive Threshold function

clear;close all;
im1=imread('page.png');
im2=imread('tshape.png');
bwim1=adaptivethreshold(im1,11,0.03,0);
bwim2=adaptivethreshold(im2,15,0.02,0);
subplot(2,2,1);
imshow(im1);
subplot(2,2,2);
imshow(bwim1);
subplot(2,2,3);
imshow(im2);
subplot(2,2,4);
imshow(bwim2);

those coding from https://www.mathworks.com/matlabcentral/fileexchange/8647-local-adaptive-thresholding

  • Is the folder where the png files and adaptivethreshold function in your path? If not you either need add that folder to your path of run your code from that folder – ThomasK Jun 21 '22 at 07:53

1 Answers1

0

You should open MATLAB editor inside a folder containing all files: adaptivethreshold.m, page.png, testadaptivethreshold.m, tshape.png and run your script testadaptivethreshold.

enter image description here

Or, you can copy-paste the function definition below the script as follows and make sure also that the images are in the same folder or MATLAB search path.

clear;close all;
im1=imread('page.png');
im2=imread('tshape.png');
bwim1=adaptivethreshold(im1,11,0.03,0);
bwim2=adaptivethreshold(im2,15,0.02,0);
subplot(2,2,1);
imshow(im1);
subplot(2,2,2);
imshow(bwim1);
subplot(2,2,3);
imshow(im2);
subplot(2,2,4);
imshow(bwim2);


function bw=adaptivethreshold(IM,ws,C,tm)
    %ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
    %foreground from the background with nonuniform illumination.
    %  bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local 
    %   threshold mean-C or median-C to the image IM.
    %  ws is the local window size.
    %  tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
    %
    %  Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
    %  at Tsinghua University, Beijing, China.
    %
    %  For more information, please see
    %  http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm

    if (nargin<3)
        error('You must provide the image IM, the window size ws, and C.');
    elseif (nargin==3)
        tm=0;
    elseif (tm~=0 && tm~=1)
        error('tm must be 0 or 1.');
    end

    IM=mat2gray(IM);

    if tm==0
        mIM=imfilter(IM,fspecial('average',ws),'replicate');
    else
        mIM=medfilt2(IM,[ws ws]);
    end
    sIM=mIM-IM-C;
    bw=imbinarize(sIM,0);
    bw=imcomplement(bw);
end

And here is how it shows the output:

enter image description here

AboAmmar
  • 5,439
  • 2
  • 13
  • 24