macro 'Center of Mass'; { Center of Mass macro. Will calculate center of mass for an entire image or an ROI. No attempt is made to speed the ROI center of mass as opposed to the entire image. The pixel values are calibrated if a calibration value has been set. Speed is slow!!! You were warned. v2.0 6/20/94 Doug Morris - UI-UIUC Biomedical Magnetic Resonance Lab dmorris@bmrl.med.uiuc.edu } var r,i,j,xsize,ysize,numx,numy,denomx,denomy,top,top1,left,left1,width,height:integer; count,ppv,min,max,year,month,day,hour,minute,second,dayofweek:integer; right,bottom:integer; x,y,time,time1:real; begin GetTime(year,month,day,hour,minute,second,dayofweek); time1 := 3600*hour + 60*minute + second; { Start timer} numx := 0; numy := 0; denomx := 0; denomy := 0; RequiresVersion(1.51); GetPicSize(xsize,ysize); GetRoi(left,top,width,height); SetCursor('watch'); if width=0 then begin { If no ROI assume COM measurement of Image } for i:=0 to xsize-1 do begin for j:=0 to ysize-1 do begin r := cvalue(GetPixel(i,j)); numx := numx + (i * r); denomx := denomx + r; numy := numy + (j * r); denomy := denomy + r; end; if i mod 10=0 then ShowMessage('Evaluating Line',i,'/',ysize:1); end; end; if width<>0 then begin {If ROI exists copy to empty image and calculate COM} Copy; SetNewSize(xsize,ysize); MakeNewWindow('ROI'); Paste; Clear; GetROI(left1,top1,width,height); MoveROI(left-left1,top-top1); Paste; KillRoi; SelectWindow('ROI'); for i:=0 to xsize-1 do begin for j:=0 to ysize-1 do begin r := cvalue(GetPixel(i,j)); numx := numx + (i * r); denomx := denomx + r; numy := numy + (j * r); denomy := denomy + r; end; if i mod 10=0 then ShowMessage('Evaluating Line',i,'/',ysize:1); end; Dispose; end; SetCursor('arrow'); if denomx = 0 then begin {If no data then don't divide by zero} PutMessage('No Intensity Values between 1 and 254.'); Exit; end; x := numx/denomx; y := numy/denomy; GetTime(year,month,day,hour,minute,second,dayofweek); time := (hour*3600 + 60*minute + second) - time1; { Stop timer.} SetCursor('arrow'); { Name the output window based on input window.} If width<>0 then NewTextWindow('ROI Center of Mass',260,100); If width=0 then NewTextWindow('Image Center of Mass',260,100); Writeln('Center of Mass=(',x:10:3,',',y:10:3,')'); if width<>0 then begin bottom := top + height; right := left + width; Writeln('Extrema of ROI: : top: ',top:6:0,' bottom: ',bottom:6:0); Writeln( ' left: ',left:6:0,' right: ',right:6:0); end; Writeln('Elapsed time: ',time:5:0,' s'); ShowMessage(' '); end;