9点标定是通过9个点计算出相机坐标系到机械手坐标系的一个仿射变换,(实际上空间中的二维平面的仿射变换最低只需要3个点就可以,但是为了保证矩阵的准确性,数据点一般采用9-12点)。在实际应用过程中,需要获取像素下特征点的坐标和对应机械手的坐标。联立方程组求解即可得到对应仿射变换的矩阵,实际应用场景主要分为眼在手上和眼在手外,下面具体介绍使用过程:
vector_to_hom_mat2d(Row,Column,Row_robot,Column_robot,HomMat2D)
%求解变换矩阵,HomMat2D是图像坐标和机械手坐标之间的关系
读取空白图片
read_image (Image33, 'E:/Halcon数据/资源图片/33.png')
绘制九点
get_image_size (Image33, Width, Height)
dev_get_window (WindowHandle)
query_font (WindowHandle, Font)
FontWithSize := Font[0]+'-40'
dev_set_color ('green')
dev_set_line_width (3)
set_font (WindowHandle, FontWithSize)
gen_arrow_contour_xld (Arrow, 100, 100, 100, Width-100, 15, 15)
set_tposition (WindowHandle, 75, Width-50)
write_string (WindowHandle, 'X')
gen_arrow_contour_xld (Arrow, 100, 100, Height-100,100, 15, 15)
set_tposition (WindowHandle, Height-70,100)
write_string (WindowHandle, 'Y')
stop ()
dev_set_color ('red')
R:=[]
C:=[]
R_Robot:=[]
C_Robot:=[]
for Index := 0 to 2 by 1
for Index1 := 0 to 2 by 1
R_Robot:=[R_Robot,1+Index*2]
C_Robot:=[C_Robot,1+Index1*2]
R:=[R,200+Index*200]
C:=[C,200+Index1*200]
endfor
endfor
tuple_gen_const (9, 10, Radios)
gen_circle (Circle, R, C, Radios)
for Index2 := 0 to 8 by 1
set_tposition (WindowHandle, R[Index2]-100, C[Index2]-20)
write_string (WindowHandle, Index2+1)
endfor
col:=[0,0,0]
overpaint_region (Image33, Circle, col, 'fill')
rgb1_to_gray (Image33, GrayImage)
本次采用运动轨迹如图所示
获取图像与机械坐标
R_Image:=[]
C_Image:=[]
gen_empty_obj (EmptyObject)
draw_circle (WindowHandle, Row, Column, Radius)
for Index := 0 to 2 by 1
for Index1 := 0 to 2 by 1
gen_circle (Circle1, Row+Index*200, Column+Index1*200, Radius)
reduce_domain (GrayImage, Circle1, ImageReduced)
threshold (ImageReduced, Region, 0, 120)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 450)
area_center (SelectedRegions, Area, Row1, Column1)
gen_cross_contour_xld (Cross, Row1, Column1, 20, 0.785398)
concat_obj ( Cross,EmptyObject, EmptyObject)
dev_display (GrayImage)
dev_display (EmptyObject)
R_Image:=[R_Image,Row1]
C_Image:=[C_Image,Column1]
stop ()
endfor
endfor
计算标定矩阵
vector_to_hom_mat2d(C_Image,R_Image, C_Robot,R_Robot, HomMat2Dcalib)
FontWithSize := Font[0]+'-20'
dev_set_color ('blue')
set_font (WindowHandle, FontWithSize)
set_tposition (WindowHandle, 700, 100)
write_string (WindowHandle, '矩阵参数:'+HomMat2Dcalib[0]+','+HomMat2Dcalib[1]+','+HomMat2Dcalib[2]+','+HomMat2Dcalib[3]+','+HomMat2Dcalib[4]+','+HomMat2Dcalib[5])
affine_trans_point_2d (HomMat2Dcalib, Column1, Row1, Qx, Qy)
获取旋转标定点位
在旋转轴工具上放置一个Mark点,旋转角度,获得三个点,三点拟合圆,获得圆心坐标和旋转半径(默认已经做完九点标定,这三个点转为世界坐标后再计算圆心坐标和旋转半径)。
dev_set_color ('green')
*计算旋转目标
YY:=[]
XX:=[]
*以图像坐标第一点位为原点,以200为半径拟合圆,方便反推验证
gen_circle_contour_xld (ContCircle, R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', 1)
*点1
intersection_line_circle (R_Image[0], C_Image[0],R_Image[2], C_Image[2], R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', Row2, Column2)
YY:=[YY,Row2[0]]
XX:=[XX,Column2[0]]
*点2
intersection_line_circle (R_Image[0], C_Image[0],R_Image[5], C_Image[5], R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', Row2, Column2)
YY:=[YY,Row2[0]]
XX:=[XX,Column2[0]]
*点3
intersection_line_circle (R_Image[0], C_Image[0],R_Image[8], C_Image[8], R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', Row2, Column2)
YY:=[YY,Row2[0]]
XX:=[XX,Column2[0]]
*点4
intersection_line_circle (R_Image[0], C_Image[0],R_Image[7], C_Image[7], R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', Row2, Column2)
YY:=[YY,Row2[0]]
XX:=[XX,Column2[0]]
*点5
intersection_line_circle (R_Image[0], C_Image[0],R_Image[6], C_Image[6], R_Image[0], C_Image[0], 300, 0, 6.28318, 'positive', Row2, Column2)
YY:=[YY,Row2[0]]
XX:=[XX,Column2[0]]
tuple_gen_const (5, 20, Radios1)
gen_circle (Circle1, YY, XX, Radios1)
dev_display (Image33)
dev_display (Circle1)
Xx:=[]
Yy:=[]
for Index3 := 0 to 4 by 1
affine_trans_point_2d (HomMat2Dcalib, XX[Index3], YY[Index3], X1, Y1)
Xx:=[Xx,X1]
Yy:=[Yy,Y1]
endfor
*拟合圆,求取旋转中心
gen_contour_polygon_xld (Contour, Yy, Xx)
fit_circle_contour_xld (Contour, 'algebraic', -1, 0, 0, 3, 2, Y_center, X_center, Radius1, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle1, Y_center, X_center, Radius1, StartPhi, EndPhi, 'positive', 1)
*求取目标旋转角度,可与初始模板/欲贴合模板对比
Angle:=0
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, rad(Angle), X_center, Y_center, HomMat2DRotate)
set_tposition (WindowHandle, 700, 100)
write_string (WindowHandle, '旋转矩阵参数:'+HomMat2DRotate[0]+','+HomMat2DRotate[1]+','+HomMat2DRotate[2]+','+HomMat2DRotate[3]+','+HomMat2DRotate[4]+','+HomMat2DRotate[5])
标签:Image,九点,标定,gen,Halcon,YY,XX,circle,WindowHandle
From: https://blog.csdn.net/2202_75557553/article/details/142974223