4 views (last 30 days)

Show older comments

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.

Kevin Claytor
on 17 Aug 2012

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.

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?

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

Start Hunting!