MFC与HALCON混合编程六之运动目标检测
运动自行车检测引言一、结果1.1 Halcon演示结果1.2 MFC演示结果二、Halcon代码三、MFC源代码1.头文件主要代码2.源文件主要代码引言使用MFC联合Halcon,将HDevelop中的演示结果用MFC中对话框的形式显示一、结果1.1 Halcon演示结果1.2 MFC演示结果二、Halcon代码* This example demonstrates the use of the
·
引言
使用MFC联合Halcon,将HDevelop中的演示结果用MFC中对话框的形式显示
一、结果
1.1 Halcon演示结果
1.2 MFC演示结果
二、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
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')
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
三、MFC源代码
1.头文件主要代码
public:
MyAssist myassist;
HTuple picture1_WindowID,picture2_WindowID;
CWnd *pWnd;
// Local iconic variables
HObject ho_Image1, ho_ROI, ho_RegionROI, ho_Image1ROI;
HObject ho_Image2, ho_Image2ROI, ho_VectorField, ho_LengthImage;
HObject ho_RegionMovement, ho_ConnectedRegions, ho_ConvexHullregion;
HObject ho_RegionMovementInROI, ho_VectorReduced, ho_Row;
HObject ho_Column, ho_Arrow;
// Local control variables
HTuple hv_ZoomFactor, hv_WindowHandle, hv_I;
HTuple hv_Min, hv_Max, hv_Range, hv_Area, hv_RCenterNew;
HTuple hv_CCenterNew, hv_MeanRow, hv_Deviation, hv_MeanColumn;
HTuple hv_Deviation1;
public:
afx_msg void OnBnClickedButtonReadImg();
2.源文件主要代码
void CHalconMFCDlg::OnBnClickedButtonReadImg()
{
// TODO: 在此添加控件通知处理程序代码
CRect rect;
pWnd = GetDlgItem(IDC_STATIC_ORIGINAL_IMG);
picture1_WindowID = (Hlong)pWnd->m_hWnd;
pWnd->GetWindowRect(&rect);
//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
myassist.dev_update_off();
ReadImage(&ho_Image1, "bicycle/bicycle_01");
hv_ZoomFactor =1;
ZoomImageFactor(ho_Image1, &ho_Image1, hv_ZoomFactor, hv_ZoomFactor, "constant");
OpenWindow(0, 0, rect.Width(), rect.Height(), picture1_WindowID, "visible", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
//myassist.dev_open_window_fit_image(ho_Image1, 0, 0, -1, -1, &hv_WindowHandle);
if (HDevWindowStack::IsOpen())
SetDraw(HDevWindowStack::GetActive(), "margin");
//
//Generate ROI
GenContourPolygonXld(&ho_ROI, (((((HTuple(0).Append(0)).Append(283)).Append(348)).Append(479)).Append(479))*hv_ZoomFactor,
(((((HTuple(0).Append(379)).Append(379)).Append(434)).Append(639)).Append(0))*hv_ZoomFactor);
GenRegionContourXld(ho_ROI, &ho_RegionROI, "filled");
ReduceDomain(ho_Image1, ho_RegionROI, &ho_Image1ROI);
//
//Main loop: Calculate optical flow and display moving area
//
for (hv_I = 2; hv_I <= 27; hv_I += 1)
{
ReadImage(&ho_Image2, "bicycle/bicycle_" + (hv_I.TupleString(".2")));
//
//Zoom images to speed-up calculation of optical flow
ZoomImageFactor(ho_Image2, &ho_Image2, hv_ZoomFactor, hv_ZoomFactor, "constant");
ReduceDomain(ho_Image2, ho_RegionROI, &ho_Image2ROI);
//
OpticalFlowMg(ho_Image1ROI, ho_Image2ROI, &ho_VectorField, "fdrig", 0.8, 1, 10,
5, (HTuple("default_parameters").Append("warp_zoom_factor")), (HTuple("fast").Append(0.8)));
VectorFieldLength(ho_VectorField, &ho_LengthImage, "squared_length");
//
//Segment regions with moving objects in the defined ROI
MinMaxGray(ho_RegionROI, ho_LengthImage, 0.1, &hv_Min, &hv_Max, &hv_Range);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image2, HDevWindowStack::GetActive());
if (0 != (hv_Max > 2))
{
Threshold(ho_LengthImage, &ho_RegionMovement, 2, hv_Max);
Connection(ho_RegionMovement, &ho_ConnectedRegions);
//Select largest moving region
SelectShapeStd(ho_ConnectedRegions, &ho_RegionMovement, "max_area", 70);
AreaCenter(ho_RegionMovement, &hv_Area, &hv_RCenterNew, &hv_CCenterNew);
if (0 != (hv_Area > 0))
{
ShapeTrans(ho_RegionMovement, &ho_ConvexHullregion, "convex");
Intersection(ho_RegionROI, ho_ConvexHullregion, &ho_RegionMovementInROI);
ReduceDomain(ho_VectorField, ho_ConvexHullregion, &ho_VectorReduced);
VectorFieldToReal(ho_VectorReduced, &ho_Row, &ho_Column);
//
//Estimate the movement direction and the speed
Intensity(ho_RegionMovementInROI, ho_Row, &hv_MeanRow, &hv_Deviation);
Intensity(ho_RegionMovementInROI, ho_Column, &hv_MeanColumn, &hv_Deviation1);
//
//Display results
if (HDevWindowStack::IsOpen())
SetLineWidth(HDevWindowStack::GetActive(), 1);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "yellow");
if (HDevWindowStack::IsOpen())
DispObj(ho_VectorReduced, HDevWindowStack::GetActive());
//Display region of interest
if (HDevWindowStack::IsOpen())
SetLineWidth(HDevWindowStack::GetActive(), 3);
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "magenta");
if (HDevWindowStack::IsOpen())
DispObj(ho_RegionROI, HDevWindowStack::GetActive());
//Display region of moving object in the region of interest
if (HDevWindowStack::IsOpen())
SetColor(HDevWindowStack::GetActive(), "green");
if (HDevWindowStack::IsOpen())
DispObj(ho_RegionMovementInROI, HDevWindowStack::GetActive());
myassist.gen_arrow_contour_xld(&ho_Arrow, hv_RCenterNew, hv_CCenterNew, hv_RCenterNew + hv_MeanRow,
hv_CCenterNew + hv_MeanColumn, 10, 10);
if (HDevWindowStack::IsOpen())
DispObj(ho_Arrow, HDevWindowStack::GetActive());
}
}
//
CopyObj(ho_Image2ROI, &ho_Image1ROI, 1, 1);
}
}
# 总结
更多推荐
所有评论(0)