主程序:
read_image (Image11, '11')*畫仿射矩形
dev_set_color ('green')
draw_rectangle2 (3600, Row, Column, Phi, Length1, Length2)*生成仿射矩形xld
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
*找頂點工具(基于卡尺工具)
peak (Image11, Row, Column, Phi, Length1, Length2, 2, 0.41, 20, 'negative', 'first', EdgesY, EdgesX, ResultRow, ResultColumn)*顯示圖像
dev_display (Image11)
*顯示仿射矩形
dev_display (Rectangle)
*顯示所有邊緣點
if(|EdgesY|>0)dev_set_color ('yellow')gen_cross_contour_xld (Cross, EdgesY, EdgesX, 0.5, 0)*顯示頂點dev_set_color ('green')gen_cross_contour_xld (Cross,ResultRow, ResultColumn, 3, 0)*顯示頂點坐標disp_message (3600, 'x:='+ResultColumn+'\r\ny:'+ResultRow, 'image', ResultRow, ResultColumn, 'blue', 'false')
endif
peak函數:
*初始化
ResultRow:=-9999
ResultCol:=-9999
EdgeColumns := []
EdgeRows := []*仿射矩形Length2所在直線作為rake工具的ROI
ROILineRow1:=0
ROILineCol1:=0
ROILineRow2:=0
ROILineCol2:=0*仿射矩形方向所直線的邊做基準線
StdLineRow1:=0
StdLineCol1:=0
StdLineRow2:=0
StdLineCol2:=0
*判斷仿射矩形是否有效
if(Length1<=0 or Length2<=0)return()
endif*計算仿射矩形角度的正弦值、余弦值
tuple_cos (Phi, Cos)
tuple_sin (Phi, Sin)*矩形第一個端點坐標
Col1 := 1.0*( Coloumn-Length1*Cos - Length2*Sin)
Row1 := 1.0*(Row-(-Length1*Sin + Length2*Cos))*矩形第二個端點坐標
Col2 := 1.0*(Coloumn+Length1*Cos - Length2*Sin)
Row2 := 1.0*(Row-(Length1*Sin + Length2*Cos))*矩形第三個端點坐標
Col3 := 1.0*(Coloumn+Length1*Cos + Length2*Sin)
Row3 := 1.0*(Row-(Length1*Sin - Length2*Cos))*矩形第四個端點坐標
Col4 := 1.0*(Coloumn-Length1*Cos + Length2*Sin)
Row4 := 1.0*(Row-(-Length1*Sin - Length2*Cos))*仿射矩形方向所直線的邊做基準線
StdLineRow1:=Row2
StdLineCol1:=Col2
StdLineRow2:=Row3
StdLineCol2:=Col3*仿射矩形Length2所在直線作為rake工具的ROI
ROILineRow1 := (Row1+Row2)*0.5
ROILineCol1:= (Col1+Col2)*0.5
ROILineRow2 := (Row3+Row4)*0.5
ROILineCol2:= (Col3+Col4)*0.5
* gen_rectangle2_contour_xld (Rectangle, Row, Coloumn, Phi, Length1, Length2)
rake (Image, Regions1, 1.0*Length2*2, Length1*2, DetectWidth, Sigma, Threshold, Transition, Select, ROILineRow1, ROILineCol1, ROILineRow2, ROILineCol2, ResultRows, ResultColumns)*求所有邊緣點到基準線的距離,保存最大距離及其對應的邊緣點坐標,作為頂點
Max:=0
if(|ResultColumns|>0)EdgeRows := ResultRowsEdgeColumns := ResultColumnsfor i := 0 to |ResultColumns|-1 by 1distance_pl (ResultRows[i], ResultColumns[i], StdLineRow1, StdLineCol1, StdLineRow2, StdLineCol2, Distance1)if(Max<Distance1)Max:=Distance1ResultRow:=ResultRows[i]ResultColumn:=ResultColumns[i]endifendfor
endifreturn ( )
rake函數:
*獲取圖像尺寸
get_image_size(Image,Width,Height)
*產生一個空顯示對象,用于顯示
gen_empty_obj(Regions)
*初始化邊緣坐標數組
ResultRow := []
ResultColumn := []
*產生直線xld
gen_contour_polygon_xld (RegionLines, [Row1,Row2], [Column1,Column2])
*存儲到顯示對象
concat_obj(Regions, RegionLines, Regions)
*計算直線與x軸的夾角,逆時針方向為正向。
angle_lx (Row1, Column1, Row2, Column2, ATan)*邊緣檢測方向垂直于檢測直線:直線方向正向旋轉90°為邊緣檢測方向
ATan:=ATan+rad(90)*根據檢測直線按順序產生測量區域矩形,并存儲到顯示對象
for i:=1 to Elements by 1
* RowC := Row1+(((Row2-Row1)*i)/(Elements+1))
* ColC := Column1+(Column2-Column1)*i/(Elements+1)
* if (RowC>Height-1 or RowC<0 or ColC>Width-1 or ColC<0)
* continue
* endif*如果只有一個測量矩形,作為卡尺工具,寬度為檢測直線的長度if(Elements=1)RowC := 1.0*(Row1+Row2)*0.5ColC := 1.0*(Column1+Column2)*0.5*判斷是否超出圖像,超出不檢測邊緣if (RowC>Height-1 or RowC<0 or ColC>Width-1 or ColC<0)continue endifdistance_pp(Row1, Column1, Row2, Column2, Distance)DetectWidth:=Distancegen_rectangle2_contour_xld(Rectangle, RowC, ColC,ATan, 1.0*DetectHeight/2,1.0*Distance/2)else*如果有多個測量矩形,產生該測量矩形xldRowC := 1.0*(Row1+(((Row2-Row1)*(i-1))/(Elements-1)))ColC := 1.0*(Column1+(Column2-Column1)*(i-1)/(Elements-1))*判斷是否超出圖像,超出不檢測邊緣if (RowC>Height-1 or RowC<0 or ColC>Width-1 or ColC<0)continue endifgen_rectangle2_contour_xld(Rectangle, RowC, ColC, ATan, 1.0*DetectHeight/2,1.0*DetectWidth/2)endif*把測量矩形xld存儲到顯示對象concat_obj (Regions, Rectangle, Regions)if(i=1)*在第一個測量矩形繪制一個箭頭xld,用于只是邊緣檢測方向RowL2:=RowC+DetectHeight/2*sin(-ATan)RowL1:=RowC-DetectHeight/2*sin(-ATan)ColL2:=ColC+DetectHeight/2*cos(-ATan)ColL1:=ColC-DetectHeight/2*cos(-ATan)gen_arrow_contour_xld (Arrow1, RowL1, ColL1, RowL2, ColL2, 25, 25)*把xld存儲到顯示對象concat_obj (Regions, Arrow1, Regions)endif*產生測量對象句柄gen_measure_rectangle2(RowC, ColC,ATan, DetectHeight/2,DetectWidth/2,Width, Height, 'nearest_neighbor', MsrHandle_Measure)*設置極性if (Transition = 'negative')Transition := 'negative'else if (Transition = 'positive')Transition := 'positive'else Transition := 'all'endifendif*設置邊緣位置。最強點是從所有邊緣中選擇幅度絕對值最大點,需要設置為'all'if (Select = 'first')Select := 'first'else if (Select = 'last')Select := 'last'else Select := 'all'endifendif*檢測邊緣measure_pos (Image, MsrHandle_Measure, Sigma, Threshold, Transition, Select, RowEdge, ColEdge, Amplitude, Distance)*清除測量對象句柄close_measure(MsrHandle_Measure)*臨時變量初始化*tRow,tCol保存找到指定邊緣的坐標tRow := 0tCol := 0*t保存邊緣的幅度絕對值t:= 0*找到的邊緣必須至少為1個tuple_length(RowEdge, Number)if(Number<1)continueendif*有多個邊緣時,選擇幅度絕對值最大的邊緣for j:=0 to Number-1 by 1if(abs(Amplitude[j])>t)tRow := RowEdge[j]tCol := ColEdge[j]t := abs(Amplitude[j])endifendfor*把找到的邊緣保存在輸出數組if(t>0)ResultRow:=[ResultRow,tRow]ResultColumn:=[ResultColumn,tCol]endif
endforreturn ()