连续图像采集*****
*Image Acquisition 01: Code generated by Image Acquisition 01
R:=[]
C:=[]
ImageFiles := []
ImageFiles[0] := ‘./MPic/pic02.png’
* Reopen the window appropriately
read_image (halconImage, ImageFiles[0])
get_image_size (halconImage, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, ‘black’, WindowHandle)
dev_display (halconImage)
dev_set_draw (‘margin’)
****测试窗口跟随被测物体对齐
for Index := 1 to 3 by 1
*连通域
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions2, ‘area’, ‘and’, 20491, 1613949)
*根据面积和矩形度 筛选
select_shape (ConnectedRegions, SelectedRegions, [‘area’,‘rectangularity’], ‘and’, [20000,0.1], [50000000,1])
select_shape (SelectedRegions, SelectedRegions1, ‘area’, ‘and’, 0, 1)
*找出面积最大区域
select_shape_std(SelectedRegions,SelectedImage,‘max_area’,30)
difference(RegionDilation,RegionErosion,RegionDifference)
*从原图里面把相交的矩形区域扣出来
reduce_domain (halconImage, RegionDifference, ImageReduced)
smallest_rectangle2(RegionDifference, Row16, Column16, Phi3, Length1, Length2)
gen_rectangle2(Rectangle1, Row16, Column16, Phi3, Length1, Length2)
dev_clear_window()
dev_display(halconImage)
dev_set_color(‘blue’)
dev_display(Rectangle1 )
*获取4个顶点
tuple_cos (Phi3, Cos)
tuple_sin (Phi3, Sin)
dev_set_color(‘red’)
Rows:=[]
Columns:=[]
右上顶点
a:= -Length1Cos - Length2Sin
b := -Length1Sin + Length2Cos
gen_cross_contour_xld(Cross, Row16-b, Column16+a, 6, Phi3)
右下顶点
c := Length1Cos - Length2Sin
d := Length1Sin + Length2Cos
gen_cross_contour_xld(Cross, Row16-d, Column16+c, 6, Phi3)
左下顶点
e:= Length1Cos + Length2Sin
f := Length1Sin - Length2Cos
gen_cross_contour_xld(Cross, Row16-f, Column16+e, 6, Phi3)
左上顶点
g := -Length1Cos + Length2Sin
h := -Length1Sin - Length2Cos
gen_cross_contour_xld(Cross, Row16-h, Column16+g, 6, Phi3)
Rows:=[Rows,Row16-b]
Rows:=[Rows,Row16-d]
Rows:=[Rows,Row16-f]
Rows:=[Rows,Row16-h]
Columns:=[Columns,Column16+a]
Columns:=[Columns,Column16+c]
Columns:=[Columns,Column16+e]
Columns:=[Columns,Column16+g]
distance_pp(Rows, Columns, [1024,1024,1024,1024], [0,0,0,0], Distance)
*获得矩形和感兴趣区域
gen_rectangle2(Rectangle2,newRow, newColumn, Phi3, 150, 150)
*从原图里面把矩形区域扣出来
reduce_domain (ImageReduced, Rectangle2, ImageReduced1)
*边缘检测 edgessubpix
edges_sub_pix (ImageReduced1, Edges, ‘canny’, 1, 1, 30)
select_shape_xld (Edges, SelectedXLD, ‘contlength’, ‘and’,20 , 2000)
*分割线
segment_contours_xld (SelectedXLD, ContoursSplit, ‘lines’, 5, 1, 0.5)
select_shape_xld (ContoursSplit, SelectedXLD1, ‘contlength’, ‘and’,20 , 1000)
union_collinear_contours_xld (SelectedXLD1, UnionContours,100, 20, 2, 0.1, ‘attr_keep’)
count_obj (UnionContours, NumberLine)
*清空,写入数组
gen_empty_obj (Lines)
dev_clear_window()
dev_display(halconImage)
dev_set_color(‘red’)
dev_set_line_width(1)
dev_display(Contour1)
dev_set_color(‘blue’)
select_contours_xld (UnionContours, LinesHorizontalS, ‘direction’, rad(-30), rad(30), 0, 0)
intersection_lines (RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Row, Column, IsOverlapping1)
*2.2 只有一个点的时候输出
tuple_length(Row,RowLength)
if (RowLength=1)
gen_cross_contour_xld (Cross, Row, Column, 30, 2)
stop()
endif
endfor