Halcon 카메라 스탬프 주석
17522 단어 기계 시각
*
* Initialize the program
dev_close_window () //
dev_open_window (0, 0, 768, 576, 'black', WindowHandle) // ,
dev_update_pc ('off') //
dev_update_window ('off') // off
dev_update_var ('off') // 。
dev_update_time ('off') // 。
dev_set_draw ('margin') //
dev_set_line_width (3) // 3
set_display_font (WindowHandle, 14, 'mono', 'false', 'false') // ,
*
* Calibrate the camera.
*
CaltabName := 'caltab_big.descr'
StartCamPar := [0.008,0,0.0000086,0.0000086,384,288,768,576] //
NStartPose := []
NRow := []
NCol := []
caltab_points (CaltabName, X, Y, Z)
NumImages := 10
for I := 1 to NumImages by 1
read_image (Image, 'calib/calib-3d-coord-' + I$'02d')
dev_display (Image)
find_caltab (Image, Caltab, CaltabName, 3, 112, 5) // 。
dev_set_color ('green')
dev_display (Caltab)
find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose) // 。
// Image CalTabRegion 2D [RCoord,CCoord]
// (StartPose) , 3D (= )
dev_set_color ('red')
disp_circle (WindowHandle, RCoord, CCoord, gen_tuple_const(|RCoord|,1.5))
NStartPose := [NStartPose,StartPose]
NRow := [NRow,RCoord]
NCol := [NCol,CCoord]
endfor
camera_calibration (X, Y, Z, NRow, NCol, StartCamPar, NStartPose, 'all', CamParam, NFinalPose, Errors)
//X x ( )。
//Y y ( )。
//Z z ( )。
//NRow ( )。
//NCol 。
//StartCamPar 。
//NStartPose 。
//
//CamParam 。
//NFinalPose 。
//Errors
//
// / \ / x \ / \ / \
// | p(c) | = | y | = | R t | * | p(w) |
// | | | z | | | | |
// \ 1 / \ 1 / \ 0 0 1 / \ 1 /
//
// , 。
//
// NumImage 10 。
NumImage := 9
//
Pose := NFinalPose[(NumImage - 1) * 7:(NumImage - 1) * 7 + 6]
read_image (Image, 'calib/calib-3d-coord-' + NumImage$'02d')
dev_display (Image)
dev_set_color ('green')
// 。
threshold (Image, Region, 0, 140) // 。
connection (Region, ConnectedRegions) // 。
select_shape (ConnectedRegions, SelectedRegions, ['holes_num','rect2_len1','rect2_len2'], 'and', [1,150,150], [1,200,200])// 。
fill_up (SelectedRegions, RegionFillUp) //Fill up holes in regions.
// 。
gen_contour_region_xld (RegionFillUp, Contours, 'center') // XLD 。
segment_contours_xld (Contours, ContoursSplit, 'lines', 7, 4, 2) // XLD 。
regress_contours_xld (ContoursSplit, RegressContours, 'no', 1) // XLD 。
select_contours_xld (RegressContours, VerticalContours, 'direction', rad(45), rad(135), -0.5, 0.5) // XLD 。
select_contours_xld (VerticalContours, LongContours, 'length', 150, 500, -0.5, 0.5)
// 。
select_obj (LongContours, Contour, 1)
get_contour_xld (Contour, Row, Col)
RowBegin1 := Row[0]
ColBegin1 := Col[0]
RowEnd1 := Row[|Row| - 1]
ColEnd1 := Col[|Col| - 1]
select_obj (LongContours, Contour, 2)
get_contour_xld (Contour, Row, Col)
RowBegin2 := Row[0]
ColBegin2 := Col[0]
RowEnd2 := Row[|Row| - 1]
ColEnd2 := Col[|Col| - 1]
RowM1 := (RowBegin1 + RowEnd1) / 2
ColM1 := (ColBegin1 + ColEnd1) / 2
RowM2 := (RowBegin2 + RowEnd2) / 2
ColM2 := (ColBegin2 + ColEnd2) / 2
Phi := atan2(ColM2 - ColM1,RowM2 - RowM1)
Length1 := sqrt(pow(ColM2 - ColM1,2) + pow(RowM2 - RowM1,2)) / 2
Row := (RowM1 + RowM2) / 2
Column := (ColM1 + ColM2) / 2
// 。
gen_measure_rectangle2 (Row, Column, Phi + rad(90), Length1 + 7, 8, 768, 576, 'nearest_neighbor', MeasureHandle) // 。
measure_pos (Image, MeasureHandle, 1, 20, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance1) // 。
close_measure (MeasureHandle) // z = 0。
Rows := [RowEdge[0],RowEdge[|RowEdge| - 1]]
Columns := [ColumnEdge[0],ColumnEdge[|RowEdge| - 1]]
// , z 。 , 。
//set_origin_pose(Pose,0,0,0.0045,Pose) 。
set_origin_pose (Pose, 0, 0, 0.0045, Pose)
// 。
image_points_to_world_plane (CamParam, Pose, Rows, Columns, 'm', SX, SY)
Width := sqrt(pow(SX[0] - SX[1],2) + pow(SY[0] - SY[1],2))
dev_display (Image)
dev_set_line_width (3)
dev_set_draw ('margin')
disp_rectangle2 (WindowHandle, Row, Column, Phi + rad(90), Length1 + 7, 8)
dev_set_color ('red')
dev_set_draw ('fill')
disp_circle (WindowHandle, Rows, Columns, gen_tuple_const(|Rows|,3.5))
dev_set_draw ('margin')
disp_message (WindowHandle, 'Width = ' + (Width * 100)$'8.5f' + 'cm', 'image', 48, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
// , 。
// 。
erosion_circle (RegionFillUp, ROI, 22.5) // 。
reduce_domain (Image, ROI, ImageReduced) // reduce_domain 。
// 。 , 。 。
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 60) // Deriche,Lanser,Shen Canny 。
* Fit ellipses to extracted edges to get their axes.
fit_ellipse_contour_xld (Edges, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)
// Deriche,Lanser,Shen Canny 。
MeanRadius1 := mean(Radius1)
MeanRadius2 := mean(Radius2)
DevRadius1 := deviation(Radius1) 。
DevRadius2 := deviation(Radius2)
// , , , 。
contour_to_world_plane_xld (Edges, WorldCircles, CamParam, Pose, 'mm')
* Fit ellipses to the circles in world coordinates to get their axes.
fit_ellipse_contour_xld (WorldCircles, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, RadiusW1, RadiusW2, StartPhi, EndPhi, PointOrder) // XLD 。
MeanRadiusW1 := mean(RadiusW1)
MeanRadiusW2 := mean(RadiusW2)
DevRadiusW1 := deviation(RadiusW1)//
DevRadiusW2 := deviation(RadiusW2)
dev_display (Image)
dev_set_color ('yellow')
dev_set_line_width (3)
dev_display (Edges)
dev_set_color ('blue')
disp_message (WindowHandle, 'Image: Mean radii: (' + MeanRadius1$'6.3f' + ',' + MeanRadius2$'6.3f' + ') pixels', 'image', 8, 12, 'black', 'true')
disp_message (WindowHandle, ' Std. dev.: (' + (DevRadius1 / MeanRadius1 * 100)$'6.3f' + ',' + (DevRadius2 / MeanRadius2 * 100)$'6.3f' + ') % Std. dev.: (' + (DevRadius1 / MeanRadius1 * 100)$'6.3f' + ',' + (DevRadius2 / MeanRadius2 * 100)$'6.3f' + ') %', 'image', 30, 12, 'black', 'true')
disp_message (WindowHandle, 'World: Mean radii: (' + (MeanRadiusW1 / 10)$'6.3f' + ',' + (MeanRadiusW2 / 10)$'6.3f' + ') cm', 'image', 52, 12, 'black', 'true')
disp_message (WindowHandle, ' Std. dev.: (' + (DevRadiusW1 / MeanRadiusW1 * 100)$'6.3f' + ',' + (DevRadiusW2 / MeanRadiusW2 * 100)$'6.3f' + ') %', 'image', 74, 12, 'black', 'true')
*