├── Protonect.cpp └── README.md /Protonect.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "examples\Protonect.h" 9 | 10 | /// [headers] 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | /// [headers] 18 | #ifdef EXAMPLES_WITH_OPENGL_SUPPORT 19 | #include "viewer.h" 20 | #endif 21 | 22 | using namespace std; 23 | using namespace cv; 24 | 25 | bool protonect_shutdown = false; ///< Whether the running application should shut down. 26 | void Bitmap1(unsigned char * pData, int width, int height, const char * filename); 27 | 28 | void Bitmap1(unsigned char * pData, int width, int height, const char * filename) 29 | { 30 | int size = width*height * 4; // 每个像素点4个字节 31 | 32 | // 位图第一部分,文件信息 33 | BITMAPFILEHEADER bfh; 34 | bfh.bfType = 0X4d42; //bm 35 | bfh.bfSize = size // 数据大小 36 | + sizeof(BITMAPFILEHEADER) // first section size 37 | + sizeof(BITMAPINFOHEADER) // second section size 38 | ; 39 | bfh.bfReserved1 = 0; // reserved 40 | bfh.bfReserved2 = 0; // reserved 41 | bfh.bfOffBits = bfh.bfSize - size; 42 | 43 | // 位图第二部分,数据信息 44 | BITMAPINFOHEADER bih; 45 | bih.biSize = sizeof(BITMAPINFOHEADER); 46 | bih.biWidth = width; 47 | bih.biHeight = -height; 48 | bih.biPlanes = 1; 49 | bih.biBitCount = 32; 50 | bih.biCompression = 0; 51 | bih.biSizeImage = size; 52 | bih.biXPelsPerMeter = 0; 53 | bih.biYPelsPerMeter = 0; 54 | bih.biClrUsed = 0; 55 | bih.biClrImportant = 0; 56 | 57 | FILE * fp = fopen(filename, "wb"); 58 | if (!fp) return; 59 | 60 | fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp); 61 | fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp); 62 | fwrite(pData, 1, size, fp); 63 | fclose(fp); 64 | } 65 | 66 | void sigint_handler(int s) 67 | { 68 | protonect_shutdown = true; 69 | } 70 | 71 | bool protonect_paused = false; 72 | bool protonect_paused1 = false; 73 | libfreenect2::Freenect2Device *devtopause; 74 | libfreenect2::Freenect2Device *devtopause1; 75 | libfreenect2::Freenect2 freenect2; 76 | libfreenect2::PacketPipeline *pipeline = 0; 77 | std::string serial = ""; 78 | 79 | bool viewer_enabled = true; 80 | bool enable_rgb = true; 81 | bool enable_depth = true; 82 | int deviceId = -1; 83 | size_t framemax = -1; 84 | libfreenect2::Freenect2Device *dev = 0; 85 | libfreenect2::Freenect2Device *dev1 = 0; 86 | libfreenect2::Freenect2Device *dev2 = 0; 87 | libfreenect2::Freenect2Device *dev3 = 0; 88 | libfreenect2::Freenect2Device *dev4 = 0; 89 | //Doing non-trivial things in signal handler is bad. If you want to pause, 90 | //do it in another thread. 91 | //Though libusb operations are generally thread safe, I cannot guarantee 92 | //everything above is thread safe when calling start()/stop() while 93 | //waitForNewFrame(). 94 | void sigusr1_handler(int s) 95 | { 96 | if (devtopause == 0) 97 | return; 98 | /// [pause] 99 | if (protonect_paused) 100 | devtopause->start(); 101 | else 102 | devtopause->stop(); 103 | protonect_paused = !protonect_paused; 104 | /// [pause] 105 | } 106 | void sigusr1_handler1(int s) 107 | { 108 | if (devtopause1 == 0) 109 | return; 110 | /// [pause] 111 | if (protonect_paused1) 112 | devtopause1->start(); 113 | else 114 | devtopause1->stop(); 115 | protonect_paused1 = !protonect_paused1; 116 | /// [pause] 117 | } 118 | //The following demostrates how to create a custom logger 119 | /// [logger] 120 | 121 | #include 122 | class MyFileLogger : public libfreenect2::Logger 123 | { 124 | private: 125 | std::ofstream logfile_; 126 | public: 127 | MyFileLogger(const char *filename) 128 | { 129 | if (filename) 130 | logfile_.open(filename); 131 | level_ = Debug; 132 | } 133 | bool good() 134 | { 135 | return logfile_.is_open() && logfile_.good(); 136 | } 137 | virtual void log(Level level, const std::string &message) 138 | { 139 | logfile_ << "[" << libfreenect2::Logger::level2str(level) << "] " << message << std::endl; 140 | } 141 | }; 142 | int thread0() 143 | { 144 | libfreenect2::PacketPipeline *pipeline = 0; 145 | std::string serial1 = ""; 146 | /*FILE *fp; 147 | char dpath[] = ""; 148 | char cpath[] = "";*/ 149 | structcd cd0; 150 | //list listcd0; 151 | bool viewer_enabled1 = true; 152 | bool enable_rgb1 = true; 153 | bool enable_depth1 = true; 154 | int deviceId1 = -1; 155 | size_t framemax1 = -1; 156 | if (!enable_rgb1 && !enable_depth1) 157 | { 158 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 159 | return -1; 160 | } 161 | dev1 = freenect2.openDevice(0); 162 | if (dev1 == 0) 163 | { 164 | std::cout << "failure opening device!" << std::endl; 165 | return -1; 166 | } 167 | 168 | devtopause1 = dev1; 169 | 170 | signal(SIGINT, sigint_handler); 171 | #ifdef SIGUSR1 172 | signal(SIGUSR1, sigusr1_handler1); 173 | #endif 174 | protonect_shutdown = false; 175 | 176 | /// [listeners] 177 | int types = 0; 178 | if (enable_rgb1) 179 | types |= libfreenect2::Frame::Color; 180 | if (enable_depth1) 181 | types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; 182 | libfreenect2::SyncMultiFrameListener listener(types); 183 | libfreenect2::FrameMap frames; 184 | 185 | dev1->setColorFrameListener(&listener); 186 | dev1->setIrAndDepthFrameListener(&listener); 187 | /// [listeners] 188 | 189 | /// [start] 190 | if (enable_rgb1 && enable_depth1) 191 | { 192 | if (!dev1->start()) 193 | return -1; 194 | } 195 | else 196 | { 197 | if (!dev1->startStreams(enable_rgb1, enable_depth1)) 198 | return -1; 199 | } 200 | 201 | std::cout << "device serial: " << dev1->getSerialNumber() << std::endl; 202 | std::cout << "device firmware: " << dev1->getFirmwareVersion() << std::endl; 203 | /// [start] 204 | 205 | /// [registration setup] 206 | libfreenect2::Registration* registration = new libfreenect2::Registration(dev1->getIrCameraParams(), dev1->getColorCameraParams()); 207 | libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); 208 | /// [registration setup] 209 | 210 | size_t framecount = 0; 211 | while (!protonect_shutdown && (framemax1 == (size_t)-1 || framecount < framemax1)) 212 | { 213 | Sleep(1000); 214 | if (!listener.waitForNewFrame(frames, 10 * 1000)) // 10 sconds 215 | { 216 | std::cout << "timeout!" << std::endl; 217 | return -1; 218 | } 219 | libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; 220 | libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; 221 | libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; 222 | /// [loop start] 223 | 224 | if (enable_rgb1 && enable_depth1) 225 | { 226 | /// [registration] 227 | registration->apply(rgb, depth, &undistorted, ®istered); 228 | const float* depth_data = (float*)depth->data; 229 | cd0.data = new unsigned char[1920 * 1080 * 4]; 230 | cd0.depth_data = new float[512 * 424 * 4]; 231 | 232 | memcpy(cd0.data, rgb->data, 1920 * 1080 * 4); 233 | memcpy(cd0.depth_data, depth_data, 512 * 424 * 4); 234 | 235 | mtx0.lock(); 236 | listcd0.push_back(cd0); 237 | mtx0.unlock(); 238 | std::cout << "Depth information 0-------0:" << depth_data[10000] << std::endl; 239 | } 240 | 241 | framecount++; 242 | if (!viewer_enabled1) 243 | { 244 | if (framecount % 100 == 0) 245 | std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; 246 | listener.release(frames); 247 | continue; 248 | } 249 | listener.release(frames); 250 | } 251 | dev1->stop(); 252 | dev1->close(); 253 | delete registration; 254 | 255 | return 0; 256 | } 257 | int thread0_si() 258 | { 259 | FILE *fp; 260 | char dpath[MAX_PATH] = ""; 261 | char cpath[MAX_PATH] = ""; 262 | char wpath[MAX_PATH] = ""; 263 | structcd scd0; 264 | structdep de0; 265 | while (1) 266 | { 267 | Sleep(10); 268 | if (listcd0.size() > 0) 269 | { 270 | mtx0.lock(); 271 | scd0.data = listcd0.front().data; 272 | scd0.depth_data = listcd0.front().depth_data; 273 | listcd0.pop_front(); 274 | mtx0.unlock(); 275 | SYSTEMTIME st; 276 | GetLocalTime(&st); 277 | sprintf(dpath, "D:\\0\\%04d%02d%02d-%02d%02d%02d%03d.data", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 278 | sprintf(cpath, "D:\\0\\%04d%02d%02d-%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 279 | sprintf(wpath, "D:\\0_jpg\\%04d%02d%02d-%02d%02d%02d%03d.jpg", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 280 | fp = fopen(dpath, "wb"); 281 | fwrite(scd0.depth_data, 512 * 424 * 4, 1, fp); 282 | fclose(fp); 283 | fp = NULL; 284 | Bitmap1(scd0.data, 1920, 1080, cpath); 285 | 286 | delete[] scd0.data; 287 | delete[] scd0.depth_data; 288 | strcpy(de0.cname, cpath); 289 | strcpy(de0.dname, dpath); 290 | strcpy(de0.wname, wpath); 291 | 292 | mtx0_0.lock(); 293 | listdep0.push_back(de0); 294 | mtx0_0.unlock(); 295 | } 296 | 297 | } 298 | return 0; 299 | 300 | 301 | } 302 | int thread0_jd() 303 | { 304 | structdep dep0; 305 | FILE *fp; 306 | float*Data = new float[424 * 512 * 4]; 307 | if (NULL != Data) 308 | { 309 | while (1) 310 | { 311 | if (!listdep0.empty()) 312 | { 313 | strcpy(dep0.cname, "xx"); 314 | strcpy(dep0.dname, "xx"); 315 | strcpy(dep0.wname, "xx"); 316 | mtx0_0.lock(); 317 | strcpy(dep0.cname, listdep0.front().cname); 318 | strcpy(dep0.dname, listdep0.front().dname); 319 | strcpy(dep0.wname, listdep0.front().wname); 320 | listdep0.pop_front(); 321 | mtx0_0.unlock(); 322 | fp = fopen(dep0.dname, "rb"); 323 | if (fp == NULL) 324 | { 325 | cout << dep0.dname << "xiancheng3 File open fail....."; 326 | } 327 | else 328 | { 329 | fread(Data, 424 * 512 * 4, 1, fp); 330 | fclose(fp); 331 | fp = NULL; 332 | int index = 0; 333 | for (int i = 16386; i < 1920*1080 - 16385; i++) 334 | { 335 | if (Data[i] < 1200 && Data[i] >0) 336 | { 337 | index++; 338 | } 339 | if (index > 25) 340 | { 341 | mtx0_1.lock(); 342 | listdep0_0.push_back(dep0); 343 | mtx0_1.unlock(); 344 | break; 345 | } 346 | } 347 | } 348 | } 349 | Sleep(1); 350 | } 351 | delete[] Data; 352 | Data = NULL; 353 | } 354 | return 0; 355 | } 356 | int thread0_lc() 357 | { 358 | FILE *fp; 359 | structdep dep0; 360 | UINT16* Ddata = new UINT16[512 * 424 * 4]; 361 | if (NULL != Ddata) 362 | { 363 | while (1) 364 | { 365 | if (!listdep0_0.empty()) 366 | { 367 | strcpy(dep0.cname, "xx"); 368 | strcpy(dep0.dname, "xx"); 369 | strcpy(dep0.wname, "xx"); 370 | mtx0_1.lock(); 371 | strcpy(dep0.cname, listdep0_0.front().cname); 372 | strcpy(dep0.dname, listdep0_0.front().dname); 373 | strcpy(dep0.wname, listdep0_0.front().wname); 374 | //cout << "从链表拿出" << listdep1.front().dname << endl; 375 | listdep0_0.pop_front(); 376 | //cout << "After process" << listdep1.size() << dep2.dname << endl; 377 | mtx0_1.unlock(); 378 | //cout << dep2.dname << endl; 379 | fp = fopen(dep0.dname, "rb"); 380 | if (fp == NULL) 381 | { 382 | cout << "xiancheng4 File open fail....."; 383 | } 384 | else 385 | { 386 | fread(Ddata, 424 * 512 * 4, 1, fp); 387 | fclose(fp); 388 | fp = NULL; 389 | Mat img = imread(dep0.cname); 390 | //cout << dep2.dname << endl; 391 | //cout << "有超限!!!" << endl; 392 | for (int i = 16386; i < 512*424 - 16385; i++) 393 | { 394 | int rows = i / 512; 395 | int cols = i % 512; 396 | if (Ddata[i] < 1200 && Ddata[i] > 0) 397 | { 398 | circle(img, Point(232 + cols * 3, (rows - 21) * 3), 1, Scalar(0, 0, 255), 12); 399 | } 400 | } 401 | imwrite(dep0.wname, img); 402 | Sleep(1); 403 | } 404 | } 405 | } 406 | delete[] Ddata; 407 | Ddata = NULL; 408 | } 409 | return 10004; 410 | } 411 | int thread1() 412 | { 413 | libfreenect2::PacketPipeline *pipeline = 0; 414 | std::string serial1 = ""; 415 | structcd cd1; 416 | bool viewer_enabled1 = true; 417 | bool enable_rgb1 = true; 418 | bool enable_depth1 = true; 419 | int deviceId1 = -1; 420 | size_t framemax1 = -1; 421 | if (!enable_rgb1 && !enable_depth1) 422 | { 423 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 424 | return -1; 425 | } 426 | dev1 = freenect2.openDevice(1); 427 | if (dev1 == 0) 428 | { 429 | std::cout << "failure opening device!" << std::endl; 430 | return -1; 431 | } 432 | 433 | devtopause1 = dev1; 434 | 435 | signal(SIGINT, sigint_handler); 436 | #ifdef SIGUSR1 437 | signal(SIGUSR1, sigusr1_handler1); 438 | #endif 439 | protonect_shutdown = false; 440 | 441 | /// [listeners] 442 | int types = 0; 443 | if (enable_rgb1) 444 | types |= libfreenect2::Frame::Color; 445 | if (enable_depth1) 446 | types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; 447 | libfreenect2::SyncMultiFrameListener listener(types); 448 | libfreenect2::FrameMap frames; 449 | 450 | dev1->setColorFrameListener(&listener); 451 | dev1->setIrAndDepthFrameListener(&listener); 452 | /// [listeners] 453 | 454 | /// [start] 455 | if (enable_rgb1 && enable_depth1) 456 | { 457 | if (!dev1->start()) 458 | return -1; 459 | } 460 | else 461 | { 462 | if (!dev1->startStreams(enable_rgb1, enable_depth1)) 463 | return -1; 464 | } 465 | 466 | std::cout << "device serial: " << dev1->getSerialNumber() << std::endl; 467 | std::cout << "device firmware: " << dev1->getFirmwareVersion() << std::endl; 468 | /// [start] 469 | 470 | /// [registration setup] 471 | libfreenect2::Registration* registration = new libfreenect2::Registration(dev1->getIrCameraParams(), dev1->getColorCameraParams()); 472 | libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); 473 | /// [registration setup] 474 | 475 | size_t framecount = 0; 476 | while (!protonect_shutdown && (framemax1 == (size_t)-1 || framecount < framemax1)) 477 | { 478 | Sleep(1000); 479 | if (!listener.waitForNewFrame(frames, 10 * 1000)) // 10 sconds 480 | { 481 | std::cout << "timeout!" << std::endl; 482 | return -1; 483 | } 484 | libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; 485 | libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; 486 | libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; 487 | /// [loop start] 488 | 489 | if (enable_rgb1 && enable_depth1) 490 | { 491 | /// [registration] 492 | registration->apply(rgb, depth, &undistorted, ®istered); 493 | const float* depth_data = (float*)depth->data; 494 | std::cout << "Depth information 1-------1:" << depth_data[10000] << std::endl; 495 | cd1.data = new unsigned char[1920 * 1080 * 4]; 496 | cd1.depth_data = new float[512 * 424 * 4]; 497 | memcpy(cd1.data, rgb->data, 1920 * 1080 * 4); 498 | memcpy(cd1.depth_data, depth_data, 512 * 424 * 4); 499 | 500 | mtx1.lock(); 501 | listcd1.push_back(cd1); 502 | mtx1.unlock(); 503 | } 504 | 505 | framecount++; 506 | if (!viewer_enabled1) 507 | { 508 | if (framecount % 100 == 0) 509 | std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; 510 | listener.release(frames); 511 | continue; 512 | } 513 | listener.release(frames); 514 | } 515 | dev1->stop(); 516 | dev1->close(); 517 | delete registration; 518 | 519 | return 0; 520 | } 521 | int thread1_si() 522 | { 523 | FILE *fp; 524 | char dpath[MAX_PATH] = ""; 525 | char cpath[MAX_PATH] = ""; 526 | char wpath[MAX_PATH] = ""; 527 | structcd scd1; 528 | structdep de1; 529 | while (1) 530 | { 531 | Sleep(10); 532 | if (listcd1.size() > 0) 533 | { 534 | mtx1.lock(); 535 | scd1.data = listcd1.front().data; 536 | scd1.depth_data = listcd1.front().depth_data; 537 | listcd1.pop_front(); 538 | mtx1.unlock(); 539 | SYSTEMTIME st; 540 | GetLocalTime(&st); 541 | sprintf(dpath, "D:\\1\\%04d%02d%02d-%02d%02d%02d%03d.data", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 542 | sprintf(cpath, "D:\\1\\%04d%02d%02d-%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 543 | sprintf(wpath, "D:\\1_jpg\\%04d%02d%02d-%02d%02d%02d%03d.jpg", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 544 | fp = fopen(dpath, "wb"); 545 | fwrite(scd1.depth_data, 512 * 424 * 4, 1, fp); 546 | fclose(fp); 547 | fp = NULL; 548 | Bitmap1(scd1.data, 1920, 1080, cpath); 549 | 550 | delete[] scd1.data; 551 | delete[] scd1.depth_data; 552 | strcpy(de1.cname, cpath); 553 | strcpy(de1.dname, dpath); 554 | strcpy(de1.wname, wpath); 555 | 556 | mtx1_0.lock(); 557 | listdep0.push_back(de1); 558 | mtx1_0.unlock(); 559 | } 560 | 561 | } 562 | return 0; 563 | 564 | 565 | } 566 | int thread1_jd() 567 | { 568 | structdep dep1; 569 | FILE *fp; 570 | float*Data = new float[424 * 512 * 4]; 571 | if (NULL != Data) 572 | { 573 | while (1) 574 | { 575 | if (!listdep1.empty()) 576 | { 577 | strcpy(dep1.cname, "xx"); 578 | strcpy(dep1.dname, "xx"); 579 | strcpy(dep1.wname, "xx"); 580 | mtx1_0.lock(); 581 | strcpy(dep1.cname, listdep0.front().cname); 582 | strcpy(dep1.dname, listdep0.front().dname); 583 | strcpy(dep1.wname, listdep0.front().wname); 584 | listdep1.pop_front(); 585 | mtx1_0.unlock(); 586 | fp = fopen(dep1.dname, "rb"); 587 | if (fp == NULL) 588 | { 589 | cout << dep1.dname << "xiancheng3 File open fail....."; 590 | } 591 | else 592 | { 593 | fread(Data, 424 * 512 * 4, 1, fp); 594 | fclose(fp); 595 | fp = NULL; 596 | int index = 0; 597 | for (int i = 16386; i < 1920 * 1080 - 16385; i++) 598 | { 599 | if (Data[i] < 1200 && Data[i] >0) 600 | { 601 | index++; 602 | } 603 | if (index > 25) 604 | { 605 | mtx1_1.lock(); 606 | listdep1_0.push_back(dep1); 607 | mtx1_1.unlock(); 608 | break; 609 | } 610 | } 611 | } 612 | } 613 | Sleep(1); 614 | } 615 | delete[] Data; 616 | Data = NULL; 617 | } 618 | return 0; 619 | } 620 | int thread1_lc() 621 | { 622 | FILE *fp; 623 | structdep dep1; 624 | float* Ddata = new float[512 * 424 * 4]; 625 | if (NULL != Ddata) 626 | { 627 | while (1) 628 | { 629 | if (!listdep1_0.empty()) 630 | { 631 | strcpy(dep1.cname, "xx"); 632 | strcpy(dep1.dname, "xx"); 633 | strcpy(dep1.wname, "xx"); 634 | mtx1_1.lock(); 635 | strcpy(dep1.cname, listdep1_0.front().cname); 636 | strcpy(dep1.dname, listdep1_0.front().dname); 637 | strcpy(dep1.wname, listdep1_0.front().wname); 638 | //cout << "从链表拿出" << listdep1.front().dname << endl; 639 | listdep1_0.pop_front(); 640 | //cout << "After process" << listdep1.size() << dep2.dname << endl; 641 | mtx0_1.unlock(); 642 | //cout << dep2.dname << endl; 643 | fp = fopen(dep1.dname, "rb"); 644 | if (fp == NULL) 645 | { 646 | cout << "xiancheng4 File open fail....."; 647 | } 648 | else 649 | { 650 | fread(Ddata, 424 * 512 * 2, 1, fp); 651 | fclose(fp); 652 | fp = NULL; 653 | Mat img = imread(dep1.cname); 654 | //cout << dep2.dname << endl; 655 | //cout << "有超限!!!" << endl; 656 | for (int i = 16386; i < 512 * 424 - 16385; i++) 657 | { 658 | int rows = i / 512; 659 | int cols = i % 512; 660 | if (Ddata[i] < 1200 && Ddata[i] > 0) 661 | { 662 | circle(img, Point(232 + cols * 3, (rows - 21) * 3), 1, Scalar(0, 0, 255), 12); 663 | } 664 | } 665 | imwrite(dep1.wname, img); 666 | Sleep(1); 667 | } 668 | } 669 | } 670 | delete[] Ddata; 671 | Ddata = NULL; 672 | } 673 | return 10004; 674 | } 675 | int thread2() 676 | { 677 | libfreenect2::PacketPipeline *pipeline = 0; 678 | std::string serial1 = ""; 679 | 680 | structcd cd2; 681 | //list listcd2; 682 | 683 | bool viewer_enabled1 = true; 684 | bool enable_rgb1 = true; 685 | bool enable_depth1 = true; 686 | int deviceId1 = -1; 687 | size_t framemax1 = -1; 688 | if (!enable_rgb1 && !enable_depth1) 689 | { 690 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 691 | return -1; 692 | } 693 | dev2 = freenect2.openDevice(2); 694 | if (dev2 == 0) 695 | { 696 | std::cout << "failure opening device!" << std::endl; 697 | return -1; 698 | } 699 | 700 | devtopause1 = dev2; 701 | 702 | signal(SIGINT, sigint_handler); 703 | #ifdef SIGUSR1 704 | signal(SIGUSR1, sigusr1_handler1); 705 | #endif 706 | protonect_shutdown = false; 707 | 708 | /// [listeners] 709 | int types = 0; 710 | if (enable_rgb1) 711 | types |= libfreenect2::Frame::Color; 712 | if (enable_depth1) 713 | types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; 714 | libfreenect2::SyncMultiFrameListener listener(types); 715 | libfreenect2::FrameMap frames; 716 | 717 | dev2->setColorFrameListener(&listener); 718 | dev2->setIrAndDepthFrameListener(&listener); 719 | /// [listeners] 720 | 721 | /// [start] 722 | if (enable_rgb1 && enable_depth1) 723 | { 724 | if (!dev2->start()) 725 | return -1; 726 | } 727 | else 728 | { 729 | if (!dev2->startStreams(enable_rgb1, enable_depth1)) 730 | return -1; 731 | } 732 | 733 | std::cout << "device serial: " << dev2->getSerialNumber() << std::endl; 734 | std::cout << "device firmware: " << dev2->getFirmwareVersion() << std::endl; 735 | /// [start] 736 | /// [registration setup] 737 | libfreenect2::Registration* registration = new libfreenect2::Registration(dev2->getIrCameraParams(), dev2->getColorCameraParams()); 738 | libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); 739 | /// [registration setup] 740 | 741 | size_t framecount = 0; 742 | while (!protonect_shutdown && (framemax1 == (size_t)-1 || framecount < framemax1)) 743 | { 744 | Sleep(1000); 745 | if (!listener.waitForNewFrame(frames, 10 * 1000)) // 10 sconds 746 | { 747 | std::cout << "timeout!" << std::endl; 748 | return -1; 749 | } 750 | libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; 751 | libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; 752 | libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; 753 | /// [loop start] 754 | 755 | if (enable_rgb1 && enable_depth1) 756 | { 757 | /// [registration] 758 | registration->apply(rgb, depth, &undistorted, ®istered); 759 | const float* depth_data = (float*)depth->data; 760 | std::cout << "Depth information 2-------2:" << depth_data[10000] << std::endl; 761 | cd2.data = new unsigned char[1920 * 1080 * 4]; 762 | cd2.depth_data = new float[512 * 424 * 4]; 763 | memcpy(cd2.data, rgb->data, 1920 * 1080 * 4); 764 | memcpy(cd2.depth_data, depth_data, 512 * 424 * 4); 765 | 766 | mtx2.lock(); 767 | listcd2.push_back(cd2); 768 | mtx2.unlock(); 769 | } 770 | 771 | framecount++; 772 | if (!viewer_enabled1) 773 | { 774 | if (framecount % 100 == 0) 775 | std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; 776 | listener.release(frames); 777 | continue; 778 | } 779 | listener.release(frames); 780 | } 781 | dev1->stop(); 782 | dev1->close(); 783 | delete registration; 784 | 785 | return 0; 786 | } 787 | int thread2_si() 788 | { 789 | FILE *fp; 790 | char dpath[MAX_PATH] = ""; 791 | char cpath[MAX_PATH] = ""; 792 | char wpath[MAX_PATH] = ""; 793 | structcd scd2; 794 | structdep de2; 795 | while (1) 796 | { 797 | Sleep(10); 798 | if (listcd2.size() > 0) 799 | { 800 | mtx2.lock(); 801 | scd2.data = listcd2.front().data; 802 | scd2.depth_data = listcd2.front().depth_data; 803 | listcd2.pop_front(); 804 | mtx2.unlock(); 805 | SYSTEMTIME st; 806 | GetLocalTime(&st); 807 | sprintf(dpath, "D:\\2\\%04d%02d%02d-%02d%02d%02d%03d.data", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 808 | sprintf(cpath, "D:\\2\\%04d%02d%02d-%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 809 | sprintf(wpath, "D:\\2_jpg\\%04d%02d%02d-%02d%02d%02d%03d.jpg", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 810 | fp = fopen(dpath, "wb"); 811 | fwrite(scd2.depth_data, 512 * 424 * 4, 1, fp); 812 | fclose(fp); 813 | fp = NULL; 814 | Bitmap1(scd2.data, 1920, 1080, cpath); 815 | 816 | delete[] scd2.data; 817 | delete[] scd2.depth_data; 818 | strcpy(de2.cname, cpath); 819 | strcpy(de2.dname, dpath); 820 | strcpy(de2.wname, wpath); 821 | 822 | mtx2_0.lock(); 823 | listdep2.push_back(de2); 824 | mtx2_0.unlock(); 825 | } 826 | 827 | } 828 | return 0; 829 | 830 | 831 | } 832 | int thread2_jd() 833 | { 834 | structdep dep2; 835 | FILE *fp; 836 | float*Data = new float[424 * 512 * 4]; 837 | if (NULL != Data) 838 | { 839 | while (1) 840 | { 841 | if (!listdep2.empty()) 842 | { 843 | strcpy(dep2.cname, "xx"); 844 | strcpy(dep2.dname, "xx"); 845 | strcpy(dep2.wname, "xx"); 846 | mtx2_0.lock(); 847 | strcpy(dep2.cname, listdep2.front().cname); 848 | strcpy(dep2.dname, listdep2.front().dname); 849 | strcpy(dep2.wname, listdep2.front().wname); 850 | listdep2.pop_front(); 851 | mtx2_0.unlock(); 852 | fp = fopen(dep2.dname, "rb"); 853 | if (fp == NULL) 854 | { 855 | cout << dep2.dname << "xiancheng3 File open fail....."; 856 | } 857 | else 858 | { 859 | fread(Data, 424 * 512 * 4, 1, fp); 860 | fclose(fp); 861 | fp = NULL; 862 | int index = 0; 863 | for (int i = 16386; i < 1920 * 1080 - 16385; i++) 864 | { 865 | if (Data[i] < 1200 && Data[i] >0) 866 | { 867 | index++; 868 | } 869 | if (index > 25) 870 | { 871 | mtx2_1.lock(); 872 | listdep2_0.push_back(dep2); 873 | mtx2_1.unlock(); 874 | break; 875 | } 876 | } 877 | } 878 | } 879 | Sleep(1); 880 | } 881 | delete[] Data; 882 | Data = NULL; 883 | } 884 | return 0; 885 | } 886 | int thread2_lc() 887 | { 888 | FILE *fp; 889 | structdep dep2; 890 | float* Ddata = new float[512 * 424 * 4]; 891 | if (NULL != Ddata) 892 | { 893 | while (1) 894 | { 895 | if (!listdep2_0.empty()) 896 | { 897 | strcpy(dep2.cname, "xx"); 898 | strcpy(dep2.dname, "xx"); 899 | strcpy(dep2.wname, "xx"); 900 | mtx2_1.lock(); 901 | strcpy(dep2.cname, listdep2_0.front().cname); 902 | strcpy(dep2.dname, listdep2_0.front().dname); 903 | strcpy(dep2.wname, listdep2_0.front().wname); 904 | //cout << "从链表拿出" << listdep1.front().dname << endl; 905 | listdep2_0.pop_front(); 906 | //cout << "After process" << listdep1.size() << dep2.dname << endl; 907 | mtx2_1.unlock(); 908 | //cout << dep2.dname << endl; 909 | fp = fopen(dep2.dname, "rb"); 910 | if (fp == NULL) 911 | { 912 | cout << "xiancheng4 File open fail....."; 913 | } 914 | else 915 | { 916 | fread(Ddata, 424 * 512 * 4, 1, fp); 917 | fclose(fp); 918 | fp = NULL; 919 | Mat img = imread(dep2.cname); 920 | //cout << dep2.dname << endl; 921 | //cout << "有超限!!!" << endl; 922 | for (int i = 16386; i < 512 * 424 - 16385; i++) 923 | { 924 | int rows = i / 512; 925 | int cols = i % 512; 926 | if (Ddata[i] < 1200 && Ddata[i] > 0) 927 | { 928 | circle(img, Point(232 + cols * 3, (rows - 21) * 3), 1, Scalar(0, 0, 255), 12); 929 | } 930 | } 931 | imwrite(dep2.wname, img); 932 | Sleep(1); 933 | } 934 | } 935 | } 936 | delete[] Ddata; 937 | Ddata = NULL; 938 | } 939 | //myKinect.Writeimg(); 940 | //Sleep(1); 941 | return 10004; 942 | } 943 | int thread3() 944 | { 945 | libfreenect2::PacketPipeline *pipeline = 0; 946 | std::string serial1 = ""; 947 | //FILE *fp; 948 | //char dpath[] = ""; 949 | //char cpath[] = ""; 950 | 951 | structcd cd3; 952 | //list listcd3; 953 | 954 | bool viewer_enabled1 = true; 955 | bool enable_rgb1 = true; 956 | bool enable_depth1 = true; 957 | int deviceId1 = -1; 958 | size_t framemax1 = -1; 959 | if (!enable_rgb1 && !enable_depth1) 960 | { 961 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 962 | return -1; 963 | } 964 | dev3 = freenect2.openDevice(3); 965 | if (dev1 == 0) 966 | { 967 | std::cout << "failure opening device!" << std::endl; 968 | return -1; 969 | } 970 | 971 | devtopause1 = dev3; 972 | 973 | signal(SIGINT, sigint_handler); 974 | #ifdef SIGUSR1 975 | signal(SIGUSR1, sigusr1_handler1); 976 | #endif 977 | protonect_shutdown = false; 978 | 979 | /// [listeners] 980 | int types = 0; 981 | if (enable_rgb1) 982 | types |= libfreenect2::Frame::Color; 983 | if (enable_depth1) 984 | types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; 985 | libfreenect2::SyncMultiFrameListener listener(types); 986 | libfreenect2::FrameMap frames; 987 | 988 | dev3->setColorFrameListener(&listener); 989 | dev3->setIrAndDepthFrameListener(&listener); 990 | /// [listeners] 991 | 992 | /// [start] 993 | if (enable_rgb1 && enable_depth1) 994 | { 995 | if (!dev3->start()) 996 | return -1; 997 | } 998 | else 999 | { 1000 | if (!dev3->startStreams(enable_rgb1, enable_depth1)) 1001 | return -1; 1002 | } 1003 | 1004 | std::cout << "device serial: " << dev3->getSerialNumber() << std::endl; 1005 | std::cout << "device firmware: " << dev3->getFirmwareVersion() << std::endl; 1006 | /// [start] 1007 | 1008 | /// [registration setup] 1009 | libfreenect2::Registration* registration = new libfreenect2::Registration(dev3->getIrCameraParams(), dev3->getColorCameraParams()); 1010 | libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); 1011 | /// [registration setup] 1012 | 1013 | size_t framecount = 0; 1014 | while (!protonect_shutdown && (framemax1 == (size_t)-1 || framecount < framemax1)) 1015 | { 1016 | Sleep(1000); 1017 | if (!listener.waitForNewFrame(frames, 10 * 1000)) // 10 sconds 1018 | { 1019 | std::cout << "timeout!" << std::endl; 1020 | return -1; 1021 | } 1022 | libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; 1023 | libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; 1024 | libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; 1025 | /// [loop start] 1026 | 1027 | if (enable_rgb1 && enable_depth1) 1028 | { 1029 | /// [registration] 1030 | registration->apply(rgb, depth, &undistorted, ®istered); 1031 | const float* depth_data = (float*)depth->data; 1032 | 1033 | std::cout << "Depth information 3-------3:" << depth_data[10000] << std::endl; 1034 | cd3.data = new unsigned char[1920 * 1080 * 4]; 1035 | cd3.depth_data = new float[512 * 424 * 4]; 1036 | 1037 | memcpy(cd3.data, rgb->data, 1920 * 1080 * 4); 1038 | memcpy(cd3.depth_data, depth->data, 512 * 424 * 4); 1039 | 1040 | mtx3.lock(); 1041 | listcd3.push_back(cd3); 1042 | mtx3.unlock(); 1043 | } 1044 | 1045 | framecount++; 1046 | if (!viewer_enabled1) 1047 | { 1048 | if (framecount % 100 == 0) 1049 | std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; 1050 | listener.release(frames); 1051 | continue; 1052 | } 1053 | listener.release(frames); 1054 | } 1055 | dev1->stop(); 1056 | dev1->close(); 1057 | delete registration; 1058 | 1059 | return 0; 1060 | } 1061 | int thread3_si() 1062 | { 1063 | FILE *fp; 1064 | char dpath[MAX_PATH] = ""; 1065 | char cpath[MAX_PATH] = ""; 1066 | char wpath[MAX_PATH] = ""; 1067 | structcd scd3; 1068 | structdep de3; 1069 | while (1) 1070 | { 1071 | Sleep(10); 1072 | if (listcd3.size() > 0) 1073 | { 1074 | mtx3.lock(); 1075 | scd3.data = listcd3.front().data; 1076 | scd3.depth_data = listcd3.front().depth_data; 1077 | listcd3.pop_front(); 1078 | mtx3.unlock(); 1079 | SYSTEMTIME st; 1080 | GetLocalTime(&st); 1081 | sprintf(dpath, "D:\\3\\%04d%02d%02d-%02d%02d%02d%03d.data", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1082 | sprintf(cpath, "D:\\3\\%04d%02d%02d-%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1083 | sprintf(wpath, "D:\\3_jpg\\%04d%02d%02d-%02d%02d%02d%03d.jpg", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1084 | fp = fopen(dpath, "wb"); 1085 | fwrite(scd3.depth_data, 512 * 424 * 4, 1, fp); 1086 | fclose(fp); 1087 | fp = NULL; 1088 | Bitmap1(scd3.data, 1920, 1080, cpath); 1089 | 1090 | delete[] scd3.data; 1091 | delete[] scd3.depth_data; 1092 | strcpy(de3.cname, cpath); 1093 | strcpy(de3.dname, dpath); 1094 | strcpy(de3.wname, wpath); 1095 | 1096 | mtx3_0.lock(); 1097 | listdep3.push_back(de3); 1098 | mtx3_0.unlock(); 1099 | } 1100 | 1101 | } 1102 | return 0; 1103 | 1104 | 1105 | } 1106 | int thread3_jd() 1107 | { 1108 | structdep dep3; 1109 | FILE *fp; 1110 | float*Data = new float[424 * 512 * 4]; 1111 | if (NULL != Data) 1112 | { 1113 | while (1) 1114 | { 1115 | if (!listdep3.empty()) 1116 | { 1117 | strcpy(dep3.cname, "xx"); 1118 | strcpy(dep3.dname, "xx"); 1119 | strcpy(dep3.wname, "xx"); 1120 | mtx3_0.lock(); 1121 | strcpy(dep3.cname, listdep3.front().cname); 1122 | strcpy(dep3.dname, listdep3.front().dname); 1123 | strcpy(dep3.wname, listdep3.front().wname); 1124 | listdep3.pop_front(); 1125 | mtx3_0.unlock(); 1126 | fp = fopen(dep3.dname, "rb"); 1127 | if (fp == NULL) 1128 | { 1129 | cout << dep3.dname << "xiancheng3 File open fail....."; 1130 | } 1131 | else 1132 | { 1133 | fread(Data, 424 * 512 * 4, 1, fp); 1134 | fclose(fp); 1135 | fp = NULL; 1136 | int index = 0; 1137 | for (int i = 16386; i < 1920 * 1080 - 16385; i++) 1138 | { 1139 | if (Data[i] < 1200 && Data[i] >0) 1140 | { 1141 | index++; 1142 | } 1143 | if (index > 25) 1144 | { 1145 | mtx3_1.lock(); 1146 | listdep3_0.push_back(dep3); 1147 | mtx3_1.unlock(); 1148 | break; 1149 | } 1150 | } 1151 | } 1152 | } 1153 | Sleep(1); 1154 | } 1155 | delete[] Data; 1156 | Data = NULL; 1157 | } 1158 | return 0; 1159 | } 1160 | int thread3_lc() 1161 | { 1162 | FILE *fp; 1163 | structdep dep3; 1164 | float* Ddata = new float[512 * 424 * 4]; 1165 | if (NULL != Ddata) 1166 | { 1167 | while (1) 1168 | { 1169 | if (!listdep3_0.empty()) 1170 | { 1171 | strcpy(dep3.cname, "xx"); 1172 | strcpy(dep3.dname, "xx"); 1173 | strcpy(dep3.wname, "xx"); 1174 | mtx3_1.lock(); 1175 | strcpy(dep3.cname, listdep3_0.front().cname); 1176 | strcpy(dep3.dname, listdep3_0.front().dname); 1177 | strcpy(dep3.wname, listdep3_0.front().wname); 1178 | //cout << "从链表拿出" << listdep1.front().dname << endl; 1179 | listdep3_0.pop_front(); 1180 | //cout << "After process" << listdep1.size() << dep2.dname << endl; 1181 | mtx3_1.unlock(); 1182 | //cout << dep2.dname << endl; 1183 | fp = fopen(dep3.dname, "rb"); 1184 | if (fp == NULL) 1185 | { 1186 | cout << "xiancheng4 File open fail....."; 1187 | } 1188 | else 1189 | { 1190 | fread(Ddata, 424 * 512 * 4, 1, fp); 1191 | fclose(fp); 1192 | fp = NULL; 1193 | Mat img = imread(dep3.cname); 1194 | //cout << dep2.dname << endl; 1195 | //cout << "有超限!!!" << endl; 1196 | for (int i = 16386; i < 512 * 424 - 16385; i++) 1197 | { 1198 | int rows = i / 512; 1199 | int cols = i % 512; 1200 | if (Ddata[i] < 1200 && Ddata[i] > 0) 1201 | { 1202 | circle(img, Point(232 + cols * 3, (rows - 21) * 3), 1, Scalar(0, 0, 255), 12); 1203 | } 1204 | } 1205 | imwrite(dep3.wname, img); 1206 | Sleep(1); 1207 | } 1208 | } 1209 | } 1210 | delete[] Ddata; 1211 | Ddata = NULL; 1212 | } 1213 | //myKinect.Writeimg(); 1214 | //Sleep(1); 1215 | return 10004; 1216 | } 1217 | int thread4() 1218 | { 1219 | libfreenect2::PacketPipeline *pipeline = 0; 1220 | std::string serial1 = ""; 1221 | 1222 | structcd cd4; 1223 | //list listcd4; 1224 | bool viewer_enabled1 = true; 1225 | bool enable_rgb1 = true; 1226 | bool enable_depth1 = true; 1227 | int deviceId1 = -1; 1228 | size_t framemax1 = -1; 1229 | if (!enable_rgb1 && !enable_depth1) 1230 | { 1231 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 1232 | return -1; 1233 | } 1234 | dev1 = freenect2.openDevice(4); 1235 | if (dev1 == 0) 1236 | { 1237 | std::cout << "failure opening device!" << std::endl; 1238 | return -1; 1239 | } 1240 | 1241 | devtopause1 = dev1; 1242 | 1243 | signal(SIGINT, sigint_handler); 1244 | #ifdef SIGUSR1 1245 | signal(SIGUSR1, sigusr1_handler1); 1246 | #endif 1247 | protonect_shutdown = false; 1248 | 1249 | /// [listeners] 1250 | int types = 0; 1251 | if (enable_rgb1) 1252 | types |= libfreenect2::Frame::Color; 1253 | if (enable_depth1) 1254 | types |= libfreenect2::Frame::Ir | libfreenect2::Frame::Depth; 1255 | libfreenect2::SyncMultiFrameListener listener(types); 1256 | libfreenect2::FrameMap frames; 1257 | 1258 | dev1->setColorFrameListener(&listener); 1259 | dev1->setIrAndDepthFrameListener(&listener); 1260 | /// [listeners] 1261 | 1262 | /// [start] 1263 | if (enable_rgb1 && enable_depth1) 1264 | { 1265 | if (!dev1->start()) 1266 | return -1; 1267 | } 1268 | else 1269 | { 1270 | if (!dev1->startStreams(enable_rgb1, enable_depth1)) 1271 | return -1; 1272 | } 1273 | 1274 | std::cout << "device serial: " << dev1->getSerialNumber() << std::endl; 1275 | std::cout << "device firmware: " << dev1->getFirmwareVersion() << std::endl; 1276 | /// [start] 1277 | 1278 | /// [registration setup] 1279 | libfreenect2::Registration* registration = new libfreenect2::Registration(dev1->getIrCameraParams(), dev1->getColorCameraParams()); 1280 | libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); 1281 | /// [registration setup] 1282 | 1283 | size_t framecount = 0; 1284 | while (!protonect_shutdown && (framemax1 == (size_t)-1 || framecount < framemax1)) 1285 | { 1286 | Sleep(1000); 1287 | if (!listener.waitForNewFrame(frames, 10 * 1000)) // 10 sconds 1288 | { 1289 | std::cout << "timeout!" << std::endl; 1290 | return -1; 1291 | } 1292 | libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; 1293 | libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir]; 1294 | libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; 1295 | /// [loop start] 1296 | 1297 | if (enable_rgb1 && enable_depth1) 1298 | { 1299 | /// [registration] 1300 | registration->apply(rgb, depth, &undistorted, ®istered); 1301 | const float* depth_data = (float*)depth->data; 1302 | 1303 | 1304 | cd4.data = new unsigned char[1920 * 1080 * 4]; 1305 | cd4.depth_data = new float[512 * 424 * 4]; 1306 | 1307 | memcpy(cd4.data, rgb->data, 1920 * 1080 * 4); 1308 | memcpy(cd4.depth_data, depth->data, 512 * 424 * 4); 1309 | 1310 | mtx4.lock(); 1311 | listcd4.push_back(cd4); 1312 | mtx4.unlock(); 1313 | 1314 | } 1315 | 1316 | framecount++; 1317 | if (!viewer_enabled1) 1318 | { 1319 | if (framecount % 100 == 0) 1320 | std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; 1321 | listener.release(frames); 1322 | continue; 1323 | } 1324 | listener.release(frames); 1325 | } 1326 | dev1->stop(); 1327 | dev1->close(); 1328 | delete registration; 1329 | 1330 | return 0; 1331 | } 1332 | int thread4_si() 1333 | { 1334 | FILE *fp; 1335 | char dpath[MAX_PATH] = ""; 1336 | char cpath[MAX_PATH] = ""; 1337 | char wpath[MAX_PATH] = ""; 1338 | structcd scd4; 1339 | structdep de4; 1340 | while (1) 1341 | { 1342 | Sleep(10); 1343 | if (listcd4.size() > 0) 1344 | { 1345 | mtx4.lock(); 1346 | scd4.data = listcd4.front().data; 1347 | scd4.depth_data = listcd4.front().depth_data; 1348 | listcd4.pop_front(); 1349 | mtx4.unlock(); 1350 | 1351 | SYSTEMTIME st; 1352 | GetLocalTime(&st); 1353 | sprintf(dpath, "D:\\4\\%04d%02d%02d-%02d%02d%02d%03d.data", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1354 | sprintf(cpath, "D:\\4\\%04d%02d%02d-%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1355 | sprintf(wpath, "D:\\4_jpg\\%04d%02d%02d-%02d%02d%02d%03d.jpg", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 1356 | fp = fopen(dpath, "wb"); 1357 | fwrite(scd4.depth_data, 512 * 424 * 4, 1, fp); 1358 | fclose(fp); 1359 | fp = NULL; 1360 | Bitmap1(scd4.data, 1920, 1080, cpath); 1361 | 1362 | delete[] scd4.data; 1363 | delete[] scd4.depth_data; 1364 | strcpy(de4.cname, cpath); 1365 | strcpy(de4.dname, dpath); 1366 | strcpy(de4.wname, wpath); 1367 | 1368 | mtx4_0.lock(); 1369 | listdep4.push_back(de4); 1370 | mtx4_0.unlock(); 1371 | } 1372 | 1373 | } 1374 | return 0; 1375 | 1376 | 1377 | } 1378 | int thread4_jd() 1379 | { 1380 | structdep dep4; 1381 | FILE *fp; 1382 | float*Data = new float[424 * 512 * 4]; 1383 | if (NULL != Data) 1384 | { 1385 | while (1) 1386 | { 1387 | if (!listdep4.empty()) 1388 | { 1389 | strcpy(dep4.cname, "xx"); 1390 | strcpy(dep4.dname, "xx"); 1391 | strcpy(dep4.wname, "xx"); 1392 | mtx4_0.lock(); 1393 | strcpy(dep4.cname, listdep4.front().cname); 1394 | strcpy(dep4.dname, listdep4.front().dname); 1395 | strcpy(dep4.wname, listdep4.front().wname); 1396 | listdep4.pop_front(); 1397 | mtx4_0.unlock(); 1398 | fp = fopen(dep4.dname, "rb"); 1399 | if (fp == NULL) 1400 | { 1401 | cout << dep4.dname << "xiancheng3 File open fail....."; 1402 | } 1403 | else 1404 | { 1405 | fread(Data, 424 * 512 * 4, 1, fp); 1406 | fclose(fp); 1407 | fp = NULL; 1408 | int index = 0; 1409 | for (int i = 16386; i < 1920 * 1080 - 16385; i++) 1410 | { 1411 | if (Data[i] < 1200 && Data[i] >0) 1412 | { 1413 | index++; 1414 | } 1415 | if (index > 25) 1416 | { 1417 | mtx4_1.lock(); 1418 | listdep4_0.push_back(dep4); 1419 | mtx4_1.unlock(); 1420 | break; 1421 | } 1422 | } 1423 | } 1424 | } 1425 | Sleep(1); 1426 | } 1427 | delete[] Data; 1428 | Data = NULL; 1429 | } 1430 | return 0; 1431 | } 1432 | int thread4_lc() 1433 | { 1434 | FILE *fp; 1435 | structdep dep4; 1436 | float* Ddata = new float[512 * 424 * 4]; 1437 | if (NULL != Ddata) 1438 | { 1439 | while (1) 1440 | { 1441 | if (!listdep4_0.empty()) 1442 | { 1443 | strcpy(dep4.cname, "xx"); 1444 | strcpy(dep4.dname, "xx"); 1445 | strcpy(dep4.wname, "xx"); 1446 | mtx4_1.lock(); 1447 | strcpy(dep4.cname, listdep4_0.front().cname); 1448 | strcpy(dep4.dname, listdep4_0.front().dname); 1449 | strcpy(dep4.wname, listdep4_0.front().wname); 1450 | //cout << "从链表拿出" << listdep1.front().dname << endl; 1451 | listdep4_0.pop_front(); 1452 | //cout << "After process" << listdep1.size() << dep2.dname << endl; 1453 | mtx4_1.unlock(); 1454 | //cout << dep2.dname << endl; 1455 | fp = fopen(dep4.dname, "rb"); 1456 | if (fp == NULL) 1457 | { 1458 | cout << "xiancheng4 File open fail....."; 1459 | } 1460 | else 1461 | { 1462 | fread(Ddata, 424 * 512 * 4, 1, fp); 1463 | fclose(fp); 1464 | fp = NULL; 1465 | Mat img = imread(dep4.cname); 1466 | //cout << dep2.dname << endl; 1467 | //cout << "有超限!!!" << endl; 1468 | for (int i = 16386; i < 512 * 424 - 16385; i++) 1469 | { 1470 | int rows = i / 512; 1471 | int cols = i % 512; 1472 | if (Ddata[i] < 1200 && Ddata[i] > 0) 1473 | { 1474 | circle(img, Point(232 + cols * 3, (rows - 21) * 3), 1, Scalar(0, 0, 255), 12); 1475 | } 1476 | } 1477 | imwrite(dep4.wname, img); 1478 | Sleep(1); 1479 | } 1480 | } 1481 | } 1482 | delete[] Ddata; 1483 | Ddata = NULL; 1484 | } 1485 | //myKinect.Writeimg(); 1486 | //Sleep(1); 1487 | return 10004; 1488 | } 1489 | int main() 1490 | { 1491 | std::cout << "Kinect v2 num:" << freenect2.enumerateDevices() << std::endl; 1492 | if (!enable_rgb && !enable_depth) 1493 | { 1494 | std::cerr << "Disabling both streams is not allowed!" << std::endl; 1495 | return -1; 1496 | } 1497 | dev = freenect2.openDefaultDevice(); 1498 | if (dev == 0) 1499 | { 1500 | std::cout << "failure opening device!" << std::endl; 1501 | return -1; 1502 | } 1503 | std::thread t1(thread0); 1504 | std::thread t2(thread1); 1505 | std::thread t3(thread2); 1506 | std::thread t4(thread3); 1507 | std::thread t5(thread4); 1508 | std::thread t1_1(thread0_si); 1509 | std::thread t2_1(thread1_si); 1510 | std::thread t3_1(thread2_si); 1511 | std::thread t4_1(thread3_si); 1512 | std::thread t5_1(thread4_si); 1513 | std::thread t1_2(thread0_jd); 1514 | std::thread t2_2(thread1_jd); 1515 | std::thread t3_2(thread2_jd); 1516 | std::thread t4_2(thread3_jd); 1517 | std::thread t5_2(thread4_jd); 1518 | std::thread t1_3(thread0_lc); 1519 | std::thread t2_3(thread1_lc); 1520 | std::thread t3_3(thread2_lc); 1521 | std::thread t4_3(thread3_lc); 1522 | std::thread t5_3(thread4_lc); 1523 | t1.join(); 1524 | t2.join(); 1525 | t5.join(); 1526 | t3.join(); 1527 | t4.join(); 1528 | t1_1.join(); 1529 | t2_1.join(); 1530 | t3_1.join(); 1531 | t4_1.join(); 1532 | t5_1.join(); 1533 | t1_2.join(); 1534 | t2_2.join(); 1535 | t3_2.join(); 1536 | t4_2.join(); 1537 | t5_2.join(); 1538 | t1_3.join(); 1539 | t2_3.join(); 1540 | t3_3.join(); 1541 | t4_3.join(); 1542 | t5_3.join(); 1543 | return 0; 1544 | 1545 | } 1546 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multithread_kinect2 2 | Multi thread example of multiple Kinect2 based on Libfreenect2 3 | 4 | Download libfreenect2 from https://github.com/OpenKinect/libfreenect2 5 | Compile libfreenect,if you compilation success,then,replace Protonect.cpp. 6 | To configure opencv environment of opencv 7 | --------------------------------------------------------------------------------