user3483746
user3483746

Reputation: 155

regionprops() is giving an error in matlab

I'm using regionprops() but I'm getting an error with the following code:

J = imread('E:\Canopy New Exp\Data Set\input.jpg');
I = rgb2gray(J);
BW = regionprops(J,'basic');
stats = regionprops('table',BW,'Centroid',...
     'MajorAxisLength','MinorAxisLength');
centers = stats.Centroid;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;

hold on;
viscircles(centers,radii);
hold off;

But I'm getting the following error:

Error using regionprops
Expected input number 1, L, to be one of these types:

double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64

Instead its type was char.

Error in regionprops (line 142)
     validateattributes(L, {'numeric'}, ...

Error in Untitled (line 8)
stats = regionprops('table',BW,'Centroid',...

Any suggestions?

Thanks in advance!

Upvotes: 1

Views: 1564

Answers (3)

cwissy
cwissy

Reputation: 513

You are doing regionprops twice, and the second time with 'table' as the first parameter. regionprops expects an image (black and white, connected components, or labelled) as the first parameter, so that is why you are getting the error type char.

Instead feed in a black and white (binary) image to one call of regionprops, and that should be done:

thresh = graythresh(I); % get a threshold (you could just pick one)
I_BW = im2bw(I,thresh); % make the image binary with the given threshold
stats = regionprops(I_BW,'basic'); % do regionprops on the thresholded image

You can also do regionprops with 2 image parameters, one to show the regions in the other, so instead of the regionprops call above, you could possibly try:

stats = regionprops(I_BW, J, 'basic');

Upvotes: 3

hiandbaii
hiandbaii

Reputation: 1331

You're probably feeding it a RGB array NxMx3. regionprops takes a NxM binary array according to the documentation.

Upvotes: 1

xenoclast
xenoclast

Reputation: 1635

regionprops outputs an object so in the third line of the above code sample you call it on J, an image, which is fine and returns an appropriate object BW. But then in the following line you call it again on the BW object and that's where the error comes from. It isn't meaningful to call it twice on successive objects, but it's more likely that that wasn't your intention and you meant to binarise the image first with im2bw.

When you read the error messages output by matlab be aware that the bottom line is the line in your code where the error occurred. If you are supplying the wrong kind of input to one of matlab's builtin functions (this is by far the most common kind of error in my own experience) then it won't be until you've gone deeper into matlab's internal functions that the error manifests.

So reading the error report from the bottom upwards you go deeper into the call stack until the top line, which is the 'actual' error. That top line gives you the cause of the conflict, which is half the story. You can then take that half back to the line of your code to see why it happened and how to fix it.

Upvotes: 1

Related Questions