用kinect录视频库
Posted MachineLP
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用kinect录视频库相关的知识,希望对你有一定的参考价值。
~~有兴趣的小伙伴,加kinect算法交流群:462964980。
kinect深度视频去噪
kinectmod32.dll
http://pan.baidu.com/s/1DsGqX
下载后改名kinect.dll
替换掉Redist\\OpenNI2\\Drivers\\kinect.dll
代码:
1 /* 2 * ===================================================================================== 3 * 4 * Filename: first_auoto_record.cpp 5 * 6 * Description: 7 * kinect自动录像,用于采集视频数据库,录制的视频每小段长为5min,(由子线程的模拟键盘延时决定,不同机器获取的视频长度不一样) 8 * 9 * 10 * 11 * Version: 1.0 12 * Created: 2013/10/14 16:37:10 13 * Revision: none 14 * Compiler: gcc 15 * 16 * Author: @礼杨_HDU (), yuliyang@qq.com 17 * Organization: 18 * 19 * ===================================================================================== 20 */ 21 #include <stdlib.h> 22 #include <windows.h> 23 #include <iostream> 24 #include <conio.h> 25 #include <string> 26 #include "OpenNI.h" 27 #include "opencv2/core/core.hpp" 28 #include "opencv2/highgui/highgui.hpp" 29 #include "opencv2/imgproc/imgproc.hpp" 30 31 #define RESOLUTION 640,480 32 #define RECORDRESOLUTION 590,440 33 #define ADRESOLUTION 45,40,590,440 34 #define FPS 20 35 #define GRAYTH 10 36 #define REPAIRERANGE 5 37 #define COLORTH 10 38 39 using namespace std; 40 using namespace cv; 41 using namespace openni; 42 /* 43 * === FUNCTION ====================================================================== 44 * Name: 子线程 45 * Description: 使用子进程给自己发送按键消息 46 * ===================================================================================== 47 */ 48 DWORD WINAPI ThreadFunction(LPVOID lpParameter) 49 50 printf("in thread......"); 51 while (TRUE) 52 //your code 53 54 INPUT Input1 = 0 ; 55 Input1.type = INPUT_KEYBOARD; 56 Input1.mi.dwFlags = KEYEVENTF_EXTENDEDKEY; 57 Input1.ki.wVk = 0x20; // 0 58 SendInput( 1, &Input1, sizeof( INPUT ) ); 59 INPUT Input2 = 0 ; 60 Input2.type = INPUT_KEYBOARD; 61 Input2.mi.dwFlags = KEYEVENTF_EXTENDEDKEY; 62 Input2.ki.wVk = 0x0d; // 回车 63 SendInput( 1, &Input2, sizeof( INPUT ) ); 64 Sleep(300000);//每5分钟发送一个按键消息,即录下一段视频 65 66 return true; 67 68 69 /******************************************** 70 Global variable 71 ********************************************/ 72 //Openni status 73 Status result = STATUS_OK; 74 // open device 75 Device device; 76 //OpenNI2 image 77 VideoFrameRef oniDepthImg; 78 VideoFrameRef oniColorImg; 79 // create stream 80 VideoStream oniDepthStream; 81 VideoStream oniColorStream; 82 // set video mode 83 VideoMode modeDepth; 84 VideoMode modeColor; 85 //OpenCV image 86 cv::Mat cvDepthImg; 87 cv::Mat cvDepthImg2; 88 cv::Mat cvColorImg; 89 cv::Mat cvColorImg2; 90 //OpenCV adjusted image 91 cv::Mat cvAdjustDepthImg; 92 cv::Mat cvAdjustColorImg; 93 //Resolution 94 Size se=Size(RESOLUTION); 95 Size recordse=Size(RECORDRESOLUTION); 96 /********************************************* 97 function declaration 98 *********************************************/ 99 void CheckOpenNIError( Status result, string status ) 100 101 if( result != STATUS_OK ) 102 cerr << status << " Error: " << OpenNI::getExtendedError() << endl; 103 104 void iniOpenNI(void );//初始化OpenNI 105 void releaseResource(void );//释放资源 106 void OringinCapture(void );// 107 void RemoveNoiseCapture(void );// 108 void OringinRecord(void );//普通录制视频(640,480),录制深度视频(没有去噪)和彩色视频 109 void RemoveNoiseRecord(void );//录制去噪后的视频,深度视频(未去噪)、深度视频(去噪)、彩色视频 110 void RemoveNoise(void );//去除深度图像中的噪音 111 bool pixelRepaire(int ,int ,int );//修复空洞像素点 112 bool rangeRepaire(int ,int ,int );//范围修复 113 void RepaireTest(void ); 114 void Test(void ); 115 116 117 int main( int argc, char** argv ) 118 119 HANDLE hT=CreateThread(NULL,0,ThreadFunction,NULL,0,NULL); 120 iniOpenNI(); 121 //OringinCapture(); 122 //RemoveNoiseCapture(); 123 //OringinRecord(); 124 125 RemoveNoiseRecord(); 126 releaseResource(); 127 //RepaireTest(); 128 //Test(); 129 return 0; 130 131 // 132 void iniOpenNI() 133 134 // initialize OpenNI2 135 result = OpenNI::initialize(); 136 CheckOpenNIError( result, "initialize context" ); 137 138 //open video device 139 result = device.open( openni::ANY_DEVICE ); 140 CheckOpenNIError( result, "initialize context" ); 141 142 //creat depth stream 143 result = oniDepthStream.create( device, openni::SENSOR_DEPTH ); 144 CheckOpenNIError( result, "initialize context" ); 145 //set depth mode 146 modeDepth.setResolution( RESOLUTION ); 147 modeDepth.setFps( FPS ); 148 modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM ); 149 oniDepthStream.setVideoMode(modeDepth); 150 // start depth stream 151 result = oniDepthStream.start(); 152 CheckOpenNIError( result, "initialize context" ); 153 154 // create color stream 155 result = oniColorStream.create( device, openni::SENSOR_COLOR ); 156 CheckOpenNIError( result, "initialize context" ); 157 // set color video mode 158 modeColor.setResolution( RESOLUTION ); 159 modeColor.setFps( FPS ); 160 modeColor.setPixelFormat( PIXEL_FORMAT_RGB888 ); 161 oniColorStream.setVideoMode( modeColor); 162 // start color stream 163 result = oniColorStream.start(); 164 CheckOpenNIError( result, "initialize context" ); 165 166 // set depth and color imge registration mode 167 if( device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR ) ) 168 169 cout << "support" << endl; 170 device.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR ); 171 172 173 174 // 175 void releaseResource() 176 177 //OpenNI2 destroy 178 oniDepthStream.destroy(); 179 oniColorStream.destroy(); 180 device.close(); 181 OpenNI::shutdown(); 182 183 // 184 void OringinCapture() 185 186 char DepthFilename[20]; 187 char ColorFilename[20]; 188 int n=0; 189 while(true) 190 191 if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK ) 192 193 // convert data into OpenCV type 194 cv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() ); 195 cvtColor(cvRGBImg,cvColorImg,CV_RGB2GRAY); 196 //cv::cvtColor( cvRGBImg, cvColorImg, CV_RGB2BGR ); 197 //colorVideoWriter.write(cvColorImg); 198 199 if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ) 200 201 cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() ); 202 cvRawImg16U.convertTo( cvDepthImg, CV_8UC1, 255.0/(oniDepthStream.getMaxPixelValue())); 203 //【5】 204 // convert depth image GRAY to BGR 205 //cv::cvtColor(cvDepthImg,cvDepthImg,CV_GRAY2BGR); 206 //depthVideoWriter.write(cvDepthImg); 207 208 cvAdjustDepthImg=Mat(cvDepthImg,Rect(ADRESOLUTION)); 209 cvAdjustColorImg=Mat(cvColorImg,Rect(ADRESOLUTION)); 210 if(_kbhit())//_kbhit() 211 212 n++; 213 sprintf(DepthFilename,"depthimage%03d.jpg",n); 214 sprintf(ColorFilename,"colorimage%03d.jpg",n); 215 imwrite(DepthFilename,cvAdjustDepthImg); 216 imwrite(ColorFilename,cvAdjustColorImg); 217 cout << "已经保存了" << n << "副图片" << endl; 218 system("PAUSE"); 219 220 imshow("depth",cvAdjustDepthImg); 221 imshow("color",cvAdjustColorImg); 222 waitKey(2); 223 224 225 // 226 void RemoveNoiseCapture() 227 228 char DepthFilename[20]; 229 char ColorFilename[20]; 230 int n=0; 231 while(true) 232 233 if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK ) 234 235 // convert data into OpenCV type 236 cv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() ); 237 cvtColor(cvRGBImg,cvColorImg,CV_RGB2GRAY); 238 //cv::cvtColor( cvRGBImg, cvColorImg, CV_RGB2BGR ); 239 //colorVideoWriter.write(cvColorImg); 240 241 if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ) 242 243 cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() ); 244 cvRawImg16U.convertTo( cvDepthImg, CV_8UC1, 255.0/(oniDepthStream.getMaxPixelValue())); 245 //【5】 246 // convert depth image GRAY to BGR 247 //cv::cvtColor(cvDepthImg,cvDepthImg,CV_GRAY2BGR); 248 //depthVideoWriter.write(cvDepthImg); 249 250 cvAdjustDepthImg=Mat(cvDepthImg,Rect(ADRESOLUTION)); 251 cvAdjustColorImg=Mat(cvColorImg,Rect(ADRESOLUTION)); 252 if(_kbhit())//_kbhit() 253 254 n++; 255 sprintf(DepthFilename,"depthimage%03d.jpg",n); 256 sprintf(ColorFilename,"colorimage%03d.jpg",n); 257 imwrite(DepthFilename,cvAdjustDepthImg); 258 imwrite(ColorFilename,cvAdjustColorImg); 259 cout << "已经保存了" << n << "副图片" << endl; 260 system("PAUSE"); 261 262 imshow("depth",cvAdjustDepthImg); 263 imshow("color",cvAdjustColorImg); 264 waitKey(2); 265 266 267 268 // 269 void OringinRecord() 270 271 int n=0; 272 int operation; 273 char DepthFilename[20]; 274 char ColorFilename[20]; 275 while(true) 276 277 n++; 278 sprintf(DepthFilename,"oringindepthvideo%03d.avi",n); 279 sprintf(ColorFilename,"oringincolorvideo%03d.avi",n); 280 VideoWriter colorVideoWriter=VideoWriter(ColorFilename,CV_FOURCC('X','V','I','D'),FPS,se); 281 VideoWriter depthVideoWriter=VideoWriter(DepthFilename,CV_FOURCC('X','V','I','D'),FPS,se); 282 namedWindow("彩色图",1); 283 namedWindow("深度图",1); 284 while(true) 285 286 if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK ) 287 288 // convert data into OpenCV type 289 cv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() ); 290 cv::cvtColor( cvRGBImg, cvColorImg, CV_RGB2BGR ); 291 colorVideoWriter.write(cvColorImg); 292 cv::imshow( "彩色图", cvColorImg ); 293 294 if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ) 295 296 cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() ); 297 cvRawImg16U.convertTo( cvDepthImg, CV_8UC1, 255.0/(oniDepthStream.getMaxPixelValue())); 298 //【5】 299 // convert depth image GRAY to BGR 300 cv::cvtColor(cvDepthImg,cvDepthImg,CV_GRAY2BGR); 301 depthVideoWriter.write(cvDepthImg); 302 cv::imshow( "深度图", cvDepthImg ); 303 304 int key; 305 key=waitKey(100); 306 if(key==32) 307 308 break; 309 310 311 destroyWindow("彩色图"); 312 destroyWindow("深度图"); 313 cout << "已经录制了" << n << "段视频" << endl; 314 315 316 /* 317 * === FUNCTION ====================================================================== 318 * Name: removenoiserecord 319 * Description: 包含录取彩色图像,去噪后的灰度图像,未去噪声的深度图像和去噪后的深度图像 320 * 321 * ===================================================================================== 322 */ 323 void RemoveNoiseRecord() 324 325 int n=0; 326 327 char DepthFilename[20]; 328 char ColorFilename[20]; 329 char removeDepthFilename[20]; 330 char removeColorFilename[20]; 331 Mat t1; 332 Mat t2; 333 while(true) 334 335 n++; 336 sprintf(removeDepthFilename,"removedepthvideo%03d.avi",n); 337 sprintf(removeColorFilename,"removecolorvideo%03d.avi",n); 338 sprintf(DepthFilename,"oringindepthvideo%03d.avi",n); /* 未去噪的深度图像 */ 339 sprintf(ColorFilename,"oringincolorvideo%03d.avi",n); /* 未去噪声的彩色图像 */ 340 341 342 VideoWriter removecolorVideoWriter=VideoWriter(removeColorFilename,CV_FOURCC('X','V','I','D'),FPS,recordse); 343 VideoWriter removedepthVideoWriter=VideoWriter(removeDepthFilename,CV_FOURCC('X','V','I','D'),FPS,recordse); 344 345 VideoWriter colorVideoWriter=VideoWriter(ColorFilename,CV_FOURCC('X','V','I','D'),FPS,se); 346 VideoWriter depthVideoWriter=VideoWriter(DepthFilename,CV_FOURCC('X','V','I','D'),FPS,se);// 347 namedWindow("去噪灰度图",1); 348 namedWindow("去噪深度图",1); 349 namedWindow("未去噪彩色图",1); 350 namedWindow("未去噪深度图",1); 351 while(true) 352 353 if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK ) 354 355 // convert data into OpenCV type 356 cv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() ); 357 cv::cvtColor( cvRGBImg, cvColorImg2, CV_RGB2BGR ); 358 cvColorImg2=Mat(cvColorImg2,Rect(ADRESOLUTION)); 359 colorVideoWriter.write(cvColorImg2); 360 cv::imshow( "未去噪彩色图", cvColorImg2 ); 361 cvtColor(cvRGBImg,cvColorImg,CV_RGB2GRAY); 362 //colorVideoWriter.write(cvColorImg); 363 364 365 366 if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ) 367 368 cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() ); 369 cvRawImg16U.convertTo( cvDepthImg, CV_8UC1, 255.0/(oniDepthStream.getMaxPixelValue())); 370 cv::cvtColor(cvDepthImg,cvDepthImg2,CV_GRAY2BGR); 371 cvDepthImg2=Mat(cvDepthImg2,Rect(ADRESOLUTION)); 372 depthVideoWriter.write(cvDepthImg2); 373 cv::imshow( "未去噪深度图", cvDepthImg2 ); 374 375 //调整图像尺寸 376 cvAdjustDepthImg=Mat(cvDepthImg,Rect(ADRESOLUTION)); 377 cvAdjustColorImg=Mat(cvColorImg,Rect(ADRESOLUTION)); 378 379 380 381 RemoveNoise(); 382 cvtColor(cvAdjustColorImg,cvAdjustColorImg,CV_GRAY2BGR); 383 cvtColor(cvAdjustDepthImg,cvAdjustDepthImg,CV_GRAY2BGR); 384 removecolorVideoWriter.write(cvAdjustColorImg); 385 removedepthVideoWriter.write(cvAdjustDepthImg); 386 imshow("去噪灰度图",cvAdjustColorImg); 387 imshow("去噪深度图",cvAdjustDepthImg); 388 int key; 389 key=waitKey(10); 390 if(key==32) 391 392 break; 393 394 395 destroyWindow("去噪灰度图"); 396 destroyWindow("去噪深度图"); 397 destroyWindow("未去噪彩色图"); 398 destroyWindow("未去噪深度图"); 399 400 cout << "已经录制了" << n << "段视频" << endl; 401 402 403 // 404 void RemoveNoise() 405 406 clock_t start,finish; 407 double totaltime=0.0; 408 start=clock(); 409 410 for(int j=(cvAdjustDepthImg.rows-1);j>=0;j--)//depthImage.rows,行数 411 412 const uchar* mj=cvAdjustDepthImg.ptr<uchar>(j); 413 for(int i=(cvAdjustDepthImg.cols-1);i>=0;i--)//depthImage.cols,列数 414 415 //修复空洞 416 if(mj[i]<=GRAYTH) 417 418 uchar colorpixel=cvAdjustColorImg.at<uchar>(j,i); 419 bool reResult=false; 420 //分黑色和非黑色区域分开处理 421 if(colorpixel<GRAYTH*5) 422 423 //像素点修复 424 for(int k=1;k<REPAIRERANGE*3;k++) 425 426 reResult=pixelRepaire(i,j,k); 427 if(reResult) 428 break; 429 430 //go down 431 if(!reResult) 432 433 for(int k=1;k<=30;k++) 434 435 if((j+k)<440) 436 437 if(cvAdjustDepthImg.at<uchar>(j+k,i)>GRAYTH) 438 439 cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j+k,i); 440 reResult=true; 441 break; 442 443 444 else 445 446 break; 447 处理深度数据 - KinectProcessing教程:如何使用Kinect V2做交互设计 - 获取Kinect全部图像信息