PRO objekt2,ima,tab ;A more siple and efficient version of OBJEKT1, ;based on the IDL routine LABEL_REGION. ; ;This program labels objects in a segmented image. ;(Segmentation can be performed using e.g. UDISOL.PRO.) ;Objects are defined as subsets of non-zero pixels which ;are neighboring side-by-side. Single-pixel objects are ;considered as a noise and excluded, i.e. their object ;number is set to zero. Pixels forming an object are ;labelled by the same object number, i.e. all pixels of ;the n-th object are labelled by object number 'n'. ; ;CALLING SEQUENCE: objekt1,image,table ; ;INPUT: 2-D array IMAGE - a segmented image, where the ; potential object pixels have a non-zero value, ; other (background) pixels have a zero value. ;OUTPUT: 2-D array TABLE - a two-column table, where ; the 0-th column contains object numbers and ; the 1-st column contains subscripts of non-zero ; pixels in 1-D representation. ;NOTE: Since the subscripts are stored as longwords, ; there is practically no limitation concerning ; the image size. ; ; 24 February 2000, Michal. ; on_error,1 s=size(ima) if s(0) ne 2 then message,'*** Input must be a 2-D array' h=where(ima,n) ;non-zero pixels if n le 0 then message,'*** Input image is empty' if n eq s(4) then message,'*** Input image is not segmented' h=LABEL_REGION(ima) h=HISTOGRAM(h,reverse_indices=r) ;See IDL help for this ; tricky keyword. n=n_elements(h) ;Number of objects. Object no. 0 is the ; background and is not considered in ; the following process. sub=r(r(1):r(n)-1) ;Reverse-indices trick; we start from ; object no. 1. tab=lonarr(2,n_elements(sub)) ;Output array (LONG) tab(1,*)=sub ;List of subscripts cnt=0 ;Counter to know the position in TAB lab=0 ;Counter giving the new object labels ; after skipping single-pixel objects for i=1,n-1 do begin if h(i) gt 1 then begin lab=lab+1 ;Incrementing the object label tab(0,cnt:cnt+h(i)-1)=REPLICATE(lab,h(i)) endif else tab(0,cnt:cnt+h(i)-1)=0 ;Here the single-pixel objects are excluded, ; i.e., their object number is set to zero. cnt=cnt+h(i) ;Incrementing the position counter in TAB. endfor print,' Number of objects:',lab END