PROGRAM TRACE C C Version 1.2.5, August 1, 1997 by M. Knappertsbusch, C Natural History Museum Basel, Augustinergasse 2, C 4001-Basel, Switzerland. Email: Knappertsbus@ubaclu.unibas.ch C C Program to trace the outline of a digitized light image. C The image is a grey-level picture with YPIX lines and C XPIX pixels per line. C C Output are cartesian X,Y coordinates of the outline, C arranged in counter-clockwise direction. C The program includes an optional conversion of pixels in micrometers. C C The program calls for the external file CALIBRATION C INTEGER I,J,N,N0,X,Y,THRES,MAG INTEGER XPIX,YPIX,YHALF INTEGER GREY(1:8), K(1:8), A(1:8,1:8) DOUBLE PRECISION XPREC,YPREC,XRES,YRES DOUBLE PRECISION AX,BX,AY,BY CHARACTER*1 CHAR(1:8),ANSWER CHARACTER*20 INPUT CHARACTER*32 HEADER C C In A(I,J) the local numbering scheme of circum-points around a central C point in the 3x3 pixel search mask is stored: C DATA ((A(I,J),I=1,8),J=1,8)/1, * 2,3,4,5,6,7,8, * 2,3,4,5,6,7,8,1, * 3,4,5,6,7,8,1,2, * 4,5,6,7,8,1,2,3, * 5,6,7,8,1,2,3,4, * 6,7,8,1,2,3,4,5, * 7,8,1,2,3,4,5,6, * 8,1,2,3,4,5,6,7/ C C Experimental setup and calibration: C C A Leica MZ6 Stereo-Binocular eqipped with a CF 11/2 colour C CCD-camera 795(H)x596(V) pixels from Kappa. C The magnification to be entered is calculated as the product C of the objective lens inserted (1x or 2x) times the number indicated C at the zoom-knob. Example: if the objective lens is 1x and the C number at the zoom-knob indicates 4.0 then the magnification to be C entered is 4.0. C C Specifications on computer side: C Frame grabber: built in Quicktime framegrabber. C Video control: Grey-scale, full-size, NTSC format C Digital image: resolution: 72 pixel per inch, standard image size: 640x480 pixel C = Quicktime format). C Pixel aspect ratio on computer screen: 1 C C C The conversion of pixel coordinates into µm coordinates C depends on the format of the image and the magnification of the C microscope. To determine the magnification factor from the real C object to its digitized image, the system must be calibrated. C In the present configuration the system is calibrated C for 640x480 pixel images. These specifications are written in the C external and editable text file CALIBRATION. If other formats are C used the parameters must be changed in file CALIBRATION, and the C system must be calibrated first. C C Reading the size of the image from CALIBRATION: C OPEN(14,FILE='CALIBRATION',STATUS='OLD') C READ(14,*) XPIX,YPIX WRITE(9,*) 'Size of image: ',XPIX,'x',YPIX,' pixels' C IF ( (XPIX.NE.640).OR.(YPIX.NE.480) ) THEN WRITE(9,*) 'Please calibrate conversion of pixels into µm !' WRITE(9,*) ' . . .Output of outline in pixels only. . .' ANSWER='1' GOTO 7 END IF C C C Entering the microscopic magnification used: C WRITE(9,*) ' . . .Enter Magnification used. . .' WRITE(9,*) 'Note: MAG=Objective lens x zoom factor' READ(9,*) MAG C C Conversion of pixels to µm: C In the following the factors are calculated to convert pixels into µm. C This conversion is dependent on the microscopic magnification of the C microscope (object to CCD-camera), and on a constant factor between C the camera to the computer hardware. Depending on the camera-type these C factors can be different in X and Y direction. Therefore, the conversion C is expressed by two separate linear relationships, one for the X-direction C and one for the Y-direction: C C XPREC=AX*MAG+BX (X-direction) (in pixel/µm) C YPREC=AY*MAG+BY (Y-direction) (in pixel/µm) C C The necessary resolutions XRES and YRES to convert pixels into µm C are then calculated as: C C XRES=1/XPREC (X-direction) (in µm/pixel) C YRES=1/YPREC (Y-direction) (in µm/pixel) C C The coefficients AX,BX and AY and BY are written in the external C text file "CALIBRATION". This allows to easily adapt the program C to different experimental setups. C C Example: C For an image of the size of 640x480 pixels, and the present setup C the magnification dependent linear relationships are: C C XPREC = 0.11829 * MAG + 0.00201 (in pixel/µm) C YPREC = 0.11792 * MAG + 0.00221 (in pixel/µm) C C (XPREC and XRES are in horizontal, YPREC and YRES are in vertical C direction). C C The necessary resolutions XRES and YRES are then calculated as: C C XRES = 1/XPREC (in µm/pixel) C YRES = 1/YPREC (in µm/pixel) C C C Reading the comment files, grey-level condition, and the C calibration coefficients from file CALIBRATION: C READ(14,18) HEADER READ(14,18) HEADER READ(14,18) HEADER C C THRES is the Grey-level at which the extraction is performed C A value of THRES=49 gives reasonable results C READ(14,*) THRES WRITE(9,*) 'Grey-level for extraction: „ ',THRES READ(14,18) HEADER READ(14,18) HEADER READ(14,18) HEADER C C Reading calibration coefficients and closing file CALIBRATION: C READ(14,*) AX,BX,AY,BY WRITE(9,*) AX,BX,AY,BY 18 FORMAT(A32) CLOSE(14) C XPREC=AX*MAG+BX YPREC=AY*MAG+BY C XRES=1/XPREC YRES=1/YPREC C C Inquiry whether output shall be given in pixel- or µm coordinates: C WRITE(9,*) '. . .Output in Pixels (1) or Microns (2) ?. . .' READ(9,4) ANSWER 4 FORMAT(A1) C C Now reading input image file: C 7 WRITE(9,*) '. . .Enter input file name (max 20 chars). . .' READ(9,3) INPUT 3 FORMAT(A20) C WRITE(9,*) '. . .calculating. . .' OPEN(15,FILE=INPUT,ACCESS='DIRECT', * RECL=1,FORM='FORMATTED',STATUS='OLD') OPEN(16,FILE='OUTPUT',STATUS='NEW') C C Find first point of outline in the middle of the C TV-screen (this is line YHALF; its record is calculated C as YHALF=INT(YPIX/2-1)*XPIX): C YHALF=INT((YPIX/2-1)*XPIX) C DO 1, I=1,XPIX N=YHALF+I READ(15,50,REC=N) CHAR(1) GREY(1)=ICHAR(CHAR(1)) IF (GREY(1).GE.THRES) THEN N0=N GOTO 5 END IF 1 CONTINUE C 5 CALL COORD(XPIX,YPIX,N,X,Y) C WRITE(9,*) X,Y C C Output of results to file: C IF (ANSWER.EQ.'1') THEN C Output of X and Y in pixels: WRITE(16,*) X,',',Y ELSE C Output of X and Y in micrometers: WRITE(16,*) X*XRES,',',Y*YRES END IF C C Local initialization: At each point (pixel) of the outline (record number N) C the eight surrounding pixels are read, then their grey-levels C are determined, and the pixels are numbered from 1 to 8: C C C 2 3 4 C C 1 N 5 C C 8 7 6 C C READ(15,50,REC=N-1) CHAR(1) READ(15,50,REC=N-XPIX-1) CHAR(2) READ(15,50,REC=N-XPIX) CHAR(3) READ(15,50,REC=N-XPIX+1) CHAR(4) READ(15,50,REC=N+1) CHAR(5) READ(15,50,REC=N+XPIX+1) CHAR(6) READ(15,50,REC=N+XPIX) CHAR(7) READ(15,50,REC=N+XPIX-1) CHAR(8) 50 FORMAT(A1) C C Determination of the grey-levels: C DO 10, I=1,8 GREY(I)=ICHAR(CHAR(I)) 10 CONTINUE C C Assignment of the local pixel number (i.e. 1 through 8) C to the record number N for each pixel. C (i.e. in K are the record numers of the pixels 1 to 8 stored, C with K having the idices from i to 8): C K(1)=N-1 K(2)=N-XPIX-1 K(3)=N-XPIX K(4)=N-XPIX+1 K(5)=N+1 K(6)=N+XPIX+1 K(7)=N+XPIX K(8)=N+XPIX-1 C C Search algoritm for the next point of the outline: The algoritm C searches first for the local point to start with: This first C point has the grey-level „ Threshold. From this initial C local point it searches the next point, where the greylevel is C lower than Threshold. The pixel, which is previous to that C point is the next point of the outline (i.e. the next center for C the search algoritm). C C Look for first point of local-neighbour-search: C DO 20, I=1,8 IF (GREY(I).GE.THRES) THEN C C First point of search path found. Check every next surrounding point in C clockwise direction: C DO 30, J=1,8 IF (GREY(A(I,J)).LT.THRES) THEN IF (A(I,J).EQ.1) THEN N=K(8) ELSE N=K(A(I,J)-1) END IF C C Stop-Criterium: C IF (N.EQ.N0) THEN WRITE(9,*) 'Outline closed' STOP END IF C GOTO 5 END IF 30 CONTINUE END IF 20 CONTINUE C END SUBROUTINE COORD(UPIX,VPIX,M,U,V) C C Determines the cartesian coordinates U and V as a function of C the record number M representing a pixel in the image. C The image has VPIX lines and UPIX pixels per line. C C The record number starts with M=1 (representing the first C pixel in line 1). C INTEGER M,U,V INTEGER UPIX,VPIX C C This determination of U and V is valid for all pixels except those C that are at the end of each line (for example for a 640x480 pixel image pixel no. 640, 1280,...). C U=MOD(M,UPIX) V=VPIX-1-INT(M/UPIX) C RETURN END