// MFC_DialogDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFC_Dialog.h"
#include "MFC_DialogDlg.h"
#include "afxdialogex.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define USE_FACE
#include "NtKinect.h"
using namespace cv;
// CMFCDialogDlg dialog
CMFCDialogDlg::CMFCDialogDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MFC_DIALOG_DIALOG, pParent)
, m_staticStatus(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_STATIC_STATUS, m_staticStatus);
}
BEGIN_MESSAGE_MAP(CMFCDialogDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CMFCDialogDlg::OnBnClickedButtonConnect)
END_MESSAGE_MAP()
// CMFCDialogDlg message handlers
BOOL CMFCDialogDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
HRESULT hTig;
hTig = GetDefaultKinectSensor(&m_sensor);
if (FAILED(hTig))
{
m_staticStatus = "Failed to connect Kinect Sensor";
exit(10);
}
else
{
m_staticStatus = "Kinect Sensor Connected";
}
UpdateData(FALSE);
m_sensor->Open();
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMFCDialogDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCDialogDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCDialogDlg::OnBnClickedButtonConnect()
{
// TODO: Add your control notification handler code here
IDepthFrameSource* mdepthFrameSource;
HRESULT hr;
//Get the frame source
hr = m_sensor->get_DepthFrameSource(&mdepthFrameSource);
if (FAILED(hr))
{
m_staticStatus = "Failed to get frame source from Kinect Sensor";
exit(10);
}
else
{
m_staticStatus = "Accessed the Frame Source from Kinect Sensor";
}
UpdateData(FALSE);
//Get the frame reader
hr = mdepthFrameSource->OpenReader(&m_depthFrameReader);
if (FAILED(hr))
{
m_staticStatus = "Failed to get frame Reader from Kinect Sensor";
exit(10);
}
else
{
m_staticStatus = "Accessed the Frame Reader from Kinect Sensor";
}
UpdateData(FALSE);
NtKinect kinect;
while (1) {
kinect.setRGB();
kinect.setSkeleton();
for (auto person : kinect.skeleton) {
for (auto joint : person) {
if (joint.TrackingState == TrackingState_NotTracked) continue;
ColorSpacePoint cp;
kinect.coordinateMapper->MapCameraPointToColorSpace(joint.Position, &cp);
cv::rectangle(kinect.rgbImage, cv::Rect((int)cp.X - 5, (int)cp.Y - 5, 10, 10), cv::Scalar(0, 0, 255), 2);
}
}
kinect.setFace();
for (cv::Rect r : kinect.faceRect) {
cv::rectangle(kinect.rgbImage, r, cv::Scalar(255, 255, 0), 2);
}
for (vector<PointF> vf : kinect.facePoint) {
for (PointF p : vf) {
cv::rectangle(kinect.rgbImage, cv::Rect((int)p.X - 3, (int)p.Y - 3, 6, 6), cv::Scalar(0, 255, 255), 2);
}
}
cv::imshow("rgb", kinect.rgbImage);
auto key = cv::waitKey(1);
if (key == 'q') break;
}
cv::destroyAllWindows();
}