halcon第十三讲:halcon联合vc实时人脸跟踪

1、在halcon中完成人脸实时跟踪程序开发,程序如下,将其导出为c++格式;(这里用到的原理是基于相关性的模板匹配,在我的halcon第十二讲博客中有所讲解)

open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] Lenovo EasyCamera', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
grab_image_async (Image, AcqHandle, -1)

get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Width/2, 'black', WindowHandle)
dev_display (Image)
*创建模板
disp_message (WindowHandle, '请创建模板并以鼠标右击结束', 'window', -1, -1, 'white', 'false')
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
area_center (Rectangle, Area, Row, Column)
orientation_region (Rectangle, Phi)
reduce_domain (Image, Rectangle, ImageReduced)
create_ncc_model (ImageReduced, 'auto', -0.39, 0.79, 'auto', 'use_polarity', ModelID)
dev_set_draw ('margin')
dev_set_line_width (4)
while (true)
    grab_image_async (Image, AcqHandle, -1)
      *寻找模板
    find_ncc_model (Image, ModelID, -0.39, 0.78, 0.4, 1, 0.5, 'true', 0, Row3, Column3, Angle, Score)
    vector_angle_to_rigid (Row, Column, Phi, Row3, Column3, Angle, HomMat2D)
    affine_trans_region (Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
    dev_display (Image)
    dev_display (RegionAffineTrans)
endwhile
close_framegrabber (AcqHandle)


 

2、新建MFC工程,下一步基于对话框,完成项目创建;

3、配置halcon环境,在我的halcon第八讲博客中有所讲解;

4、为对话框添加控件;

4、将导出的c++程序中的显示函数和action中的变量定义拷贝到按钮的消息响应函数的开头部分当作全局变量;

5、将相机初始化即while前面的程序拷贝到“人脸采集“按钮的消息响应函数中;

6、在halcon中实时采集是利用while循环实现的,在vc中可以用定时器,右击整个对话框属性,找到消息中的WM_TIMER,添加定时器,将while循环里的实时采集程序拷贝到定时器的消息响应函数中;

7、在“人脸跟踪“按钮的消息响应函数中启动定时器,这里设置启动后每隔40ms跳到定时器函数中进行采集跟踪;

8、在“关闭采集”按钮的消息响应函数中杀死定时器。

人脸采集按钮消息响应函数:

//【1】相机初始化,采集图像、创建模板
void CMFCApplication1Dlg::OnBnClickedButton4()
{
	HWND hwnd = GetDlgItem(PIC)->m_hWnd;
	LONG windowID = (LONG)hwnd;
	CRect rect;
	GetDlgItem(PIC)->GetWindowRect(&rect);
	kuan[0] = rect.Width();
	gao[0] = rect.Height();

	OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false",
		"default", "[0] Lenovo EasyCamera", 0, -1, &hv_AcqHandle);
	GrabImageStart(hv_AcqHandle, -1);
	GrabImageAsync(&ho_Image, hv_AcqHandle, -1);

	GetImageSize(ho_Image, &hv_Width, &hv_Height);
	SetWindowAttr("background_color", "black");
	OpenWindow(0, 0, kuan[0], gao[0], windowID, "", "", &hv_WindowHandle);
	HDevWindowStack::Push(hv_WindowHandle);
	if (HDevWindowStack::IsOpen())
		DispObj(ho_Image, HDevWindowStack::GetActive());
	//创建模板
	disp_message(hv_WindowHandle, "请创建感兴趣区域,鼠标右击结束", "window", -1, -1, "white",
		"false");
	DrawRectangle1(hv_WindowHandle, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);
	GenRectangle1(&ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
	AreaCenter(ho_Rectangle, &hv_Area, &hv_Row, &hv_Column);
	OrientationRegion(ho_Rectangle, &hv_Phi);
	ReduceDomain(ho_Image, ho_Rectangle, &ho_ImageReduced);
	CreateNccModel(ho_ImageReduced, "auto", -0.39, 0.79, "auto", "use_polarity", &hv_ModelID);
	if (HDevWindowStack::IsOpen())
		SetDraw(HDevWindowStack::GetActive(), "margin");
	if (HDevWindowStack::IsOpen())
		SetLineWidth(HDevWindowStack::GetActive(), 4);
}

定时器消息响应函数: 

//【2】在整个对话框右键属性的消息中添加定时器
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
	GrabImageAsync(&ho_Image, hv_AcqHandle, -1);
	//寻找模板
	FindNccModel(ho_Image, hv_ModelID, -0.39, 0.78, 0.4, 1, 0.5, "true", 0, &hv_Row3,
		&hv_Column3, &hv_Angle, &hv_Score);
	VectorAngleToRigid(hv_Row, hv_Column, hv_Phi, hv_Row3, hv_Column3, hv_Angle,
		&hv_HomMat2D);
	AffineTransRegion(ho_Rectangle, &ho_RegionAffineTrans, hv_HomMat2D, "nearest_neighbor");
	if (HDevWindowStack::IsOpen())
		DispObj(ho_Image, HDevWindowStack::GetActive());
	if (HDevWindowStack::IsOpen())
		DispObj(ho_RegionAffineTrans, HDevWindowStack::GetActive());
	CDialogEx::OnTimer(nIDEvent);
}

人脸跟踪按钮消息响应函数: 

//【3】启动定时器,跳转到上面OnTimer函数
void CMFCApplication1Dlg::OnBnClickedButton2()
{
	//设置定时器,第一个参数为定时器标号,第二为隔40ms发一个消息,第三为缺省参数
	SetTimer(1, 40, NULL);
}

关闭采集按钮消息响应函数:

//【4】关闭定时器
void CMFCApplication1Dlg::OnBnClickedButton3()
{
	KillTimer(1);
}

运行结果如下:

halcon第十三讲:halcon联合vc实时人脸跟踪_第1张图片

点击采集人脸按钮如下:

halcon第十三讲:halcon联合vc实时人脸跟踪_第2张图片

点击人脸跟踪按钮截取两个片段如下:

halcon第十三讲:halcon联合vc实时人脸跟踪_第3张图片

halcon第十三讲:halcon联合vc实时人脸跟踪_第4张图片

最后,针对双击控件,弹出重载函数***已存在的问题,解决办法如下:

1、删除cpp文件下的BEING_MESSENGE_MAP中的相关信息和void相关信息;

2、删除头文件中的public下面的相关信息。

 

你可能感兴趣的:(halcon)