MATLAB ConnectedComponentLabeler does not work in for loop

90 views Asked by At

I am trying to get a set of binary images' eccentricity and solidity values using the regionprops function. I obtain the label matrix using the vision.ConnectedComponentLabeler function.

This is the code I have so far:

files = getFiles('images');
ecc = zeros(length(files)); %eccentricity values
sol = zeros(length(files)); %solidity values

ccl = vision.ConnectedComponentLabeler;

for i=1:length(files)
 
    I = imread(files{i});
    
    [L NUM] = step(ccl, I);


    for j=1:NUM
      L = changem(L==j, 1, j); %*
    end

    stats = regionprops(L, 'all');

    ecc(i) = stats.Eccentricity;
    sol(i) = stats.Solidity;

end

However, when I run this, I get an error says indicating the line marked with *:

Error using ConnectedComponentLabeler/step

Variable-size input signals are not supported when the OutputDataType property is set to 'Automatic'.'

I do not understand what MATLAB is talking about and I do not have any idea about how to get rid of it.

Edit

I have returned back to bwlabel function and have no problems now.

1

There are 1 answers

1
rayryeng On BEST ANSWER

The error is a bit hard to understand, but I can explain what exactly it means. When you use the CVST Connected Components Labeller, it assumes that all of your images that you're going to use with the function are all the same size. That error happens because it looks like the images aren't... hence the notion about "Variable-size input signals".

The "Automatic" property means that the output data type of the images are automatic, meaning that you don't have to worry about whether the data type of the output is uint8, uint16, etc. If you want to remove this error, you need to manually set the output data type of the images produced by this labeller, or the OutputDataType property to be static. Hopefully, the images in the directory you're reading are all the same data type, so override this field to be a data type that this function accepts. The available types are uint8, uint16 and uint32. Therefore, assuming your images were uint8 for example, do this before you run your loop:

ccl = vision.ConnectedComponentLabeler;
ccl.OutputDataType = 'uint8';

Now run your code, and it should work. Bear in mind that the input needs to be logical for this to have any meaningful output.

Minor comment

Why are you using the CVST Connected Component Labeller when the Image Processing Toolbox bwlabel function works exactly the same way? As you are using regionprops, you have access to the Image Processing Toolbox, so this should be available to you. It's much simpler to use and requires no setup: http://www.mathworks.com/help/images/ref/bwlabel.html