[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Watersheds and Label_Region for 1d



My apologies to anyone who is seeing this message twice.

On Thu, 6 Jul 2000, Ben Tupper wrote:
> In order to do this I need to mimic the LABEL_REGION function on a
> vector.   I can think of a number of
> ways of doing this... but they seem computationally expensive (lots of
> WHERE's and connectedness checking.)

One cheap trick is to replicate the vector three times and do the
label_region on the resulting 2-D array:

    vec = [0,0,0,2,2,2,0,1,5,1,2,1,0,0,0,3,99,3,3]
    arr = [0,vec,0] # [1,1,1]   ; Avoid edge effects.
    lab2d = label_region(arr)
    lab1d = (lab2d[*,1])[1:n_elements(vec)]

Another thing you can do if you just want the endpoints of the runs is
this:

    nv = n_elements(vec)
    flag = [0, vec NE 0, 0]     ; Avoid endpoint effects.
    diff = flag[1:nv+1] - flag[0:nv]
    run_start = where(diff EQ 1)
    run_end = where(diff EQ -1) - 1

Footnote:  Be careful with the type of the flag array, which
later determines the type of the diff array.  Comparisons may depend
on variable type, because of signed vs. unsigned.  In other words,
-1b (byte) is not equal to -1 (integer) because -1b is actually 255.

---Bob H.



Sent via Deja.com http://www.deja.com/
Before you buy.