I have an image that contains some points (or objects). I want to create another image base on this image that shows the distance from these objects. For example, this new image should has the maximum value at the object locations. Is there any solution in matlab?
Asked
Active
Viewed 1,933 times
3
-
What distance are you trying to show? "...the distance from these objects" does not make it clear what you are measuring relative to. – dustincarr Dec 05 '12 at 19:12
1 Answers
6
You can use bwdist
for this, which calculates the distance of each pixel from the signal in a binary image.
%# read the image
img = imread('https://i.stack.imgur.com/Hc7ay.png');
%# convert to grayscale
gs = im2bw(img);
%# segment the objects
sig = gs~=1;
%# remove the border
sig = sig(5:end-4,5:end-4);
%# calculate the distance
distFromObjects = -bwdist(sig);

Jonas
- 74,690
- 10
- 137
- 177
-
-
-
Thanks a lot Jonas, but how I can control the radius? There is no such option in matlab and in bwdist. – Sam Dec 05 '12 at 23:48
-
@Sam: when you want to show only a specific distance, such as 10 pixels, you can do `imshow(distFromObjects==10)` – Jonas Dec 06 '12 at 00:40
-
@Jonas, I posted an interesting question in SO. Maybe you're also interested in :-) http://stackoverflow.com/questions/22116123/matlab-relabeling-pixels-based-on-distance-between-objects-centerline-and-boun – Tin Mar 01 '14 at 17:33