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 1read_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)* 移动区域大于0if(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 1read_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 1select_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