comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: checking for connectedness of a given set of pixels
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: checking for connectedness of a given set of pixels [message #73311] Wed, 03 November 2010 06:35 Go to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Guillermo writes:

> At the end of the day, it might make more sense to stick to
> Label_Region :(

That would be my conclusion, too. :-)

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: checking for connectedness of a given set of pixels [message #73312 is a reply to message #73311] Wed, 03 November 2010 06:32 Go to previous messageGo to next message
guillermo.castilla.ca is currently offline  guillermo.castilla.ca
Messages: 27
Registered: September 2008
Junior Member
> You might try some variation of the chain-code algorithm then

I see what you mean David. So you pick from the input set the pixel
with lowest 1D index, which necessarily lies in the boundary of (one
of) the region(s), find the boundary of that region with
Find_Boundary, use that boundary as input to POLYFILLV as to get the
addresses of the pixels within it, and if there are some pixels in the
input set that do not belong to boundary+interior, then you know the
input set is not connected. However, if no pixel is outside but the
boundary+interior contains more pixels than the input set, then you
have to make additional checks to make sure that the holes are not
such that some group from the input set is disconnected from the rest.
At the end of the day, it might make more sense to stick to
Label_Region :( Thanks anyways for the advice.

Cheers

Guillermo
Re: checking for connectedness of a given set of pixels [message #73332 is a reply to message #73312] Tue, 02 November 2010 15:14 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Guillermo writes:

> Libreme Dios! Both functions are great, but I just need to know
> whether or not the pixels are connected. That is, as soon as I find
> one of the little guys who doesn't go hand in hand with the rest,
> that's good enough for me. So I thought there should be a way of
> getting this info without having to call the big shots ...

Bueno! You might try some variation of the chain-code algorithm
then. If you get to the end of a chain and there are no roads
leading anywhere but back the way you came, you are probably
at the end of the road.

This is not the easiest of algorithms to code, but you
can find one version (written for my purposes) in
find_boundary.pro. I think I found the algorithm in the
Russ image processing book.

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: checking for connectedness of a given set of pixels [message #73333 is a reply to message #73332] Tue, 02 November 2010 15:06 Go to previous messageGo to next message
guillermo.castilla.ca is currently offline  guillermo.castilla.ca
Messages: 27
Registered: September 2008
Junior Member
On Nov 2, 5:37 pm, David Fanning <n...@dfanning.com> wrote:
> Why are you hoping to re-write these two useful routines?

Libreme Dios! Both functions are great, but I just need to know
whether or not the pixels are connected. That is, as soon as I find
one of the little guys who doesn't go hand in hand with the rest,
that's good enough for me. So I thought there should be a way of
getting this info without having to call the big shots ...

Cheers

Guillermo
Re: checking for connectedness of a given set of pixels [message #73338 is a reply to message #73333] Tue, 02 November 2010 13:37 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Guillermo writes:

> Hi there, I would need to create a function that tells me whether a
> set of pixels (given by their 2D coordinates) forms a single blob or
> consists of two or more disjoint groups, in either a 4- or 8-
> neighborhood. Can anyone suggest a computationally cheap method for
> doing that (i.e., one that doesn't rely on label_region or search_2d)?

Why are you hoping to re-write these two useful routines?

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: checking for connectedness of a given set of pixels [message #73430 is a reply to message #73311] Mon, 08 November 2010 11:22 Go to previous messageGo to next message
James[2] is currently offline  James[2]
Messages: 44
Registered: November 2009
Member
LABEL_REGION's speed relies on a fast implementation of the union and
find operations on disjoint set data structures (http://
en.wikipedia.org/wiki/Disjoint-set_data_structure). I'm guessing that
the IDL programmers have implemented this in C for LABEL_REGION,
probably with a higher speed than you'd be able to get with anything
written in IDL. If they did it "right" (with path compression) then
it's an optimally fast algorithm for this problem (see Wikipedia
article).

One problem is that LABEL_REGION runs over a whole array even if it is
mostly empty (sparse). This is why LABEL_REGION might be slow for
your task - it could be doing a lot of unnecessary extra work. Since
your data is already in 2D coordinates, I'd take the max/min values
and use a tightly-fitted array. Supposing your coordinates are in a 2-
by-N array POINTS:

function ISCONNECTED(points, _extra=ex)
mins = min(points, dimension=2, max=maxs)
shift = mins # replicate(1, n_elements(points)/2)
arr = bytarr(maxs-mins+1)
arr[points - shift] = 1
return, max(label_region(arr, _extra=ex)) le 1
end

the _extra keywords allow you to pass ALL_NEIGHBORS along to choose
between 4- and 8-connectivity. Of course, this function adds a lot of
extra baggage, so it's only going to be faster if your regions are
significantly smaller than the whole array.
Re: checking for connectedness of a given set of pixels [message #73514 is a reply to message #73430] Sun, 14 November 2010 13:56 Go to previous messageGo to next message
guillermo.castilla.ca is currently offline  guillermo.castilla.ca
Messages: 27
Registered: September 2008
Junior Member
On Nov 8, 3:22 pm, James <donje...@gmail.com> wrote:

> One problem is that LABEL_REGION runs over a whole array even if it is
> mostly empty (sparse).  This is why LABEL_REGION might be slow for
> your task - it could be doing a lot of unnecessary extra work.  

Thanks a lot James, very insightful comments, and very nice piece of
code :). After giving it some more thought, I found out that there is
an IDL function called REGI0N_GROW that may be faster than
LABEL_REGION for this purpose when the minimum bounding box for the
input set of pixels is large. I have modified your function to include
the former as an alternative method (below). I'll do some tests and
report back (hopefully within this year :).

function ISCONNECTED, points, method=method, _extra=ex
mins = min(points, max=maxs, dimension=2)-1
dims=maxs-mins+3
npts= n_elements(points)/2
indices= points - mins # replicate(1, npts)
indices= indices[0,*] + dims[0]*indices[1,*]
arr = bytarr(dims)
arr[indices] = 1B
if ~keyword_set(method) then $
return, max(label_region(arr, _extra=ex)) le 1 $
else return, n_elements(region_grow( $
arr, indices[0], thresh=[1B,1B], _extra=ex)) eq npts
end
Re: checking for connectedness of a given set of pixels [message #74182 is a reply to message #73514] Mon, 03 January 2011 13:50 Go to previous message
guillermo.castilla.ca is currently offline  guillermo.castilla.ca
Messages: 27
Registered: September 2008
Junior Member
On Nov 14 2010, 5:56 pm, Guillermo
<guillermo.castilla.castell...@gmail.com> wrote:

> ...REGI0N_GROW that may be faster than
> LABEL_REGION for this purpose when the minimum bounding box for the
> input set of pixels is large. I have modified your function to include
> the former as an alternative method (below). I'll do some tests and
> report back (hopefully within this year :).

Just for the record, today I made a test with a 50 megapixel labeled
image derived from the rasterization of a polygon vector layer
containing 185 elongated features (rivers), of which 65 ended up
consisting of disconnected groups of pixels. Checking for
connectedness of the pixels within each feature took almost 3 times
more using the REGI0N_GROW method than using LABEL_REGION (the latter
taking in average 30 millisecs per feature). I quickly looked into the
REGI0N_GROW code and noticed that it is based on LABEL_REGION itself,
so no wonder. Repeated the test with SEARCH2D instead of REGI0N_GROW
and although it was two times faster than the latter, it was still
slower than LABEL_REGION. So it looks like LABEL_REGION, even if it
does a lot of unnecessary extra work for this task, is the method of
choice for checking for connectedness of a set of pixels.

Happy new year!

Guillermo
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Random NetCDF I/O error
Next Topic: Re: Random NetCDF I/O error

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:10:13 PDT 2025

Total time taken to generate the page: 0.00563 seconds