Halcon实例系列教程-应用范围-安全系统-光流分析一例optical_flow_bicycle.hdev

本文介绍了如何使用光学流算法对图像序列中的移动物体进行检测,通过分析ROI区域,当检测到移动时控制设备动作,如自动开启出口门。展示了从图像预处理、区域分割到运动分析的完整过程。
摘要由CSDN通过智能技术生成

这个例子演示了使用光流来检测图像序列中的移动物体。通过分析感兴趣区域来检测运动目标。如果检测到移动物体,则自动打开出口门。

如图所示,ROI设置在闸门前,通过分析骑自行车人的运动,控制横杆抬起。

* This example demonstrates the use of the optical flow for
* the detection of moving objects in an image sequence.
* The region of interest is analyzed to detect moving objects.
* If a moving object is detected, the exit gate is opened automatically.
* 
* Initialize output window
dev_update_off ()
read_image (Image1, 'bicycle/bicycle_01')
ZoomFactor := 0.5
zoom_image_factor (Image1, Image1, ZoomFactor, ZoomFactor, 'constant')
*zoom_image_factor(Image : ImageZoomed : ScaleWidth, ScaleHeight, Interpolation : )
*zoom_image_factor将图像图像按宽度的ScaleWidth因子和高度的ScaleHeight因子缩放。参数*Interpolation决定了使用的插值类型(参见affine_trans_image)。输入图像的域被忽略,即假定为图像的*整个矩形。
dev_close_window ()
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
dev_set_draw ('margin')
* 
* Generate ROI
gen_contour_polygon_xld (ROI, [0, 0, 283, 348, 479, 479] * ZoomFactor, [0, 379, 379, 434, 639, 0] * ZoomFactor)
*gen_contour_polygon_xld( : Contour : Row, Col : )
*gen_contour_polygon_xld从元组Row和col中给定的多边形生成XLD轮廓轮廓。如果轮廓是从核心库之外的
*例程获得的,则此操作符很有用,但更高级的操作符,例如多边形逼近和提取平行线,将在轮廓上执行。
gen_region_contour_xld (ROI, RegionROI, 'filled')
*gen_region_contour_xld(Contour : Region : Mode : )
*gen_region_contour_xld从亚像素XLD轮廓创建区域。轮廓根据Bresenham算法采样,并受算子set_system
*参数“邻域”的影响。打开的轮廓在转换为区域之前是关闭的。最后,Mode参数定义该区域是填充('fill ')还
*是由其轮廓('margin')返回。
reduce_domain (Image1, RegionROI, Image1ROI)
*reduce_domain(Image, Region : ImageReduced : : )
*算子reduce_domain将给定图像的定义域缩减到指定区域。新定义域计算为旧定义域与区域的交集。因此,新
*的定义域可以是区域的子集。矩阵的大小没有改变。
* 
* Main loop: Calculate optical flow and display moving area
* 
for I := 2 to 27 by 1
    read_image (Image2, 'bicycle/bicycle_' + I$'.2')
    * 
    * Zoom images to speed-up calculation of optical flow
    zoom_image_factor (Image2, Image2, ZoomFactor, ZoomFactor, 'constant')
    reduce_domain (Image2, RegionROI, Image2ROI)
    * 
    optical_flow_mg (Image1ROI, Image2ROI, VectorField, 'fdrig', 0.8, 1, 10, 5, ['default_parameters', 'warp_zoom_factor'], ['fast', 0.8])
    vector_field_length (VectorField, LengthImage, 'squared_length')
    * 
    * Segment regions with moving objects in the defined ROI
    min_max_gray (RegionROI, LengthImage, 0.1, Min, Max, Range)
    dev_display (Image2)
    if (Max > 2)
        threshold (LengthImage, RegionMovement, 2, Max)
        connection (RegionMovement, ConnectedRegions)
        * Select largest moving region
        select_shape_std (ConnectedRegions, RegionMovement, 'max_area', 70)
        area_center (RegionMovement, Area, RCenterNew, CCenterNew)
        if (Area > 0)
            shape_trans (RegionMovement, ConvexHullregion, 'convex')
            intersection (RegionROI, ConvexHullregion, RegionMovementInROI)
            reduce_domain (VectorField, ConvexHullregion, VectorReduced)
            vector_field_to_real (VectorReduced, Row, Column)
            * 
            * Estimate the movement direction and the speed
            intensity (RegionMovementInROI, Row, MeanRow, Deviation)
            intensity (RegionMovementInROI, Column, MeanColumn, Deviation1)
            * 
            * Display results
            dev_set_line_width (1)
            dev_set_color ('yellow')
            dev_display (VectorReduced)
            * Display region of interest
            dev_set_line_width (3)
            dev_set_color ('magenta')
            dev_display (RegionROI)
            * Display region of moving object in the region of interest
            dev_set_color ('green')
            dev_display (RegionMovementInROI)
            gen_arrow_contour_xld (Arrow, RCenterNew, CCenterNew, RCenterNew + MeanRow, CCenterNew + MeanColumn, 10, 10)
            dev_display (Arrow)
        endif
    endif
    * 
    copy_obj (Image2ROI, Image1ROI, 1, 1)
endfor

reduce_domain处理后,ROI区域被定义下来。

随着目标物体的出现,area逐渐变大

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hushenming3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值