bwboundaries returning empty cell array

The attached binary array should have two regions, one small one big. When I run:
[B L] = bwboundaries(BW);
B has one full cell corresponding to the small region, and one empty cell i assume should be the large region. Further, the L array correctly sets the larger region values to 2, so I know the large region is being detected. So why is that B cell empty?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 8 Aug. 2017

0 Stimmen

There are either 1 8-connected region or 4 four connected regions:
s = load('bin_array.mat')
BW = s.BW;
imshow(BW);
[boundaries, L] = bwboundaries(BW, 8);
fprintf('With 8-connected, there are %d boundaries.\n', length(boundaries));
[boundaries, L] = bwboundaries(BW, 4);
fprintf('With 4-connected, there are %d boundaries.\n', length(boundaries));
BW: [493×160 double]
With 8-connected, there are 1 boundaries.
With 4-connected, there are 2 boundaries.
You must not be using 4 as the second argument. If you use nothing, or 8, you will have L be 1 everywhere, not 2 like you said. The max value of L will always match the number of regions. So your problem cannot be reproduced, and always gives the correct answer. If you don't think so, then post your code.

5 Kommentare

There is a discrepancy between our results. The L array I generate does indeed contain values of 2, and the length of my boundaries array is 2 (this is regardless of conn). To be sure, I ran the code you offered and got the following output:
if true
BW: [493×160 double]
With 8-connected, there are 2 boundaries.
With 4-connected, there are 2 boundaries.
end
Also, I have two other issues, which may or may not be related:
1. The explanation does not explain why the second cell would be empty. It should not exist at all.
2. The purpose of this is to identify the number of regions in the BW image, and be able to calculate the coordinates associated wth each region. Without knowing BW, i have no way of automating whether conn should be 4 or 8.
Try casting BW from double to logical. See if that helps.
You can find out the number of regions with bwlabel:
BW = logical(s.BW);
[labeledImage, numregions] = bwlabel(BW);
You don't need to compute boundaries to get the number of regions. And I don't think knowing that should help you determine the connectedness your should use in your algorithm.
I don't understand your last sentence. Deciding whether to use 4 or 8 connected is something you decide in advance, not change on the fly. Anyway, you do know BW - it's your image. You're just not getting the right labeled image or number of regions.
Just a wild guess: you didn't write your own bwboundaries function did you? What does this show:
>> which -all bwboundaries
Since I can't reproduce your problem, you'll have to call the Mathwork technical support. Let them know all your "ver" information especially your release number and license number.
Zachary
Zachary am 9 Aug. 2017
I tried casting BW as a logical; no luck.
I do in fact alter code sometimes, saving it as an alternate function. Unfortunately, not the case here. I ran the which function both before and after I set my paths, the function is the original.
So, I should have been more clear, I apologize. I am using bwboundaries as part of an automated spike detector I designed for iEEGs. The signal is run through a time-frequency analysis, generating the image, which is then analyzed using image processing (though this is not the most efficient method of spike detection, I find it to have higher precision and specificity for my needs). The algorithm does use bwboundaries to count regions, but it also requires the actual boundaries. Further, autonomy requires the algorithm to run without viewing the actual image. Again, I should have been more specific. One possible work around might be to set the algorithm to ALWAYS use conn = 4; I can look into this.
My problem, still, begs investigation. I am concerned by the fact that my results are not reproducible. Does your L array only contain 2s when conn=4? I will dive into the bwboundaries code and see if I may have off handedly altered it, although I am pretty sure I don't have the permissions.
Thank you for working with me on this, this problem has gotten interesting, and I appreciate the assistance.
Image Analyst
Image Analyst am 9 Aug. 2017
My L contains values of 0, 1, and 2 when conn = 4.
I'll be traveling for the next 6 or 7 days in Washington DC so won't be able to answer more than a few hours from now.
Zachary
Zachary am 9 Aug. 2017
I tried it on a different computer and my results match yours, which is confirmation there is something different about the bwboundaries. I don't know how it got changed, but it did. The bwboundaries.m files are identical, thus I believe the issue is with the bwboundariesmex function, unfortunately that appears to be proprietary, so I don't have access.
I have generated a work around. I think my only option at this point is to reinstall the image processing toolbox, and if that doesn't work, call mathworks. Unfortunately, I am not admin on the server, so reinstalling might take a while. For now, I will consider this issue resolved. Thank you for your help. Enjoy DC.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by