Re: Watersheds and Label_Region for 1d [message #20544] |
Fri, 07 July 2000 00:00 |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
rshill@my-deja.com wrote:
>
>
> 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)]
>
>
Smart and Cheap! Thanks!
Ben
--
Ben Tupper
Bigelow Laboratory for Ocean Science
tupper@seadas.bigelow.org
pemaquidriver@tidewater.net
|
|
|
Re: Watersheds and Label_Region for 1d [message #20545 is a reply to message #20544] |
Fri, 07 July 2000 00:00  |
rshill
Messages: 2 Registered: May 2000
|
Junior Member |
|
|
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.
|
|
|
Re: Watersheds and Label_Region for 1d [message #20547 is a reply to message #20544] |
Fri, 07 July 2000 00:00  |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
Shame on me!
I posted unfinished code. Please find the corrected version below.
Ben
----snip
;+
; NAME: LABEL_VECTOR
;
; PURPOSE: This function returns a labeled (blob-colored) vector
; where each unique region bears a unique region number.
; This function is analogous to the built in LABEL_REGION function for IDL.
;
; CALLING SEQUENCE:
; Result = LABEL_VECTOR(Vector, [BackGround])
;
; ARGUMENTS:
; Vector Set this value to a numeric vector (Byte,Integer, etc.)
; BackGround Set this argument equal to the background value
; of the vector... that is, the value that separates the blobs.
; If not provided, the default value of zero is used.
;
; KEYWORDS:
;
; MAXLABEL Set this keyword to a named variable to retrieve the
; maximum label value. (Saves a MAX(Result) later.)
;
; EXAMPLE:
; Generate a dummy vector... then plot it with the colorings
; superimposed.
; IDL> v = indgen(20)
; IDL> v = rebin(shift(v*5,5), 80,/sample)
; IDL> f = Label_Vector(V)
; IDL> plot, v
; IDL> TEK_COLOR
; IDL> plots, indgen(80), v, color=f, /data, psym = 6
;
; MODIFICATION HISTORY:
; Written 6JULY2000, Ben Tupper
; Bigelow Laboratoryu for Ocean Science
; tupper@seadas.bigelow.org
; pemaquidriver@tidewater.net
;
; 7JUL2000 oops!, Actually implement Background argument! BT
;-
;-------
; Label_Vector
;-------
FUNCTION Label_Vector, Vec, BackGround ,MaxLabel = MaxLabel
On_Error, 2
Sz = Size(Vec)
If Sz[0] NE 1 Then Begin
Message,'First argument must be a 1d vector'
Return, -1
EndIf
If N_Params() EQ 2 then Background = Background[0] Else Background = 0
LabeledVec = Fix(Vec NE BackGround)
N = Sz[3]
MaxLabel = 0
A = Where(Labeledvec GT 0, Count)
If Count GT 0 Then Begin
MaxLabel = 1
For i = A[0] , N - 1L Do Begin
If LabeledVec[i] GT 0 Then Begin
LabeledVec[i] = MaxLabel
EndIf Else Begin
If i NE N-1L Then $
If LabeledVec[i] NE LabeledVec[i+1L] Then $
MaxLabel = MaxLabel +1
EndElse
EndFor ; i loop
EndIf ; Count GT 0
Return, LabeledVec
END
----snip
--
Ben Tupper
Bigelow Laboratory for Ocean Science
tupper@seadas.bigelow.org
pemaquidriver@tidewater.net
|
|
|
Re: Watersheds and Label_Region for 1d [message #20552 is a reply to message #20544] |
Thu, 06 July 2000 00:00  |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
Hello,
I have made some progress. Well, I 've made some progress toward the
Label_Region function for vectors.
I tried a number of tricks including using the REVERSE_INDICES keyword for
HISTOGRAM.
In the end, I settled for the tried and true brute-force-and-ignorance
approach. It's not dainty but seems
to work with all of the mocked-up data I could think of.
;---------SNIP-------
;+
; NAME: LABEL_VECTOR
;
; PURPOSE: This function returns a labeled (blob-colored) vector
; where each unique region bears a unique region number.
; This function is analogous to the built in LABEL_REGION function for
IDL.
;
; CALLING SEQUENCE:
; Result = LABEL_VECTOR(Vector, [BackGround])
;
; ARGUMENTS:
; Vector Set this value to a numeric vector (Byte,Integer, etc.)
; BackGround Set this argument equal to the background value
; of the vector... that is, the value that separates the blobs.
; If not provided, the default value of zero is used.
;
; KEYWORDS:
;
; MAXLABEL Set this keyword to a named variable to retrieve the
; maximum label value. (Saves a MAX(Result) later.)
;
; EXAMPLE:
; Generate a dummy vector... then plot it with the colorings
; superimposed.
; IDL> v = indgen(20)
; IDL> v = rebin(shift(v*5,5), 80,/sample)
; IDL> f = Label_Vector(V)
; IDL> plot, v
; IDL> TEK_COLOR
; IDL> plots, indgen(80), v, color=f, /data, psym = 6
;
; MODIFICATION HISTORY:
; Written 6JULY2000, Ben Tupper
; Bigelow Laboratoryu for Ocean Science
; tupper@seadas.bigelow.org
; pemaquidriver@tidewater.net
;-
;-------
; Label_Vector
;-------
FUNCTION Label_Vector, Vec, BackGround ,MaxLabel = MaxLabel
LabeledVec = Fix(Vec GT 0)
N = N_elements(Vec)
MaxLabel = 0
If N_Params() EQ 2 then Background = Background[0] Else Background = 0
A = Where(Labeledvec GT 0, Count)
If Count GT 0 Then Begin
MaxLabel = 1
For i = A[0] , N - 1L Do Begin
If LabeledVec[i] GT 0 Then Begin
LabeledVec[i] = MaxLabel
EndIf Else Begin
If i NE N-1L Then $
If LabeledVec[i] NE LabeledVec[i+1L] Then $
MaxLabel = MaxLabel +1
EndElse
EndFor ; i loop
EndIf ; Count GT 0
Return, LabeledVec
END
;--------SNIP-------
--
Ben Tupper
Bigelow Laboratory for Ocean Science
tupper@seadas.bigelow.org
pemaquidriver@tidewater.net
|
|
|