line/edge detection and output of coordinates.

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

line/edge detection and output of coordinates.

Dharhas Pothina
Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

example_small.jpg (46K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

David Baddeley
Hi Dharhas,

most traditional edge detection algorithms use some kind of gradient based filter - your image has two features which suggest that this approach might not be ideal. First it is noisy (which means that any gradient image will be even noisier) and second, you actually have relatively good contrast as to what is part of the object and what is not, and might not need gradient based edge detection per se. I'd take the following approach:

- low pass filter with scipy.ndimage.gaussian_filter to reduce the noise
- threshold the image to get a binary mask of your band (eg mask = image > threshold)
- potentially use a morphological operation such as binary closing (scipy.ndimage.binary_closing) or hole filling to tidy up the mask

from this mask you have a number of options to get the edges:
A - do a binary dilation and then subtract the original mask - this should give you a mask with all the edge pixels (both top and bottom). Applying ndimage.label to this mask might allow you to extract masks of both edges separately.

B - as you want to find the top and bottom edges (rather than edges which can fold back on themselves), you could take the difference between the mask and a copy of the mask which has been shifted vertically by one pixel (eg mask[:-1,:] - mask[1:,:]). This should give you an image in which the top edge pixels have a value of 1, the bottom edge has a value of -1, and all other pixels are zero.

once you have an image in which the pixels of each edge have different values, you can find the coordinates using numpy.where.

cheers,
David



----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: [hidden email]
Sent: Fri, 16 April, 2010 6:54:58 AM
Subject: [SciPy-User] line/edge detection and output of coordinates.

Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas


     
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Timmie
Administrator
In reply to this post by Dharhas Pothina
> I'm trying to do some line/edge detection in python. I've done some
> googling and found some mailing list archives that talked about ways
> to do edge detection but they seem to mainly return an image with the
> edges highlighted. What I need is x,y coordinates of the pixels that
> make up the lines. I
I'd go for a GIS application (GRASS, gvSIG, etc.):
1) import
2) convert raster to vector
3) export

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Chris Barker - NOAA Federal
Tim Michelsen wrote:
>> I'm trying to do some line/edge detection in python. I've done some
>> googling and found some mailing list archives that talked about ways
>> to do edge detection but they seem to mainly return an image with the
>> edges highlighted. What I need is x,y coordinates of the pixels that
>> make up the lines. I
> I'd go for a GIS application (GRASS, gvSIG, etc.):

or the GDAL command line utilities.


Another option is to look at drawing programs -- InkScape has something
that would probably work.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Charrett, Thomas
In reply to this post by Dharhas Pothina
You could try Radon / Hough transforms - which give you the gradient intercept of the lines.

Tom

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: 15 April 2010 19:55
To: [hidden email]
Subject: SciPy-User Digest, Vol 80, Issue 31

Send SciPy-User mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.scipy.org/mailman/listinfo/scipy-user
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of SciPy-User digest..."


Today's Topics:

   1. line/edge detection and output of coordinates. (Dharhas Pothina)


----------------------------------------------------------------------

Message: 1
Date: Thu, 15 Apr 2010 13:54:58 -0500
From: "Dharhas Pothina" <[hidden email]>
Subject: [SciPy-User] line/edge detection and output of coordinates.
To: <SciPy Users List <[hidden email]>
Message-ID: <[hidden email]>
Content-Type: text/plain; charset="us-ascii"

Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: example_small.jpg
Type: image/jpeg
Size: 35045 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20100415/bd67be42/attachment.jpg 

------------------------------

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


End of SciPy-User Digest, Vol 80, Issue 31
******************************************
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Dharhas Pothina
In reply to this post by Chris Barker - NOAA Federal
Tim/Chris,

So for the GIS/GDAL suggestion are you talking about exporting the lines once I've used another technique to highlight the edges? Or will the Raster 2 Vector functions also help with the edge detection.

- dharhas

>>> Christopher Barker <[hidden email]> 4/15/2010 4:42 PM >>>
Tim Michelsen wrote:
>> I'm trying to do some line/edge detection in python. I've done some
>> googling and found some mailing list archives that talked about ways
>> to do edge detection but they seem to mainly return an image with the
>> edges highlighted. What I need is x,y coordinates of the pixels that
>> make up the lines. I
> I'd go for a GIS application (GRASS, gvSIG, etc.):

or the GDAL command line utilities.


Another option is to look at drawing programs -- InkScape has something
that would probably work.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Dharhas Pothina
In reply to this post by Charrett, Thomas
Tom,

I found the Radon/Hough transforms while googling but all the examples I found were for detecting straight lines. Do they work for curves?

thanks

- dharhas

>>> "Charrett, Thomas" <[hidden email]> 4/16/2010 2:40 AM >>>
You could try Radon / Hough transforms - which give you the gradient intercept of the lines.

Tom

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: 15 April 2010 19:55
To: [hidden email]
Subject: SciPy-User Digest, Vol 80, Issue 31

Send SciPy-User mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.scipy.org/mailman/listinfo/scipy-user 
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of SciPy-User digest..."


Today's Topics:

   1. line/edge detection and output of coordinates. (Dharhas Pothina)


----------------------------------------------------------------------

Message: 1
Date: Thu, 15 Apr 2010 13:54:58 -0500
From: "Dharhas Pothina" <[hidden email]>
Subject: [SciPy-User] line/edge detection and output of coordinates.
To: <SciPy Users List <[hidden email]>
Message-ID: <[hidden email]>
Content-Type: text/plain; charset="us-ascii"

Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: example_small.jpg
Type: image/jpeg
Size: 35045 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20100415/bd67be42/attachment.jpg 

------------------------------

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user 


End of SciPy-User Digest, Vol 80, Issue 31
******************************************
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Chris Barker - NOAA Federal
In reply to this post by Dharhas Pothina
Dharhas Pothina wrote:
> So for the GIS/GDAL suggestion are you talking about exporting the
> lines once I've used another technique to highlight the edges? Or
> will the Raster 2 Vector functions also help with the edge detection.

In GIS, the a common problem is create contour lines from a digital
elevation model. In your case, you could think of the darkness of the
pixels as an "elevation", and thus contouring it would create a vector
outline of the dark pixels.

Another problem in GIS is taking a rater where each pixel value
represents some scalr quantity, like land cover type, and to turn that
into vector polygons -- that might actually be closer to what you need
than contouring.

One trick is that your data are noisy -- hopefully appropriate smoothing
will take care of that.

Now that I think about it, there are other contouring tools in python,
one in Matplotlib, for instance, that might work.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Dharhas Pothina

I like this idea. I'm more familiar with GDAL than with the scipy image processing tools. I think I'll try the suggestion of using a low pass filter to remove noise followed by contouring using the raster 2 vector tools in GDAL.

- dharhas

>>> Christopher Barker <[hidden email]> 4/16/2010 11:15 AM >>>
Dharhas Pothina wrote:
> So for the GIS/GDAL suggestion are you talking about exporting the
> lines once I've used another technique to highlight the edges? Or
> will the Raster 2 Vector functions also help with the edge detection.

In GIS, the a common problem is create contour lines from a digital
elevation model. In your case, you could think of the darkness of the
pixels as an "elevation", and thus contouring it would create a vector
outline of the dark pixels.

Another problem in GIS is taking a rater where each pixel value
represents some scalr quantity, like land cover type, and to turn that
into vector polygons -- that might actually be closer to what you need
than contouring.

One trick is that your data are noisy -- hopefully appropriate smoothing
will take care of that.

Now that I think about it, there are other contouring tools in python,
one in Matplotlib, for instance, that might work.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

Dharhas Pothina
In reply to this post by David Baddeley
David & Others,

Using the method similarto 'B' outlined by David below, I managed to get to an array of x,y coordinates that define the edge. I've attached the image to this email. As you can see,  I still have noise that needs to be removed. Any suggestions on how to do this?

thanks,

- dharhas




>>> David Baddeley <[hidden email]> 4/19/2010 5:51 AM >>>
Hi Dharhas,

I think the best approach would probably be trial and error - I'd start with a sigma of around 2-3 (all the other parameters can be left as their defaults ) and either decrease of increase it depending on whether you're blurring too much, or not enough. You might also want to take a look at ndimage.median_filter (again I'd experiment, this time with the size parameter), as there is a chance that this will let you smooth more without loosing edge definition.

cheers,
David


----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: David Baddeley <[hidden email]>
Sent: Sat, 17 April, 2010 5:32:45 AM
Subject: Re: [SciPy-User] line/edge detection and output of coordinates.

David,

Can you give any guidance (or point me to documentation) on how to specify parameters for scipy.ndimage.gaussian_filter to remove the noise in my image? I don't have enough background in this area for the docstring to be useful.

thanks,

- dharhas


>>> David Baddeley <[hidden email]> 4/15/2010 3:21 PM >>>
Hi Dharhas,

most traditional edge detection algorithms use some kind of gradient based filter - your image has two features which suggest that this approach might not be ideal. First it is noisy (which means that any gradient image will be even noisier) and second, you actually have relatively good contrast as to what is part of the object and what is not, and might not need gradient based edge detection per se. I'd take the following approach:

- low pass filter with scipy.ndimage.gaussian_filter to reduce the noise
- threshold the image to get a binary mask of your band (eg mask = image > threshold)
- potentially use a morphological operation such as binary closing (scipy.ndimage.binary_closing) or hole filling to tidy up the mask

from this mask you have a number of options to get the edges:
A - do a binary dilation and then subtract the original mask - this should give you a mask with all the edge pixels (both top and bottom). Applying ndimage.label to this mask might allow you to extract masks of both edges separately.

B - as you want to find the top and bottom edges (rather than edges which can fold back on themselves), you could take the difference between the mask and a copy of the mask which has been shifted vertically by one pixel (eg mask[:-1,:] - mask[1:,:]). This should give you an image in which the top edge pixels have a value of 1, the bottom edge has a value of -1, and all other pixels are zero.

once you have an image in which the pixels of each edge have different values, you can find the coordinates using numpy.where.

cheers,
David



----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: [hidden email]
Sent: Fri, 16 April, 2010 6:54:58 AM
Subject: [SciPy-User] line/edge detection and output of coordinates.

Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas


     
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user 




_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

test1_small.jpg (16K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: line/edge detection and output of coordinates.

David Baddeley
In reply to this post by David Baddeley
Hi Dharhas,

it's a bit hard to tell without playing with the data, but I suspect that the best point to get rid of the noise would be in the thresholded image, before you take the difference between the two shifted copies. I suspect that a ndimage.binary_fill_holes, potentially preceeded by a binary_closing might remove some of the clutter you're getting off the lower edge of the band. I'd then try running ndimage.label on the resulting image, which should give you connected regions. You could then throw away all the connected regions smaller than a certain cuttoff using logic such as:

#label connected regions
labels, numObjects = scipy.ndimage.label(thresholded_image)

#find the number of pixels in each object (alternatively use ndimage.measure)
nPixels, bins = scipy.histogram(labels, scipy.arange(numObjects) + 1)

newMask = scipy.zeros(labels.shape)

#loop over objects
for i in range(numObjects):
    #and add back into mask if larger than the cutoff
    if nPixels[i] > sizeCutoff:
        newMask += (labels == (i+1))

hope this helps,
David


----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: David Baddeley <[hidden email]>
Sent: Wed, 21 April, 2010 4:25:30 AM
Subject: Re: [SciPy-User] line/edge detection and output of coordinates.

David & Others,

Using the method similarto 'B' outlined by David below, I managed to get to an array of x,y coordinates that define the edge. I've attached the image to this email. As you can see,  I still have noise that needs to be removed. Any suggestions on how to do this?

thanks,

- dharhas




>>> David Baddeley <[hidden email]> 4/19/2010 5:51 AM >>>
Hi Dharhas,

I think the best approach would probably be trial and error - I'd start with a sigma of around 2-3 (all the other parameters can be left as their defaults ) and either decrease of increase it depending on whether you're blurring too much, or not enough. You might also want to take a look at ndimage.median_filter (again I'd experiment, this time with the size parameter), as there is a chance that this will let you smooth more without loosing edge definition.

cheers,
David


----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: David Baddeley <[hidden email]>
Sent: Sat, 17 April, 2010 5:32:45 AM
Subject: Re: [SciPy-User] line/edge detection and output of coordinates.

David,

Can you give any guidance (or point me to documentation) on how to specify parameters for scipy.ndimage.gaussian_filter to remove the noise in my image? I don't have enough background in this area for the docstring to be useful.

thanks,

- dharhas


>>> David Baddeley <[hidden email]> 4/15/2010 3:21 PM >>>
Hi Dharhas,

most traditional edge detection algorithms use some kind of gradient based filter - your image has two features which suggest that this approach might not be ideal. First it is noisy (which means that any gradient image will be even noisier) and second, you actually have relatively good contrast as to what is part of the object and what is not, and might not need gradient based edge detection per se. I'd take the following approach:

- low pass filter with scipy.ndimage.gaussian_filter to reduce the noise
- threshold the image to get a binary mask of your band (eg mask = image > threshold)
- potentially use a morphological operation such as binary closing (scipy.ndimage.binary_closing) or hole filling to tidy up the mask

from this mask you have a number of options to get the edges:
A - do a binary dilation and then subtract the original mask - this should give you a mask with all the edge pixels (both top and bottom). Applying ndimage.label to this mask might allow you to extract masks of both edges separately.

B - as you want to find the top and bottom edges (rather than edges which can fold back on themselves), you could take the difference between the mask and a copy of the mask which has been shifted vertically by one pixel (eg mask[:-1,:] - mask[1:,:]). This should give you an image in which the top edge pixels have a value of 1, the bottom edge has a value of -1, and all other pixels are zero.

once you have an image in which the pixels of each edge have different values, you can find the coordinates using numpy.where.

cheers,
David



----- Original Message ----
From: Dharhas Pothina <[hidden email]>
To: [hidden email]
Sent: Fri, 16 April, 2010 6:54:58 AM
Subject: [SciPy-User] line/edge detection and output of coordinates.

Hi,

I'm trying to do some line/edge detection in python. I've done some googling and found some mailing list archives that talked about ways to do edge detection but they seem to mainly return an image with the edges highlighted. What I need is x,y coordinates of the pixels that make up the lines. I've attached an image that shows a dark band on a light background. The final output I need would be line definitions in terms of a series of x,y coordinates for the upper light to dark interface and the lower dark to light interface of the band.

Any pointers on packages to use or ways to do this are highly appreciated.

Thanks,

- dharhas


     
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user 


     
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user