Halcon 极坐标变换检测圆形物体

在这里插入图片描述

* 检测瓶口缺陷
* 
* 
* tuning parameters
SmoothX := 501
ThresholdOffset := 25
MinDefectSize := 50
* 
* initialization
PolarResolution := 640
RingSize := 70
*获取存放空区域的状态
get_system ('store_empty_region', StoreEmptyRegion)
*设置存放空区域的状态
set_system ('store_empty_region', 'false')
*读取图像
read_image (Image, 'bottles/bottle_mouth_01')
*关闭程序计数器,变量更新,图形窗口更新
dev_update_off ()
*关闭窗口
dev_close_window ()
dev_close_window ()
*按图像大小创建一个新窗口
dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1)
*设置字体信息
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
*显示图像
dev_display (Image)
*设置区域填充方式
dev_set_draw ('margin')
*设置输出对象线宽
dev_set_line_width (3)
*按图像大小创建一个新窗口
dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle)
*设置区域填充方式
dev_set_draw ('margin')
*设置输出对象线宽
dev_set_line_width (3)
*设置对象显示颜色
dev_set_color ('red')

*激活WindowHandle1窗口
dev_set_window(WindowHandle1)
* Main loop
* 
* Detect defects in bottle necks
for Index := 2 to 16 by 1
    *读取一张图像
    read_image (Image, 'bottles/bottle_mouth_'+Index$'.02')
    *显示图像
    dev_display (Image) 
    *自动阈值
    auto_threshold (Image, Regions, 2)
    *获取区域一
    select_obj (Regions, DarkRegion, 1)
    *对区域一进行开运算
    opening_circle (DarkRegion, RegionOpening, 3.5)
    *对开运算区域进行闭运算
    closing_circle (RegionOpening, RegionClosing, 25.5)
    *填充闭运算后区域
    fill_up (RegionClosing, RegionFillUp)
    *获取区域外边界
    boundary (RegionFillUp, RegionBorder, 'outer')
    *对区域边界进行膨胀运算
    dilation_circle (RegionBorder, RegionDilation, 3.5)
    *剪切区域里的图像
    reduce_domain (Image, RegionDilation, ImageReduced)
    * 
    * 运用CANNY算法进行边缘探测
    edges_sub_pix (ImageReduced, Edges, 'canny', 0.5, 20, 40)
    *对边缘轮廓分割成直线和圆
    segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
    *将共圆上的轮廓连接起来
    union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, 'true', 1)
    *获取轮廓的长度
    length_xld (UnionContours, Length)
    *对长度数值进行降序排列,并获取数值最大的长度值
    select_obj (UnionContours, LongestContour, sort_index(Length)[|Length|-1]+1)
    *对最大的轮廓进行拟合圆操作
    fit_circle_contour_xld (LongestContour, 'ahuber', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
    * 
    * Part 2: Transform the ring-shaped bottle neck region to a rectangle
    *生成拟合圆
    gen_circle (Circle, Row, Column, Radius)
    *对拟合圆进行膨胀运算
    dilation_circle (Circle, RegionDilation, 5)
    *对拟合圆进行腐蚀运算
    erosion_circle (Circle, RegionErosion, RingSize-5)
    *求两区域补集
    difference (RegionDilation, RegionErosion, RegionDifference)
    *获取补集区域里的图像
    reduce_domain (Image, RegionDifference, ImageReduced)
    *将图像从笛卡尔直角坐标系转换到极坐标系
    polar_trans_image (ImageReduced, ImagePolar, Row, Column, PolarResolution, Radius+5)
    * 
    * Part 3: Find defects with a dynamic threshold
    * Note the strong smoothing in x-direction in the transformed image.
    *剪切矩形区域里的图像
    crop_part (ImagePolar, ImagePart, Radius-RingSize, 0, PolarResolution, RingSize)
    *将最大灰度值在0-255范围拉伸
    scale_image_max (ImagePart, ImageScaleMax)
    *对灰度拉伸的图像进行均值滤波
    mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
    *局部阈值处理
    dyn_threshold (ImageScaleMax, ImageMean, Regions1, 50, 'not_equal')
    *连通处理
    connection (Regions1, Connection)
    *根据高度过滤区域
    select_shape (Connection, SelectedRegions, 'height', 'and', 9, 99999)
    * 用矩形结构元素进行闭运算
    closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20)
    *将区域连接起来
    union1 (RegionClosing1, RegionUnion)
    * 将区域从极坐标转换到直角坐标系中
    polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 6.28319, 0, Radius-RingSize, Radius, 640, RingSize, 1280, 1024, 'nearest_neighbor')
    * 
    * Part 4: Display results
    * 激活窗口WindowHandle1
    dev_set_window (WindowHandle1)
    dev_display (Image)
    dev_set_color ('blue')
    dev_display (RegionDifference)
    dev_set_color ('red')
    dev_display (XYTransRegion)
    * display polar transformed inspected region with results
    * The image and resulting region are rotated by 90 degrees
    * only for visualization purposes! (I.e. to fit better on the screen)
    * The rotation is NOT necessary for the detection algorithm.
    *激活窗口WindowHandle
    dev_set_window (WindowHandle)
    *旋转图像
    rotate_image (ImagePart, ImageRotate, 90, 'constant')
    dev_display (ImageRotate)
    count_obj (RegionUnion, Number)
    if (Number>0)
        *缺陷区域沿对角线镜像
        mirror_region (RegionUnion, RegionMirror, 'diagonal', PolarResolution)
        *对镜像区域再次沿列方向镜像
        mirror_region (RegionMirror, RegionMirror, 'row', PolarResolution)
        *显示镜像以后的区域
        dev_display (RegionMirror)
        disp_message (WindowHandle1, 'Not OK', 'window', -1, -1, 'red', 'false')
    else
        disp_message (WindowHandle1, 'OK', 'window', -1, -1, 'forest green', 'false')
    endif
    if (Index<16)
        disp_continue_message (WindowHandle1, 'black', 'true')
        stop ()
    endif
    
    dev_set_window (WindowHandle1)
    
endfor
* 恢复存储空区域的状态
set_system ('store_empty_region', StoreEmptyRegion)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值