Halcon 使用光流算子检测运动物体

算子

optical_flow_mg 计算两个图像之间的光流

optical_flow_mg(ImageT1, ImageT2 : VectorField : Algorithm, SmoothingSigma, IntegrationSigma, FlowSmoothness, GradientConstancy, MGParamName, MGParamValue : )
当使用光流计算函数时,以下是参数的解释:
    ImageT1:输入的第一幅图像。
    ImageT2:输入的第二幅图像。
    VectorField:计算得到的光流结果,会存储在这个变量中。
    Algorithm:用于计算光流的算法,可以选择 'clg', 'ddraw', 'fdrig' 中的一个。
    SmoothingSigma:初始高斯平滑的标准差。这个参数用于控制光流计算过程中的图像平滑程度。
    IntegrationSigma:积分滤波器的标准差。这个参数影响了光流场的积分过程。
    FlowSmoothness:平滑项相对于数据项的权重。它影响了光流场的平滑程度。
    GradientConstancy:梯度稳定性相对于灰度稳定性的权重。这个参数用于平衡梯度稳定性和灰度稳定性在光流计算中的影响。
    MGParamName:多网格算法的参数名称,可以指定不同的参数名来调整多网格算法的行为。
    MGParamValue:多网格算法的参数值,根据实际需求进行设置。

vector_field_length 计算向量场的向量长度

vector_field_length(VectorField : Length : Mode : )
以下是各个参数的解释:

    VectorField:输入的矢量场,包含了需要计算长度的向量。
    Length:计算得到的向量长度结果将会存储在这个变量中。
    Mode:用于计算向量长度的模式选择参数。
        'length':计算向量的长度。
        'squared_length':计算向量的平方长度。

select_shape_std 选择给定形状的区域

select_shape_std(Regions : SelectedRegions : Shape, Percent : )
以下是各个参数的解释:
    Regions:输入的区域,包含需要进行选择的区域。
    SelectedRegions:经过选择后具有所需形状的区域将会存储在这个变量中。
    Shape:要检查的形状特征参数。
        'max_area':检查最大面积。
        'rectangle1':检查矩形1'rectangle2':检查矩形2。
    Percent:相似性度量参数,用于确定区域形状的相似度。
        默认值:70.0
        推荐值范围:10.0, 30.0, 50.0, 60.0, 70.0, 80.0, 90.0, 95.0, 100.0
        典型取值范围:0.0 ≤ Percent ≤ 100.0
        最小增量:0.1
        推荐增量:10.0

vector_field_to_real 将矢量场图像转换为两个实值图像

vector_field_to_real(VectorField : Row, Col : : )
    VectorField (输入对象): 这个参数代表一个矢量场,通常描述图像中每个点处矢量的方向和大小。
    Row (输出对象): 表示沿着行方向的矢量分量,指示图像中沿着行的矢量场分量。
    Col (输出对象): 表示沿着列方向的矢量分量,指示图像中沿着列的矢量场分量。

intensity 计算灰度值的均值和偏差

intensity(Regions, Image : : : Mean, Deviation)
参数如下:
    Regions (输入对象): 计算特征的区域。
    Image (输入对象): 灰度值图像,用于计算特征。
    Mean (输出控制): 区域的平均灰度值。
    Deviation (输出控制): 区域内灰度值的偏差

local_max_sub_pix 以亚像素精度检测局部极大值

local_max_sub_pix(Image : : Filter, Sigma, Threshold : Row, Column)
参数说明如下:

    Image(输入对象):单通道图像,表示输入图像。
    Filter(输入控制):字符串,用于计算局部导数的方法。
        默认值:'facet'
        可选值:'facet''gauss'
    Sigma(输入控制):实数,高斯核的标准差。如果Filter为'facet',则Sigma可以设为0.0,以避免对输入图像进行平滑处理。
        建议值:0.7, 0.8, 0.9, 1.0, 1.2, 1.5, 2.0, 3.0
        约束:Sigma >= 0.0
    Threshold(输入控制):实数,Hessian矩阵特征值的绝对值最小值。
        默认值:5.0
        建议值:2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
        约束:Threshold >= 0.0
    Row(输出控制):点y数组,检测到的极大值的行坐标。
    Column(输出控制):点x数组,检测到的极大值的列坐标。

Halcon 光流算子示例

在这里插入图片描述

dev_update_off ()
dev_close_window ()
* Initialize the image sequence.
read_image (Image1, 'xing/xing000')
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
* Set the display parameters for the vector field.
dev_set_paint (['vector_field',6,1,2])
dev_set_draw ('margin')
for I := 1 to 587 by 1
    * Read the current image of the image sequence.
    read_image (Image2, 'xing/xing' + I$'03')
    * Compute the optical flow.
    * 计算光流
    optical_flow_mg (Image1, Image2, VectorField, 'fdrig', 0.8, 1, 8, 5, 'default_parameters', 'accurate')
    * Segment the optical flow vector field.
    * 阈值处理
    threshold (VectorField, Region, 1, 10000)
    * Display the current image of the sequence.  Note that this means that
    * the optical flow vectors will also be displayed at the "end" of the movement
    * of the objects in the image.
    * 显示
    dev_display (Image2)
    * Display the optical flow.
    dev_set_color ('yellow')
    dev_set_line_width (1)
    dev_display (VectorField)
    * Display the segmented optical flow.
    dev_set_color ('green')
    dev_set_line_width (3)
    dev_display (Region)
    * Copy the current image to the previous image of the sequence.
    * 将当前图像复制到序列的前一个图像
    copy_obj (Image2, Image1, 1, 1)
endfor

在这里插入图片描述

Halcon 获取移动物体的移动方向

* 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')
dev_close_window ()
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
dev_set_draw ('margin')
* 
* Generate ROI
* 产生一个xld多边形区域
gen_contour_polygon_xld (ROI, [0,0,283,348,479,479] * ZoomFactor, [0,379,379,434,639,0] * ZoomFactor)
* 产生轮廓
gen_region_contour_xld (ROI, RegionROI, 'filled')
* 裁剪
reduce_domain (Image1, RegionROI, Image1ROI)
* 
* 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')
    * 按照xld轮廓裁剪
    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)
        * 移动区域大于0
        if (Area > 0)
            * 形成一个外轮廓
            shape_trans (RegionMovement, ConvexHullregion, 'convex')
            * 外轮廓和ROI区域求交集
            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

在这里插入图片描述

Halcon 卫星图获取移动物体的移动方向

* This example demonstrates the use of the optical flow operators.
* By calculating the optical flow between two images, the
* position, speed, and movement direction of particles are calculated.
* 1. 读取图片
dev_update_off ()
dev_close_window ()
* 
read_image (Image1, 'hydraulic_engineering/hydraulic_engineering_01')
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
* 
dev_set_draw ('margin')
RCenter := -1
CCenter := -1
MeanR := 0
MeanC := 0
tanDir := 0
ArrowLength := 15
MinLength := 0.375
* 
for Index := 2 to 50 by 1
    read_image (Image2, 'hydraulic_engineering/hydraulic_engineering_' + Index$'02')
    * 2.估计移动粒子的区域
    * Calculate the optical flow between two images
    * 计算光流
    optical_flow_mg (Image1, Image2, VectorField, 'clg', 1, 1, 1000, 5, 'default_parameters', 'fast')
    * 计算向量场
    vector_field_length (VectorField, LengthImage, 'squared_length')
    * 
    * Estimate the region with moving particles using the length
    * of the vectors of the calculated vector field
    *
    * 计算灰度值
    min_max_gray (LengthImage, LengthImage, 0, Min, MaxLength, Range)
    if (MaxLength > MinLength)
        threshold (LengthImage, ROI, MinLength, MaxLength)
        * 
        * Find the position of particles in the original image using
        * an estimated ROI
        dilation_circle (ROI, RegionDilation, 3.5)
        reduce_domain (Image2, RegionDilation, ImageReduced)
        * 获取裁剪后图片的亚像素极大值点的行列坐标
        local_max_sub_pix (ImageReduced, 'facet', 1.0, 4, Row, Column)
        * 
        dev_set_color ('cyan')
        dev_set_line_width (2)
        if (|Row| > 0)
            * 产生区域点
            gen_region_points (Points, Row, Column)
            * 将点形成单独的连通域
            connection (Points, ConnectedRegions)
            * 将点膨胀
            dilation_circle (ConnectedRegions, PointsDilated, 2.5)
            * 
            * Display found particles and their movement directions
             * 3.获取移动方向
            count_obj (PointsDilated, NumberReg)
            dev_display (Image2)
            dev_display (PointsDilated)
            for J := 1 to NumberReg by 1
               
                select_obj (PointsDilated, SelectedRegions, J)
                * 
                * Estimate the speed of the particles
                reduce_domain (VectorField, SelectedRegions, ImageReducedVF)
                * 将矢量图转换为两个有方向的实值图像
                vector_field_to_real (ImageReducedVF, RowImage, ColumnImage)
                * 计算 行列坐标的平均灰度值和偏差
                intensity (ColumnImage, ColumnImage, MeanC, Deviation)
                intensity (RowImage, RowImage, MeanR, Deviation)
                Length := sqrt(MeanR * MeanR + MeanC * MeanC)
                * 绘制箭头
                gen_arrow_contour_xld (Arrow, Row[J - 1], Column[J - 1], Row[J - 1] + MeanR / Length * ArrowLength, Column[J - 1] + MeanC / Length * ArrowLength, 5, 10)
                dev_display (Arrow)
            endfor
        endif
    endif
    copy_obj (Image2, Image1, 1, 1)
endfor

在这里插入图片描述

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Halcon边缘检测算子是用于提取图像物体边缘的工具。根据引用的信息,Halcon常用的边缘检测算子包括以下几种: 1. edges_image: 用于提取2D图像的边缘。 2. edges_sub_pix: 用于提取2D图像的亚像素边缘。 3. edges_object_model_3d: 用于提取3D图像的边缘。 4. edges_color和edges_color_sub_pix: 用于提取彩色图像的边缘。 这些算子可以根据需要选择不同的边缘检测算法来提取物体边缘。其,edges_image以像素为单位提取边缘特征,而edges_sub_pix以比像素更小的单位来提取边缘特征,通常采用矩法、插值法和拟合法等方法。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [halcon边缘缺陷检测.zip](https://download.csdn.net/download/u014652390/13703647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Halcon —— 边缘检测算子详解](https://blog.csdn.net/baidu_35536188/article/details/112351198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值