MATLAB Answers

Ryan
0

BWDIST label matrix not returning expected labels

Asked by Ryan
on 17 Aug 2012
I am trying to use the label matrix feature of bwdist, but am encountering a problem. Instead of returning the linear index of the nearest non-zero pixel, the function is returning a matrix where every pixel's value is its own linear index. I am assuming I am misusing or misunderstanding the function, but am not sure how?
Example:
% Generate test image
BW = zeros(500);
BW(50:90,1:500) = 1; figure,imshow(BW)
[distMap,labelMask] = bwdist(~BW);
figure,imshow(labelMask,[]) % It's a gradient because of the issue noted above
It is my understanding that the labelMask here should contain mostly zeros with linear index values in the rectangular region, or at least some deviation in the rectangular region from the rest of the image.

  0 Comments

Sign in to comment.

2 Answers

Answer by Kevin Claytor on 17 Aug 2012
 Accepted Answer

I think you really want the distMap output, which gives you the distance to the nearest non-zero pixel.
labelMask gives you the index of the nearest non-zero pixel. For most of these pixels, that index is itself. Since the index is computed as i = row + col*numrows, this increases linearly with the row and col value of the pixel.
Check out the matrix itself (labelMask(45:50,1:4)) the values increase and then BOOM! When you hit 50 they flatten off - because the last pixel was the non-zero one.

  0 Comments

Sign in to comment.


Answer by Image Analyst
on 17 Aug 2012

Makes sense to me. I think you need to read this explanation over and over until you understand it: "Each element of IDX contains the linear index of the nearest nonzero pixel of BW." Then realize what the linear indexes are for each pixel. Any pixel in the white band will have that pixel as it's closest pixel, and any pixel in the big black band at the bottom will have the pixel at the bottom of the white band directly above it as the closest pixel, no matter what row you're in. For example, row 90 is the closest row no matter if you're in row 100, row 200, row 300, etc. And it goes up as you go from left to right because the column numbers increase from left to right. For example, the pixel at (90,100) has one linear index (=500*99+90) while the pixel next to it has a linear index 500 higher (=500*99+90), and they keep increasing as you go from left to right.
Do you think you have a need for this functionality?

  1 Comment

@Your last question: I am doing the distance transform on the inverse of the white rectangle image (to record the "radius" of the rectangle), but this is arbitrary for the sake of discussion. I was looking to use the label matrix to draw some lines showing the found radius at different points on the image, just for visualization purposes (I am using the bwdist and watershed functions in tandem to find the "center" of some lines to get the average radii of the lines, I was looking to draw lines at some of these locations with correct orientation, hence my interest in the labelMask).
@Your explanation (which I promise I re-read multiple times): I agree that what you said is what one would expect, but that's not the outcome. The gradient is actually slightly angled from the top left corner to the bottom right corner and when I manually look through the labelMask values they are always the index for that pixel. So in the labelMask, column 1 has the values (1-500) traveling down the rows, column 2 has the values (501-1000) and so on. As I understand the function description (and your explanation), rows 90:500 in column 1 should all contain the same value, the index of (90,1).

Sign in to comment.