其他分享
首页 > 其他分享> > Halcon 区域保存 区域测距 几何变换 仿射变换

Halcon 区域保存 区域测距 几何变换 仿射变换

作者:互联网

目录

区域操作

区域生成

以下得到的都是区域

read_image (Image, 'printer_chip/printer_chip_01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
dev_display (Image)

* 圆
draw_circle (WindowHandle, Row, Column, Radius)
gen_circle (Circle, Row, Column, Radius)

* 椭圆
draw_ellipse (WindowHandle, Row1, Column1, Phi, Radius1, Radius2)
gen_ellipse (Ellipse, Row1, Column1, Phi, Radius1, Radius2)

* 矩形
draw_rectangle1 (WindowHandle, Row11, Column11, Row2, Column2)
gen_rectangle1 (Rectangle, Row11, Column11, Row2, Column2)

* 矩形带方向
draw_rectangle2 (WindowHandle, Row3, Column3, Phi1, Length1, Length2)
gen_rectangle2 (Rectangle1, Row3, Column3, Phi1, Length1, Length2)

* 线条
draw_line (WindowHandle, Row12, Column12, Row21, Column21)
gen_region_line (RegionLines, Row12, Column12, Row21, Column21)

* 点
draw_point (WindowHandle, Row4, Column4)
gen_region_points (Region, Row4, Column4)

* 区域(任意形状)
draw_region (Region1, WindowHandle)

区域保存

* 生成矩形
draw_rectangle2 (WindowHandle, Row5, Column5, Phi2, Length11, Length21)
gen_rectangle2 (Rectangle2, Row5, Column5, Phi2, Length11, Length21)
* 保存区域
write_region (Rectangle2, 'region.hobj')

区域读取

* 读区域
read_region (RegionRoi, 'region.hobj')

区域清空和''赋值''

* 生成空的区域
gen_empty_region (EmptyRegion)
* 与gen_empty_region效果一样
gen_empty_obj() 

* 合并区域 合并多个会成为一个数组
concat_obj (EmptyRegion, RegionRoi, ObjectsConcat)

区域测距

distance 距离

image-20211230141809024

// 两点之间距离
public static double distance_pp(double x1,double x2,double y1,double y2)
{
	double b = Math.Abs(x1-x2);
    double c = Math.Abs(y1-y2);
	return Math.Sqrt(b*b + c*c);
}

点到点

distance_pp

draw_point (WindowHandle, Row, Column)
gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
draw_point (WindowHandle, Row1, Column1)
gen_cross_contour_xld (Cross1, Row1, Column1, 6, 0.785398)

distance_pp (Row, Column, Row1, Column1, Distance)
gen_region_line (RegionLines, Row, Column, Row1, Column1)
write_string (WindowHandle, '测量距离:'+ Distance)

点到直线

distance_pl 垂直直线

点到区域的最长最短

distance_pr

distance_pr (RegionLines1, Row2, Column2, DistanceMin, DistanceMax)

直线到区域的最长最短

distance_lr

draw_region (Region, WindowHandle)
draw_line (WindowHandle, Row1, Column1, Row2, Column2)
gen_region_line (RegionLines, Row1, Column1, Row2, Column2)
distance_lr (Region, Row1, Column1, Row2, Column2, DistanceMin, DistanceMax)

区域与区域最短

distance_rr

image-20211230155849801

draw_region (Region, WindowHandle)
draw_line (WindowHandle, Row1, Column1, Row2, Column2)
gen_region_line (RegionLines, Row1, Column1, Row2, Column2)
distance_rr_min (Region, RegionLines, MinDistance, Row11, Column11, Row21, Column21)
gen_region_line (RegionLines1, Row11, Column11, Row21, Column21)

案例

dilation_circle 膨胀

boundary 求区域的边缘 分界线

intersection 交集

image-20211231093939700

read_image (Image, 'D:/halcon文件/halcon学习文件/solution/image/2距离检测案例.png')
rgb1_to_gray (Image, GrayImage)
mean_image (GrayImage, ImageMean, 30, 30)
dyn_threshold (Image, ImageMean, RegionDynThresh, 5, 'light')
connection (RegionDynThresh, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
fill_up (SelectedRegions, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions1, ['area','outer_radius'], 'and', [35000,120], [200000,170])
dilation_circle (SelectedRegions1, RegionDilation, 30)
reduce_domain (Image, RegionDilation, ImageReduced)
binary_threshold (ImageReduced, Region, 'max_separability', 'light', UsedThreshold)
    
    
*找圆
connection (Region, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions2, ['area','circularity','outer_radius'], 'and', [300,0.7,7], [600,1,15])
shape_trans (SelectedRegions2, RegionTrans, 'outer_circle')
*圆点
area_center (RegionTrans, Area, Row, Column)
gen_cross_contour_xld (Cross, Row, Column, 10, 0.785398)
gen_region_points (Region1, Row, Column)
* 外部区域
select_shape (ConnectedRegions1, SelectedRegions5, 'area', 'and', 10000, 12000)
fill_up (SelectedRegions5, RegionFillUp1)
* 求边缘 分界线
boundary (RegionFillUp1, RegionBorder, 'inner')
* 得到外接矩形 得到phi
smallest_rectangle2 (RegionBorder, Row1, Column1, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, 15, 70)
* 交集的到外接矩形和生成的矩形相交的区域
intersection(RegionBorder, Rectangle, RegionIntersection)
distance_rr_min (RegionIntersection, Region1, MinDistance, Row11, Column11, Row2, Column2)
gen_region_line (RegionLines, Row11, Column11, Row2, Column2)

几何变换

halcon算子>region>Geometric Transformations

平移

move_region; 可以做区域位置平移,平移后相减可以得到区域某一边的边缘

move_region (RegionErosion, RegionMoved, -50, 0)

缩放

zoom_region 是按照比例缩放得到的,同时x,y也会放大 中心点移动

镜像

mirror_region

倒置

transpose_region

极坐标与笛卡尔坐标互换

笛卡尔坐标:直角左边

极坐标:角度和半径表示

polar_trans_region 极坐标转笛卡尔坐标

polar_trans_region_inv 笛卡尔坐标转极坐标,inv意思是相反

halcon案例:circle_barcode

转换案例:

union1 (RegionDynThresh, RegionUnion) # 合并
difference (RegionUnion, RegionUnion, RegionDifference) # 差集
intersection (SelectedRegions, ConnectedRegions, RegionIntersection) # 交集
complement # 补集

read_image (Image, 'circular_barcode')
threshold (Image, Region, 0, 100)
closing_circle (Region, RegionClosing, 3.5)
connection (RegionClosing, ConnectedRegions)
select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])
shape_trans (Ring, RegionTrans, 'outer_circle')
complement (Ring, RegionComplement)
connection (RegionComplement, ConnectedRegions1)
select_shape (ConnectedRegions1, InnerCircle, ['width','height'], 'and', [450,450], [650,650])

smallest_circle (InnerCircle, Row, Column, Radius)
smallest_circle (RegionTrans, Row1, Column1, Radius1)

mwidth := Radius1*3.14
mheigh := round(Radius1 - Radius -10)
polar_trans_image_ext (Image, PolarTransImage, Row, Column, rad(360), 0, Radius1-5, Radius+5, mwidth, mheigh, 'bilinear')

仿射变换

使用仿射变换分三步

1.创建一个二维矩阵

创建二维变换矩阵

hom_mat2d_identity (HomMat2DIdentity)

2 向矩阵中添加操作

* 2.使用平移 坐标为相对位置
hom_mat2d_translate (HomMat2DIdentity, 300, 64, HomMat2DTranslate)
平移 hom_mat2d_translate
缩放 hom_mat2d_scale
旋转 hom_mat2d_rotate

3 使用仿射变换

* 3.使用反射变换
affine_trans_region (Region, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')

刚性 仿射变换 vector_angle_to_rigid 对象图,区域进行仿射变换
扩展图像变换 affine_trans_image

综合例程讲解

ball_seq

check_blister

check_bottle

clip

find_pads

count_pellets

标签:draw,区域,region,Halcon,几何变换,Column1,gen,WindowHandle,仿射变换
来源: https://www.cnblogs.com/lddragon1/p/15765085.html