#include <k4a/k4a.hpp>
#include <fstream>
#include <iostream>
#include <vector>
#include <array>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui_c.h>
#include "Pixel.h"
#include "DepthPixel.h"
#include "ImageProperties.h"
#include<iostream>
#include<WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#define CONNECT_NUM_MAX 10
using namespace cv;
using namespace sen;
//Rect select;
bool mousedown_flag = false; //鼠标按下的标识符
bool select_flag = false; //选择区域的标识符
Point origin;
Mat frame;
int ave_depth(Rect& select, const k4a_image_t& depthImage)
{
int frame_depth = 0;
const int width = k4a_image_get_width_pixels(depthImage);
const int height = k4a_image_get_height_pixels(depthImage);
int bon_w = select.x + select.width;
int bon_h = select.y + select.height;
const uint16_t* depthData = reinterpret_cast<const uint16_t*>(k4a_image_get_buffer(depthImage));
unsigned long sum_depth = 0;
if (select.y != bon_h && select.x != bon_w) {
for (int h = select.y; h <= bon_h; ++h)
{
for (int w = select.x; w <= bon_w; ++w)
{
const size_t currentPixel = static_cast<size_t>(h * width + w);
sum_depth += (unsigned long)depthData[currentPixel];
if (h == bon_h && w == bon_w) {
long points_num = (select.width + 1) * (select.height + 1);
long ave_depth = sum_depth / points_num;
std::cout << ave_depth << std::endl;
frame_depth = ave_depth;
}
}
}
}
return frame_depth;
}
int main(int argc, char** argv)
{
//加载套接字库
WSADATA wsaData;
int iRet = 0;
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0)
{
std::cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << std::endl;;
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
std::cout << "WSADATA version is not correct!" << std::endl;
return -1;
}
//创建套接字
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
std::cout << "serverSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << std::endl;
return -1;
}
//初始化服务器地址族变量
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8888);
//绑定
iRet = ::bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (iRet == SOCKET_ERROR)
{
std::cout << "bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) execute failed!" << std::endl;
return -1;
}
//监听
iRet = listen(serverSocket, CONNECT_NUM_MAX);
if (iRet == SOCKET_ERROR)
{
std::cout << "listen(serverSocket, 10) execute failed!" << std::endl;
return -1;
}
//等待连接_接收_发送
SOCKADDR_IN clientAddr;
int len = sizeof(SOCKADDR);
SOCKET connSocket = accept(serverSocket, (SOCKADDR*)&clientAddr, &len);
if (connSocket == INVALID_SOCKET)
{
std::cout << "accept(serverSocket, (SOCKADDR*)&clientAddr, &len) execute failed!" << std::endl;
return -1;
}
const uint32_t deviceCount = k4a_device_get_installed_count();
if (deviceCount == 0)
{
std::cout << "no azure kinect devices detected!" << std::endl;
}
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.camera_fps = K4A_FRAMES_PER_SECOND_15;
config.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED;
config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
config.color_resolution = K4A_COLOR_RESOLUTION_720P;
config.synchronized_images_only = true;
std::cout << "Started opening K4A device..." << std::endl;
k4a_device_t device = nullptr;
k4a_device_open(0, &device);
k4a_device_start_cameras(device, &config);
std::cout << "Finished opening K4A device." << std::endl;
std::vector<Pixel> depthTextureBuffer;
k4a_capture_t capture;
k4a_image_t depthImage;
cv::Mat depthFrame;
namedWindow("kinect depth map master", 1);
//setMouseCallback("kinect depth map master", onMouse, 0);
//select = Rect(50, 50, 0, 0);
while (1)
{
if (k4a_device_get_capture(device, &capture, K4A_WAIT_INFINITE) == K4A_WAIT_RESULT_SUCCEEDED)
{
{
depthImage = k4a_capture_get_depth_image(capture);
ColorizeDepthImage(depthImage, DepthPixelColorizer::ColorizeBlueToRed, GetDepthModeRange(config.depth_mode), &depthTextureBuffer);
depthFrame = cv::Mat(k4a_image_get_height_pixels(depthImage), k4a_image_get_width_pixels(depthImage), CV_8UC4, depthTextureBuffer.data());
//画出矩形框
Rect new_select = Rect(depthFrame.cols / 3, depthFrame.rows / 3, depthFrame.cols / 4, depthFrame.rows / 4);
rectangle(depthFrame, new_select, Scalar(0, 0, 255), 1, 8, 0);//能够实时显示在画矩形窗口时的痕迹
int frame_depth=ave_depth(new_select, depthImage);//计算框选区域的平均深度
char sendBuf[100];
sprintf_s(sendBuf, " %d", frame_depth);
send(connSocket, sendBuf, strlen(sendBuf) + 1, 0);
char recvBuf[100];
recv(connSocket, recvBuf, 100, 0);
printf("%s\n", recvBuf);
cv::imshow("kinect depth map master", depthFrame);
k4a_image_release(depthImage);
}
}
if (waitKey(30) == 27 || waitKey(30) == 'q')
{
k4a_capture_release(capture);
k4a_device_close(device);
break;
}
}
closesocket(connSocket);
return 0;
}