Skip to content
Snippets Groups Projects
OtsuThresholdValue.m 1.54 KiB
Newer Older
% OtsuThresholdValue - threshold = OtsuThresholdValue(imageIn,device) 
function threshold = OtsuThresholdValue(imageIn)
    % check for Cuda capable devices
    curPath = which('ImProc.Cuda');
    curPath = fileparts(curPath);
    n = ImProc.Cuda.DeviceCount();

    % if there are devices find the availble one and grab the mutex
    if (n>0)
       foundDevice = false;
       device = -1;
       
       while(~foundDevice)
        for deviceIdx=1:n
            mutexfile = fullfile(curPath,sprintf('device%02d.txt',deviceIdx));
            if (~exist(mutexfile,'file'))
                try
                       fclose(fopen(mutexfile,'wt'));
                catch errMsg
                       continue;
                end
                foundDevice = true;
                device = deviceIdx;
                break;
            end
        end
        if (~foundDevice)
            pause(2);
        end
       end
       
       try
            threshold = ImProc.Cuda.OtsuThresholdValue(imageIn,device);
        catch errMsg
        	delete(mutexfile);
        	throw(errMsg);
        end
        
        delete(mutexfile);
    else
        threshold = lclOtsuThresholdValue(imageIn);

function threshold = lclOtsuThresholdValue(imageIn)
    if nargin ~= 1
        error('Incorrect number of inputs!')
    end
    
    if length(size(imageIn)) > 3
        error('Image can have a maximum of three dimensions!')
    end
    
    if ~isa(imageIn, 'double')
        imageIn = im2double(imageIn);
    end
    
    threshold = graythresh(imageIn);