Search code examples
matlabimage-processingsimulink

matlab imfindcircles : how to deal when no circle is found


i wish to track a circle with a webcam using imfindcircles in a Simulink model. There may be times in the picture when no circle is visible on the image.

I use the following code in a MATLAB function block :

    function centreOutput = fcn(image)
    coder.extrinsic('imfindcircles');
    coder.extrinsic('find');
    coder.extrinsic('max');
    temp = 0;
    rayonMax = 0;
    rayons = coder.nullcopy(zeros(1,1));
    centres = coder.nullcopy(zeros(1,2));
    %set the radius
    [centres, rayons] = imfindcircles(image,[20 60],'Sensitivity',0.9,'EdgeThreshold',0.5,'ObjectPolarity','bright');
    rayonMax = min(rayons);
    temp = find(rayons==rayonMax);
    centreOutput = centres(temp,:);
    end

my problem is that i get the following error message :

Size mismatch for MATLAB expression 'rayons'. Expected = 1x1 Actual = 0x0 Block MATLAB Function (#41) While executing: State During Action

as i understand it, the dimension of centres is not as declared when no circle is found : how could i deal with this when there is no circle to be found (like with a test that would be "if one circle is found...")


Solution

  • i got it : i just tested the function imfindcircles before assigning its results or not :

    ...
    if (isempty(imfindcircles(image,[30 50],'Sensitivity',0.9)))
        centreOutput = [0 0];
    else 
    [centres, rayons] = imfindcircles(image,[30 50],'Sensitivity',0.9);
        rayonMax = min(rayons);
        temp = find(rayons==rayonMax);
        centreOutput = centres(temp,:);
    end
    ...
    

    your answer helped me find the right syntax, thanks for the help :)