引言

使用MFC联合Halcon,将HDevelop中的演示结果用MFC中对话框的形式显示


一、结果

1.1 Halcon演示结果

在这里插入图片描述

1.2 MFC演示结果

在这里插入图片描述


二、Halcon代码

* This example program shows how to use optical_flow_mg to compute the
* optical flow in an image sequence and how to segment the optical flow.
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

三、MFC源代码

在这里插入图片描述

1.头文件主要代码

public:
	MyAssist myassist;
	HTuple picture1_WindowID;
	// Local iconic variables
	HObject  ho_Image1, ho_Image2, ho_VectorField;
	HObject  ho_Region;
	// Local control variables
	HTuple  hv_WindowHandle, hv_I;
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 program shows how to use optical_flow_mg to compute the
  //optical flow in an image sequence and how to segment the optical flow.
	myassist.dev_update_off();
	//Initialize the image sequence.
	ReadImage(&ho_Image1, "xing/xing000");
	//myassist.dev_open_window_fit_image(ho_Image1, 0, 0, -1, -1, &hv_WindowHandle);
	//Set the display parameters for the vector field.
	if (HDevWindowStack::IsOpen())
		SetPaint(HDevWindowStack::GetActive(), (((HTuple("vector_field").Append(6)).Append(1)).Append(2)));
	if (HDevWindowStack::IsOpen())
		SetDraw(HDevWindowStack::GetActive(), "margin");
	
	OpenWindow(0, 0, rect.Width(), rect.Height(), picture1_WindowID, "visible", "", &hv_WindowHandle);
	HDevWindowStack::Push(hv_WindowHandle);
		DispObj(ho_Image1, HDevWindowStack::GetActive());
		for (hv_I = 1; hv_I <= 587; hv_I += 1)
		{
			//Read the current image of the image sequence.
			ReadImage(&ho_Image2, "xing/xing" + (hv_I.TupleString("03")));
			//Compute the optical flow.
			OpticalFlowMg(ho_Image1, ho_Image2, &ho_VectorField, "fdrig", 0.8, 1, 8, 5, "default_parameters",
				"accurate");
			//Segment the optical flow vector field.
			Threshold(ho_VectorField, &ho_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.
			if (HDevWindowStack::IsOpen())
				DispObj(ho_Image2, HDevWindowStack::GetActive());
			//Display the optical flow.
			if (HDevWindowStack::IsOpen())
				SetColor(HDevWindowStack::GetActive(), "yellow");
			if (HDevWindowStack::IsOpen())
				SetLineWidth(HDevWindowStack::GetActive(), 1);
			if (HDevWindowStack::IsOpen())
				DispObj(ho_VectorField, HDevWindowStack::GetActive());
			//Display the segmented optical flow.
			if (HDevWindowStack::IsOpen())
				SetColor(HDevWindowStack::GetActive(), "green");
			if (HDevWindowStack::IsOpen())
				SetLineWidth(HDevWindowStack::GetActive(), 3);
			if (HDevWindowStack::IsOpen())
				DispObj(ho_Region, HDevWindowStack::GetActive());
			//Copy the current image to the previous image of the sequence.
			CopyObj(ho_Image2, &ho_Image1, 1, 1);
		}
}

总结

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐