# Re: Watersheds and Label_Region for 1d

```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
;   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