read_image (Image, 'E:/Halcon数据/资源图片/12.png')
dev_set_draw ('margin')
gen_rectangle1 (ROI, 18.5127, 44.065, 256.335, 451.122)
*预处理增益
Gain:=1
*预处理补偿
Offest:=0
*测量长度 根据Roi
*测量宽度
L2Measure:=2
*平滑系数
Sigam:=1
Threshod:=20
*Select可选择,'first','last','all'
Select:='first'
*Polarity‘ 由明向暗 由暗向明
Polarity:='由明向暗'
*卡尺移动参考直线LineOfModelR1-LineOfModelC2由画的直线赋值
ROI_R1:=18.5127
ROI_C1:= 44.065
ROI_R2:= 256.335
ROI_C2:=451.122
*Orientation选择'由上向下','由下向上','由左向右','由右向左'
Orientation:='由上向下'
CrossLenth:=10
*caliperNum卡尺数量
caliperNum:=20
try
*选择的测量极性方向********************************
if (Polarity='由明向暗')
polarity:='negative'
elseif(Polarity='由暗向明')
polarity:='positive'
else
polarity:='all'
endif
************************************以上是测量极性
*计算模板直线角度,根据直线角度及测量方向测算测量矩形的方向phi*******************
if (Orientation='由上向下')
LineOfModelR1:=(ROI_R1+ROI_R2)/2
LineOfModelC1:=ROI_C1
LineOfModelR2:=(ROI_R1+ROI_R2)/2
LineOfModelC2:=ROI_C2
L1Measure:=(ROI_R2-ROI_R1)/2
MeasureR1:=LineOfModelR1
MeasureC1:=LineOfModelC1+L2Measure
offestOfRow:=(LineOfModelR2-LineOfModelR1)/(caliperNum-1)
offestOfCol:=(LineOfModelC2-LineOfModelC1-2*L2Measure)/(caliperNum-1)
Phi:=-rad(90)
endif
if (Orientation='由下向上')
LineOfModelR1:=(ROI_R1+ROI_R2)/2
LineOfModelC1:=ROI_C1
LineOfModelR2:=(ROI_R1+ROI_R2)/2
LineOfModelC2:=ROI_C2
L1Measure:=(ROI_R2-ROI_R1)/2
MeasureR1:=LineOfModelR1
MeasureC1:=LineOfModelC1+L2Measure
offestOfRow:=(LineOfModelR2-LineOfModelR1)/(caliperNum-1)
offestOfCol:=(LineOfModelC2-LineOfModelC1-2*L2Measure)/(caliperNum-1)
Phi:=rad(90)
endif
if (Orientation='由左向右')
LineOfModelR1:=ROI_R1
LineOfModelC1:=(ROI_C1+ROI_C2)/2
LineOfModelR2:=ROI_R2
LineOfModelC2:=(ROI_C1+ROI_C2)/2
L1Measure:=(ROI_C2-ROI_C1)/2
MeasureR1:=LineOfModelR1+L2Measure
MeasureC1:=LineOfModelC1
offestOfRow:=(LineOfModelR2-LineOfModelR1-2*L2Measure)/(caliperNum-1)
offestOfCol:=(LineOfModelC2-LineOfModelC1)/(caliperNum-1)
Phi:=rad(0)
endif
if (Orientation='由右向左')
LineOfModelR1:=ROI_R1
LineOfModelC1:=(ROI_C1+ROI_C2)/2
LineOfModelR2:=ROI_R2
LineOfModelC2:=(ROI_C1+ROI_C2)/2
L1Measure:=(ROI_C2-ROI_C1)/2
MeasureR1:=LineOfModelR1+L2Measure
MeasureC1:=LineOfModelC1
offestOfRow:=(LineOfModelR2-LineOfModelR1-2*L2Measure)/(caliperNum-1)
offestOfCol:=(LineOfModelC2-LineOfModelC1)/(caliperNum-1)
Phi:=-rad(180)
endif
*******************************************以上是计算phi值*******************************************
reduce_domain(Image, ROI, ImageReduced)
get_image_size(ImageReduced, Width, Height)
scale_image(ImageReduced, ImageScaled, Gain, Offest)
gen_measure_rectangle2(MeasureR1, MeasureC1, Phi, L1Measure, L2Measure,Width, Height, 'nearest_neighbor', MeasureHandle)
RowDd:=[]
ColDd:=[]
for Index := 1 to caliperNum by 1
translate_measure(MeasureHandle,MeasureR1+offestOfRow*(Index-1), MeasureC1+offestOfCol*(Index-1))
gen_rectangle2(r,MeasureR1+offestOfRow*(Index-1), MeasureC1+offestOfCol*(Index-1), Phi, L1Measure, L2Measure)
measure_pos(ImageScaled, MeasureHandle, Sigam, Threshod, polarity, Select, RowEdge, ColumnEdge, Amplitude, Distance)
gen_cross_contour_xld(Cross, RowEdge, ColumnEdge, CrossLenth, 0.785398)
RowDd:=[RowDd,RowEdge]
ColDd:=[ColDd,ColumnEdge]
endfor
result:=1
catch (Exception)
result:=-1
endtry
gen_contour_polygon_xld(Contour,RowDd,ColDd)
close_measure(MeasureHandle)