老师的代码
// Dialog_Test01Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Dialog_Test01.h"
#include "Dialog_Test01Dlg.h"
#include "afxdialogex.h"
#include <iomanip>
#include <time.h>
#include <signal.h>
#pragma warning( disable : 4996 )
#include <opencv2/opencv.hpp>
#include <libfreenect2/libfreenect2.hpp>
#include <libfreenect2/frame_listener_impl.h>
#include <libfreenect2/registration.h>
#include <libfreenect2/packet_pipeline.h>
#include <libfreenect2/logger.h>
#include <mutex>
#include <condition_variable>
using namespace std;
using namespace cv;
using namespace libfreenect2;
//import cv2;
enum
{
Processor_cl,
Processor_gl,
Processor_cpu
};
libfreenect2::Freenect2 freenect2;
bool protonect_shutdown = false; // Whether the running application should shut down.
bool save_images = false; // Flag to indicate when to save images.
bool frames_ready = false; // Flag to indicate that frames from both cameras are ready.
std::mutex frames_mutex;
std::condition_variable frames_cv;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
std::vector<libfreenect2::Freenect2Device*> devices;
std::vector<libfreenect2::PacketPipeline*> pipelines;
std::vector<libfreenect2::SyncMultiFrameListener*> listeners;
std::vector<libfreenect2::FrameMap> frames;
std::string save_directory = "C:\\ImgSave\\";
void sigint_handler(int s)
{
protonect_shutdown = true;
}
std::string getCurrentTimeString() {
time_t now = time(0);
tm *ltm = localtime(&now);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", ltm);
return std::string(buffer);
}
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CDialog_Test01Dlg 对话框
CDialog_Test01Dlg::CDialog_Test01Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG_TEST01_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDialog_Test01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDialog_Test01Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CDialog_Test01Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CDialog_Test01Dlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CDialog_Test01Dlg::OnBnClickedButton3)
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CDialog_Test01Dlg 消息处理程序
BOOL CDialog_Test01Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
//系统初始化
// Search and initialize sensors
int numDevices = freenect2.enumerateDevices();
if (numDevices < 2)
{
//std::cerr << "Error: Not enough Kinect devices connected!" << std::endl;
//MessageBox(L"系统遍历相机失败");
//return -1;
}
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CDialog_Test01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CDialog_Test01Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDialog_Test01Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//相机1打开
void CDialog_Test01Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
string serial = freenect2.getDeviceSerialNumber(0);
libfreenect2::PacketPipeline* pipeline = nullptr;
int depthProcessor = Processor_cl;
if (depthProcessor == Processor_cpu)
{
pipeline = new libfreenect2::CpuPacketPipeline();
}
else if (depthProcessor == Processor_gl) // if support gl
{
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
pipeline = new libfreenect2::OpenGLPacketPipeline();
#else
std::cout << "OpenGL pipeline is not supported!" << std::endl;
#endif
}
else if (depthProcessor == Processor_cl) // if support cl
{
pipeline = new libfreenect2::OpenGLPacketPipeline();
//#ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT
// pipeline = new libfreenect2::OpenCLPacketPipeline();
//#else
// std::cout << "OpenCL pipeline is not supported!" << std::endl;
//#endif
}
if (pipeline)
{
libfreenect2::Freenect2Device* dev = freenect2.openDevice(serial, pipeline);
if (dev == nullptr)
{
//std::cerr << "Failure opening device " << i + 1 << "!" << std::endl;
MessageBox(L"相机打开失败");
return;
}
devices.push_back(dev);
pipelines.push_back(pipeline);
libfreenect2::SyncMultiFrameListener* listener = new libfreenect2::SyncMultiFrameListener(
libfreenect2::Frame::Color |
libfreenect2::Frame::Depth |
libfreenect2::Frame::Ir);
listeners.push_back(listener);
frames.push_back(libfreenect2::FrameMap());
dev->setColorFrameListener(listener);
dev->setIrAndDepthFrameListener(listener);
dev->start();
}
signal(SIGINT, sigint_handler);
protonect_shutdown = false;
// Create windows
cv::namedWindow("rgb1", WINDOW_AUTOSIZE);
cv::namedWindow("rgb2", WND_PROP_ASPECT_RATIO);
cv::namedWindow("ir1", WND_PROP_ASPECT_RATIO);
cv::namedWindow("ir2", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth1", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth2", WND_PROP_ASPECT_RATIO);
while (!protonect_shutdown)
{
for (int i = 0; i < devices.size(); ++i)
{
listeners[i]->waitForNewFrame(frames[i]);
libfreenect2::Frame* rgb = frames[i][libfreenect2::Frame::Color];
libfreenect2::Frame* ir = frames[i][libfreenect2::Frame::Ir];
libfreenect2::Frame* depth = frames[i][libfreenect2::Frame::Depth];
cv::Mat rgbmat((int)rgb->height, (int)rgb->width, CV_8UC4, rgb->data);
cv::Mat irmat((int)ir->height, (int)ir->width, CV_32FC1, ir->data);
cv::Mat depthmat((int)depth->height, (int)depth->width, CV_32FC1, depth->data);
//cv::imshow("rgb" + std::to_string(i + 1), rgbmat);
//cv::imshow("ir" + std::to_string(i + 1), irmat / 4500.0f);
//cv::imshow("depth" + std::to_string(i + 1), depthmat / 4500.0f);
// 获取窗口句柄
//利用GetSafeHwnd()
CRect rect[3];
CWnd *hwnd_rgb = FindWindow(NULL,L"rgb");
CWnd *hwnd_ir = FindWindow(NULL, L"ir");
CWnd *hwnd_depth = FindWindow(NULL, L"depth");
//GetWindowRect(&rect); // 获取当前窗口的位置和大小
if (hwnd_rgb && hwnd_ir && hwnd_depth)
{
GetDlgItem(IDC_VEDIO3)->GetWindowRect(&rect[0]);
::SetParent(hwnd_rgb->m_hWnd, GetDlgItem(IDC_VEDIO5)->m_hWnd); // 设置父窗口。
::SetWindowLong(hwnd_rgb->m_hWnd, GWL_STYLE, WS_CHILD | WS_VISIBLE); //改为子窗口属性,WS_POPUP样式 与 WS_CHILD样式互斥
::SetWindowPos(hwnd_rgb->m_hWnd,GetDlgItem(IDC_VEDIO5)->m_hWnd,rect[0].left, rect[0].top, rect->Width(), rect->Height(), SWP_SHOWWINDOW);
hwnd_rgb->ShowWindow(SW_SHOW);
cv::imshow("rgb", rgbmat);
//hwnd_rgb->MoveWindow(&rect[0]);
GetDlgItem(IDC_VEDIO5)->GetWindowRect(&rect[1]);
hwnd_ir->MoveWindow(&rect[1]);
GetDlgItem(IDC_VEDIO1)->GetWindowRect(&rect[2]);
hwnd_depth->MoveWindow(&rect[2]);
}
// 移动窗口到新位置
// Check for keypress to trigger image saving
int key = cv::waitKey(30);
if (key == 'q')
{
save_images = true; // Set flag to save images on next iteration
}
else if (key == 27) // Escape key
{
protonect_shutdown = true;
}
// Signal that a frame is ready
{
std::lock_guard<std::mutex> lock(frames_mutex);
frames_ready = true;
}
frames_cv.notify_one();
}
// Wait until all frames are ready before proceeding
std::unique_lock<std::mutex> lock(frames_mutex);
frames_cv.wait(lock, [] { return frames_ready; });
// Release frames
// Save images if the flag is set
if (save_images)
{
std::string timestamp = getCurrentTimeString();
for (int i = 0; i < devices.size(); ++i)
{
libfreenect2::Frame* rgb = frames[i][libfreenect2::Frame::Color];
libfreenect2::Frame* ir = frames[i][libfreenect2::Frame::Ir];
libfreenect2::Frame* depth = frames[i][libfreenect2::Frame::Depth];
std::string filename_rgb = save_directory + "rgb" + std::to_string(i + 1) + "_" + timestamp + ".png";
std::string filename_ir = save_directory + "ir" + std::to_string(i + 1) + "_" + timestamp + ".png";
std::string filename_depth = save_directory + "depth" + std::to_string(i + 1) + "_" + timestamp + ".png";
cv::imwrite(filename_rgb, cv::Mat((int)rgb->height, (int)rgb->width, CV_8UC4, rgb->data));
cv::imwrite(filename_ir, cv::Mat((int)ir->height, (int)ir->width, CV_32FC1, ir->data) / 4500.0f * 255);
cv::imwrite(filename_depth, cv::Mat((int)depth->height, (int)depth->width, CV_32FC1, depth->data) / 4500.0f * 255);
std::cout << "Camera " << i + 1 << " saved successfully." << std::endl;
}
save_images = false; // Reset the flag after saving
frames_ready = false; // Reset the flag after saving
}
for (int i = 0; i < devices.size(); ++i)
{
listeners[i]->release(frames[i]);
}
}
}
//相机2打开
void CDialog_Test01Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
cv::namedWindow("view", cv::WINDOW_AUTOSIZE);
HWND hWnd = FindWindow(NULL, L"view")->m_hWnd;
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_VEDIO4)->m_hWnd);
::ShowWindow(hParent, SW_SHOW);
cv::VideoCapture capture(0);//这里是摄像头个数一个一个试 0 1 2,前置摄像头会有一个镜像显示的问题
if (!capture.isOpened())
{
MessageBox(_T("打开摄像头失败"));
EndDialog(IDCANCEL);//把这个窗口也关掉
return ;
}
else
{
capture.open(0);
/*
Mat frame;
while (1)
{
capture >> frame;
if (frame.empty())
break;
imshow("view", frame);
if (waitKey(25) > 0)//按下任意键退出摄像头
break;
}
capture.release();
destroyAllWindows();//关闭所有窗口
*/
}
//capture.release();
/*
cv::Mat frame;
cv::Mat showFrame;
BOOL boob_isShutter = FALSE;//是拍照
BOOL b_takingPhoto = TRUE;//正在拍照
m_strTakePhotoPathName.Empty();
while (TRUE)
{
capture >> frame;//得到摄像头的一帧数据
resize(frame, showFrame, cv::Size(picRect.Width(), picRect.Height()));//设置这一帧图片的大小是pic控件的大小
imshow("view", showFrame);//显示
cv::waitKey(33);//设置帧率是30帧左右
if (!b_takingPhoto)
{
if (b_isShutter)
{
CString strPath, strName, strPathName;
strPath = //算一下路径
if (strPath.IsEmpty())
{
return FALSE;
}
strName = _T("\\") + m_strPicName + _T(".jpg");
strPathName = strPath + strName;
char chPathName[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, LPCTSTR(strPathName), -1, chPathName, MAX_PATH, NULL, NULL);
//保存这一帧图片
BOOL ret;
ret = imwrite(chPathName, frame);
if (!ret)
{
MessageBox(_T("照片保存失败"));
}
else
{
m_strTakePhotoPathName = strPathName;//图片的路径和名字
}
}
break;
}
}
frame.release();
showFrame.release();
//EndDialog(IDCANCEL);//关闭窗口
*/
// return ; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDialog_Test01Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
save_images = true;
}
void CDialog_Test01Dlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
// Close devices
for (auto dev : devices)
{
dev->stop();
dev->close();
}
// Clean up resources
for (auto pipeline : pipelines)
{
delete pipeline;
}
for (auto listener : listeners)
{
delete listener;
}
CDialogEx::OnClose();
}
void CAboutDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnTimer(nIDEvent);
}
我的代码
// Dialog_Test01Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Dialog_Test01.h"
#include "Dialog_Test01Dlg.h"
#include "afxdialogex.h"
#include <iomanip>
#include <time.h>
#include <signal.h>
#pragma warning( disable : 4996 )
#include <opencv2/opencv.hpp>
#include <libfreenect2/libfreenect2.hpp>
#include <libfreenect2/frame_listener_impl.h>
#include <libfreenect2/registration.h>
#include <libfreenect2/packet_pipeline.h>
#include <libfreenect2/logger.h>
#include <mutex>
#include <condition_variable>
using namespace std;
using namespace cv;
using namespace libfreenect2;
//import cv2;
enum
{
Processor_cl,
Processor_gl,
Processor_cpu
};
libfreenect2::Freenect2 freenect2;
bool protonect_shutdown = false; // 是否应该关闭运行中的应用。
bool save_images = false; // 表示何时保存图像的标志。
bool frames_ready = false; // 表示来自两个相机的帧已准备好的标志。
std::mutex frames_mutex;
std::condition_variable frames_cv;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
std::vector<libfreenect2::Freenect2Device*> devices;
std::vector<libfreenect2::PacketPipeline*> pipelines;
std::vector<libfreenect2::SyncMultiFrameListener*> listeners;
std::vector<libfreenect2::FrameMap> frames;
std::string save_directory = "C:\\ImgSave\\";
void sigint_handler(int s)
{
protonect_shutdown = true;
}
std::string getCurrentTimeString() {
time_t now = time(0);
tm *ltm = localtime(&now);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", ltm);
return std::string(buffer);
}
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CDialog_Test01Dlg 对话框
CDialog_Test01Dlg::CDialog_Test01Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG_TEST01_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDialog_Test01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDialog_Test01Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CDialog_Test01Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CDialog_Test01Dlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CDialog_Test01Dlg::OnBnClickedButton3)
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CDialog_Test01Dlg 消息处理程序
BOOL CDialog_Test01Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
//系统初始化
// Search and initialize sensors
int numDevices = freenect2.enumerateDevices();
if (numDevices < 2)
{
//std::cerr << "Error: Not enough Kinect devices connected!" << std::endl;
//MessageBox(L"系统遍历相机失败");
//return -1;
}
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CDialog_Test01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CDialog_Test01Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDialog_Test01Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//相机1打开
void CDialog_Test01Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
string serial = freenect2.getDeviceSerialNumber(0);
libfreenect2::PacketPipeline* pipeline = nullptr;
int depthProcessor = Processor_cl;
if (depthProcessor == Processor_cpu)
{
pipeline = new libfreenect2::CpuPacketPipeline();
}
else if (depthProcessor == Processor_gl) // if support gl
{
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
pipeline = new libfreenect2::OpenGLPacketPipeline();
#else
std::cout << "OpenGL pipeline is not supported!" << std::endl;
#endif
}
else if (depthProcessor == Processor_cl) // if support cl
{
pipeline = new libfreenect2::OpenGLPacketPipeline();
//#ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT
// pipeline = new libfreenect2::OpenCLPacketPipeline();
//#else
// std::cout << "OpenCL pipeline is not supported!" << std::endl;
//#endif
}
if (pipeline)
{
libfreenect2::Freenect2Device* dev = freenect2.openDevice(serial, pipeline);
if (dev == nullptr)
{
//std::cerr << "Failure opening device " << i + 1 << "!" << std::endl;
MessageBox(L"相机打开失败");
return;
}
devices.push_back(dev);
pipelines.push_back(pipeline);
libfreenect2::SyncMultiFrameListener* listener = new libfreenect2::SyncMultiFrameListener(
libfreenect2::Frame::Color |
libfreenect2::Frame::Depth |
libfreenect2::Frame::Ir);
listeners.push_back(listener);
frames.push_back(libfreenect2::FrameMap());
dev->setColorFrameListener(listener);
dev->setIrAndDepthFrameListener(listener);
dev->start();
}
signal(SIGINT, sigint_handler);
protonect_shutdown = false;
// 创建窗口
cv::namedWindow("rgb1", WINDOW_AUTOSIZE);
cv::namedWindow("rgb2", WND_PROP_ASPECT_RATIO);
cv::namedWindow("ir1", WND_PROP_ASPECT_RATIO);
cv::namedWindow("ir2", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth1", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth2", WND_PROP_ASPECT_RATIO);
while (!protonect_shutdown)
{
for (int i = 0; i < devices.size(); ++i)
{
listeners[i]->waitForNewFrame(frames[i]);
libfreenect2::Frame* rgb = frames[i][libfreenect2::Frame::Color];
libfreenect2::Frame* ir = frames[i][libfreenect2::Frame::Ir];
libfreenect2::Frame* depth = frames[i][libfreenect2::Frame::Depth];
cv::Mat rgbmat((int)rgb->height, (int)rgb->width, CV_8UC4, rgb->data);
cv::Mat irmat((int)ir->height, (int)ir->width, CV_32FC1, ir->data);
cv::Mat depthmat((int)depth->height, (int)depth->width, CV_32FC1, depth->data);
// 获取窗口句柄
CWnd *hwnd_rgb = FindWindow(NULL, L"rgb1");
CWnd *hwnd_ir = FindWindow(NULL, L"ir1");
CWnd *hwnd_depth = FindWindow(NULL, L"depth1");
// 设置父窗口并调整样式
if (hwnd_rgb && hwnd_ir && hwnd_depth)
{
::SetParent(hwnd_rgb->m_hWnd, GetDlgItem(IDC_VEDIO3)->m_hWnd);
::SetWindowLong(hwnd_rgb->m_hWnd, GWL_STYLE, WS_CHILD | WS_VISIBLE);
::SetWindowPos(hwnd_rgb->m_hWnd, GetDlgItem(IDC_VEDIO3)->m_hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
::SetParent(hwnd_ir->m_hWnd, GetDlgItem(IDC_VEDIO5)->m_hWnd);
::SetWindowLong(hwnd_ir->m_hWnd, GWL_STYLE, WS_CHILD | WS_VISIBLE);
::SetWindowPos(hwnd_ir->m_hWnd, GetDlgItem(IDC_VEDIO5)->m_hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
::SetParent(hwnd_depth->m_hWnd, GetDlgItem(IDC_VEDIO1)->m_hWnd);
::SetWindowLong(hwnd_depth->m_hWnd, GWL_STYLE, WS_CHILD | WS_VISIBLE);
::SetWindowPos(hwnd_depth->m_hWnd, GetDlgItem(IDC_VEDIO1)->m_hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
}
// 显示图像
cv::imshow("rgb1", rgbmat);
cv::imshow("ir1", irmat / 4500.0f);
cv::imshow("depth1", depthmat / 4500.0f);
// 检查按键以触发图像保存
int key = cv::waitKey(30);
if (key == 'q')
{
save_images = true; // 设置标志以在下一迭代时保存图像
}
else if (key == 27) // Esc键
{
protonect_shutdown = true;
}
// 信号表示一帧已准备好
{
std::lock_guard<std::mutex> lock(frames_mutex);
frames_ready = true;
}
frames_cv.notify_one();
}
// 等待直到所有帧都准备好才继续
std::unique_lock<std::mutex> lock(frames_mutex);
frames_cv.wait(lock, [] { return frames_ready; });
// 保存图像如果设置了标志
if (save_images)
{
std::string timestamp = getCurrentTimeString();
for (int i = 0; i < devices.size(); ++i)
{
libfreenect2::Frame* rgb = frames[i][libfreenect2::Frame::Color];
libfreenect2::Frame* ir = frames[i][libfreenect2::Frame::Ir];
libfreenect2::Frame* depth = frames[i][libfreenect2::Frame::Depth];
std::string filename_rgb = save_directory + "rgb" + std::to_string(i + 1) + "_" + timestamp + ".png";
std::string filename_ir = save_directory + "ir" + std::to_string(i + 1) + "_" + timestamp + ".png";
std::string filename_depth = save_directory + "depth" + std::to_string(i + 1) + "_" + timestamp + ".png";
cv::imwrite(filename_rgb, cv::Mat((int)rgb->height, (int)rgb->width, CV_8UC4, rgb->data));
cv::imwrite(filename_ir, cv::Mat((int)ir->height, (int)ir->width, CV_32FC1, ir->data) / 4500.0f * 255);
cv::imwrite(filename_depth, cv::Mat((int)depth->height, (int)depth->width, CV_32FC1, depth->data) / 4500.0f * 255);
std::cout << "Camera " << i + 1 << " saved successfully." << std::endl;
}
save_images = false; // 保存后重置标志
frames_ready = false; // 保存后重置标志
}
// 释放帧
for (int i = 0; i < devices.size(); ++i)
{
listeners[i]->release(frames[i]);
}
}
}
//相机2打开
void CDialog_Test01Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
// 这里可以添加打开第二个相机的代码
}
void CDialog_Test01Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
save_images = true;
}
void CDialog_Test01Dlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
// 关闭设备
for (auto dev : devices)
{
dev->stop();
dev->close();
}
// 清理资源
for (auto pipeline : pipelines)
{
delete pipeline;
}
for (auto listener : listeners)
{
delete listener;
}
CDialogEx::OnClose();
}
void CAboutDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnTimer(nIDEvent);
}
需要学习的代码
// OpenCV_Test_01Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "OpenCV_Test_01.h"
#include "OpenCV_Test_01Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BOOL m_bRun;//线程是否正常运行
UINT ThreadProc(LPVOID pParam) //线程函数
{
COpenCV_Test_01Dlg* pDlg = (COpenCV_Test_01Dlg*)pParam;
while (pDlg->m_bRun)
{
// 打开视频捕获设备
pDlg->capture.open(0); // 0 是默认摄像头
if (!pDlg->capture.isOpened())
{
AfxMessageBox(_T("无法打开摄像头"));
return 1;
}
pDlg->flg = TRUE;
namedWindow("newWindows", WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle("newWindows");
::SetParent(hWnd, pDlg->GetDlgItem(IDC_VIDEO)->m_hWnd);
CRect rect;
pDlg->GetDlgItem(IDC_VIDEO)->GetWindowRect(&rect);
::SetWindowPos(hWnd, HWND_BOTTOM, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
Mat frame;
while (pDlg->flg)
{
pDlg->capture >> frame;
if (frame.empty())
{
AfxMessageBox(L"视频为空!");
break;
}
if (pDlg->SaveImg)//抓图
{
//保存未调整大小的原始图像
cv::imwrite(pDlg->ImgPath + pDlg->GetSysTime() + ".jpg", frame);
pDlg->SaveImg = FALSE;
}
//调整帧图像大小,适合控件的大小
cv::Mat resizedImage;
cv::resize(frame, resizedImage, cv::Size(rect.Width(), rect.Height()), cv::INTER_LINEAR);
imshow("newWindows", resizedImage);
//隐藏OpenCV窗口,但一直不起作用……,再研究
//HWND cvhWnd = ::FindWindow(NULL,_T("newWindows"));
//if(cvhWnd !=NULL)
//{
// //::ShowWindow(cvhWnd, SW_HIDE);
// ::SetWindowPos(cvhWnd, HWND_BOTTOM, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
//}
if ((char)waitKey(10) >= 0)
{
break;//按任意键推出
}
}
pDlg->capture.release();
destroyAllWindows();
}
return 0;
}
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// COpenCV_Test_01Dlg 对话框
COpenCV_Test_01Dlg::COpenCV_Test_01Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_OPENCV_TEST_01_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void COpenCV_Test_01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(COpenCV_Test_01Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &COpenCV_Test_01Dlg::OnBnClickedButton1)
ON_WM_TIMER()
ON_BN_CLICKED(IDCANCEL, &COpenCV_Test_01Dlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_BUTTON3, &COpenCV_Test_01Dlg::OnBnClickedButton3)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON2, &COpenCV_Test_01Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// COpenCV_Test_01Dlg 消息处理程序
BOOL COpenCV_Test_01Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
SaveImg = FALSE;
ImgPath = "c:\\img\\";
m_bRun = FALSE;
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void COpenCV_Test_01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void COpenCV_Test_01Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR COpenCV_Test_01Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//打开相机
void COpenCV_Test_01Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
flg = TRUE;
m_bRun = TRUE;
pThread = AfxBeginThread((AFX_THREADPROC)ThreadProc, this);
}
void COpenCV_Test_01Dlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnTimer(nIDEvent);
}
void COpenCV_Test_01Dlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnCancel();
}
void COpenCV_Test_01Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
m_bRun = FALSE;
flg = FALSE;
}
void COpenCV_Test_01Dlg::OnDestroy()
{
CDialogEx::OnDestroy();
//capture.release();
//destroyAllWindows();
// TODO: 在此处添加消息处理程序代码
}
void COpenCV_Test_01Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
SaveImg = TRUE;
}
// CString转std::string
std::string CStringToStdString(const CString& cstr) {
return std::string(static_cast<LPCSTR>(CT2A(cstr.GetString())));
}
string COpenCV_Test_01Dlg::GetSysTime()
{
CTime t = CTime::GetCurrentTime();
CString strTime;
strTime.Format(_T("%04d%02d%02d%02d%02d%02d"), t.GetYear(), t.GetMonth(), t.GetDay(), t.GetHour(), t.GetMinute(), t.GetSecond());
return CStringToStdString(strTime); ;
}