1

Why is the bwdist and imhmin image completely black? When I check the the two matrices in the workspace, the values seem fine. You can run this code in Matlab to check it out. The expected result is a grayscale image where the centre of each connected component has low intensity, while the edges of the connected components have high intensity. The pixel distance of the edges are further from the centroid and would have a high distance value. If the distance values are considered to be an intensity, thus high intensity at the edges.

Consider the 1's as the connected components to be distance transformed while the 0's are merely the background. I would like to have the background as 0's.

image = zeros(5,5);
image(2,2) = 1;
image(4,4) = 1;

binary = im2bw(image,graythresh(image));
distance = bwdist(binary,'cityblock');
hminima = imhmin(distance,5);

figure; imshow(binary)
figure; imshow(distace)
figure; imshow(hminima)

The needed result is something like this.

enter image description here

Senyokbalgul
  • 1,058
  • 4
  • 13
  • 37
  • Why is `distance` negative? – beaker Aug 31 '16 at 15:23
  • @beaker I've edited the question. The distance transformed image after `bwdist(~binary,'cityblock')` looks exactly the same as the binary one for some reason. – Senyokbalgul Aug 31 '16 at 15:47
  • Because that's the distance that's computed. It would be helpful if you described what you're trying to achieve and the results that you expect to get. – beaker Aug 31 '16 at 16:07
  • @beaker The expected result is a grayscale image where the centre of each connected component has low intensity, while the edges of the connected components have high intensity. The pixel distance of the edges are further from the centroid and would have a high distance value. If the distance values are considered to be an intensity, thus high intensity at the edges. – Senyokbalgul Aug 31 '16 at 16:32
  • `bwdist` for any pixel that is 0 / black computes the distance to the closest non-zero pixel. You are inverting the image first, so that means that there are only two zero pixels in the image. The closest non-zero pixels to any of these is in fact 1 and since the other pixels are non-zero, these are simply set to 0 as per the definition of `bwdist`. That's why you get the `distance` variable to be the same as the original. It is unclear as to why you are inverting the image first. – rayryeng Aug 31 '16 at 16:51
  • 1
    Clarification should be edited into the original question. Comments are not permanent. What do you consider your connected components, the single pixels you added to the blank image, or the `true` pixels of the inverted image? – beaker Aug 31 '16 at 16:52
  • And what do you want the values to be *between* the connected components? – beaker Aug 31 '16 at 17:08
  • @beaker I have edited the question and clarified the needed result. – Senyokbalgul Aug 31 '16 at 17:14
  • @rayryeng The image was inverted since I wanted the background to be black instead of white. – Senyokbalgul Aug 31 '16 at 17:15
  • That doesn't make sense for the distance transform. You don't know how `bwdist` works if you are providing that input. – rayryeng Aug 31 '16 at 17:19
  • You have a 5x5 image you've constructed. Please show us the `distance` image you want from this. What you're trying to do sounds simple enough, but you've made contradictory statements that confuse this. For example, your original image is all black with 2 white pixels. Then you say you invert the image so that the background is black instead of white. How is the background white in your constructed image? – beaker Aug 31 '16 at 17:37
  • At any rate, I have to leave for a bit. I suspect you want to do something like `max(distance(:)) - distance` (maybe `+1`) rather than take the negative. But then you'll need to scale it properly and filter out the background pixels. – beaker Aug 31 '16 at 17:39
  • @beaker I have provided an example of what is needed. I think this should be easier to explain what is needed. – Senyokbalgul Aug 31 '16 at 17:46
  • @rayryeng I have provided an example of what is needed. I think this should be easier to explain what is needed. – Senyokbalgul Aug 31 '16 at 17:47

1 Answers1

2

I don't know if I'm understanding the question or not, but it seems from the most recent edit that you want this line

figure; imshow(distace)

to look like the image you posted. To accomplish that, you have to understand that imshow produces a grayscale image, mapping 0 to black and 1 as white. In your example, distance varies from 0 to 5. If you use the following code

scaledDistance = distance/max(distance(:));
figure; imshow(scaledDistance)

You will get a grayscale image instead of just two dots. If you use this code:

scaledDistance = distance/max(distance(:));
figure; imshow(1 - scaledDistance)

It will be inverted to be similar to the image you posted.

Trogdor
  • 1,346
  • 9
  • 16