bwdist feature transform output is wrong class and imprecise?
5 views (last 30 days)
Show older comments
I am attempting to use the feature transform of bwdist, however, I find that the linear index IDs in the feature transform output do not match the linear index values of the input matrix. Secondly, the class of the output is single while the matlab website says that it should be uint32 or uint64. Is there something I am doing wrong?
Here is an example of what I am trying to do:
cellPos = rand(12000,12000);
cellPos(cellPos<.99999)= 0; % create ~ 1400 true instances in matrix
cellPos = logical(cellPos);
[cellDist, cellLabel] = bwdist(cellPos);
class(cellLabel) % should this be uint32?
cellID = find(cellPos); % linear index values of input matrix
ismember(cellID,cellLabel) % why is this not all 1's
Thank you for any help
0 Comments
Accepted Answer
Teja Muppirala
on 24 Aug 2012
After checking on this, you're right. In R2010b, it was returning IDX as a single, and because a single can only represent consecutive integers up to 2^24, it fails for large images that have more than this many pixels. This was changed in R2012a to return uint32/uint64 instead. The bwdist_old function might still work though.
0 Comments
More Answers (1)
Image Analyst
on 22 Aug 2012
[D,IDX] = bwdist(BW)
BW can be numeric or logical, and it must be nonsparse. D is a single matrix with the same size as BW. The class of IDX depends on the number of elements in the input image, and is determined using the following table.
So the web site says it will be single, and it is. Where do you see that the "website says that it should be uint32 or uint64"?
3 Comments
Teja Muppirala
on 23 Aug 2012
From the documentation: [D,IDX] = bwdist(BW)
From your command: [cellDist, cellLabel] = bwdist(cellPos);
So, in other words,
cellDist <--> D
cellLabel <--> IDX
The first one "cellDist" (D) represents actual distance. It is a real number like 17.2313 or 78.8342 or 65.4539. The documentation says that D is a single precision matrix. If you type
class(cellDist)
you will find that it is in fact a single precision matrix.
On the other hand, "cellLabel" (IDX) refers to index values. These are positive integers like 5623 or 9831 or 815. Since these are always going to be positive integers, it makes sense to store them as an unsigned integer datatype. UINT32 can store up to 2^32-1, which is the maximum index that you could get out of about a 4 gigapixel image. The documentation says the class of IDX will be uint32, and as you can see
class(cellLabel)
is indeed uint32. So there is nothing weird or inconsistent here.
As far as why this:
ismember(cellID,cellLabel)
is not all ones, I am not able to reproduce your results. It certainly gives all ones for me. What MATLAB version are you using? Does the following code not give you ans = 1?
rng(0); % Fix the random number state for repeatability
cellPos = rand(12000,12000);
cellPos(cellPos<.99999)= 0; % create ~ 1400 true instances in matrix
cellPos = logical(cellPos);
[cellDist, cellLabel] = bwdist(cellPos);
cellID = find(cellPos);
all(ismember(cellID,cellLabel))
See Also
Categories
Find more on Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!