我的第一个项目:用kinect录视频库

kinect深度视频去噪

kinectmod32.dll

http://pan.baidu.com/s/1DsGqX

下载后改名kinect.dll

替换掉RedistOpenNI2Driverskinect.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                             }
448                         }
449                     }
450                     //go up
451                     if(!reResult)
452                     {
453                         for(int k=1;k<=30;k++)
454                         {
455                             if((j-k)>=0)
456                             {
457                                 if(cvAdjustDepthImg.at<uchar>(j-k,i)>GRAYTH)
458                                 {
459                                     cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j-k,i);
460                                     reResult=true;
461                                     break;
462                                 }
463                             }
464                             else
465                             {
466                                 break;
467                             }
468                         }
469                     }
470                 }
471                 else
472                 {
473                     //消除物体边界的噪音
474                     for(int k=1;k<30;k++)
475                     {
476                         if((i+k)<590 && !reResult)
477                         {
478                             if(abs(cvAdjustColorImg.at<uchar>(j,i+k)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(j,i+k)>GRAYTH)
479                             {
480                                 cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j,i+k);
481                                 reResult=true;
482                             }
483                         }
484                         else
485                         {
486                             break;
487                         }
488                     }    
489                 }
490                 //像素点周围寻找可利用点(考虑彩色图像是否匹配)
491                 if(!reResult)
492                 {
493                     for(int k=1;k<REPAIRERANGE;k++)
494                     {
495                         reResult=pixelRepaire(i,j,k);
496                         if(reResult)
497                             break;
498                     }
499                 }
500                 //范围寻找可利用点(不管彩色图像是否匹配)
501                 if(!reResult)
502                 {
503                     for(int k=0;k<REPAIRERANGE*3;k++)
504                     {
505                         reResult=rangeRepaire(i,j,k);
506                         if(reResult)
507                             break;
508                     }
509                 }
510             }
511         }
512     }
513 
514 
515     finish=clock();
516     totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
517     //cout<<"
此帧图像的去噪时间为"<< totaltime << "秒!"<< endl;
518 }
519 //像素点修复
520 bool pixelRepaire(int i,int j,int repaireRange)
521 {
522     uchar colorpixel=cvAdjustColorImg.at<uchar>(j,i);
523     int x=0;
524     int y=0;
525     int n=0;//符合要求的点的数量
526     int sum=0;//符合要求的点的灰度值的和
527     for(y=j-repaireRange;y<=j+repaireRange;y++)
528     {
529         if(y>=0 && y<440)
530         {
531             //上下边界寻找
532             if(y==(j-repaireRange) ||  y==(j+repaireRange))
533             {
534                 for(x=i-repaireRange;x<=i+repaireRange;x++)
535                 {
536                     if(x>=0 && x<590)
537                     {
538                         if(abs(cvAdjustColorImg.at<uchar>(y,x)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(y,x)>GRAYTH)
539                         {
540                             n++;
541                             sum=sum+cvAdjustDepthImg.at<uchar>(y,x);
542                         }
543                     }
544 
545                 }
546             }
547             //左右边界寻找
548             else
549             {
550                 //
551                 x=i-repaireRange;
552                 if(x>=0 && x<590)
553                 {
554                     if(abs(cvAdjustColorImg.at<uchar>(y,x)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(y,x)>GRAYTH)
555                     {
556                         n++;
557                         sum=sum+cvAdjustDepthImg.at<uchar>(y,x);
558                     }
559                 }
560                 //
561                 x=i+repaireRange;
562                 if(x>=0 && x<590)
563                 {
564                     if(abs(cvAdjustColorImg.at<uchar>(y,x)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(y,x)>GRAYTH)
565                     {
566                         n++;
567                         sum=sum+cvAdjustDepthImg.at<uchar>(y,x);
568                     }
569                 }
570             }
571         }
572         
573     }
574     if(n<repaireRange*2)
575     {return false;}
576     else
577     {
578         cvAdjustDepthImg.at<uchar>(j,i)=(uchar)(sum/n);
579         return true;
580     }
581 
582 }
583 //范围性修复
584 bool rangeRepaire(int i,int j,int repaireRange)
585 {
586     uchar colorpixel=cvAdjustColorImg.at<uchar>(j,i);
587     int x=0;
588     int y=0;
589     int n=0;
590     int sum=0;
591     for(y=j-repaireRange;y<=j+repaireRange;y++)
592     {
593         if(y>=0 && y<440)
594         {
595             for(x=i-repaireRange;x<=i+repaireRange;x++)
596             {
597                 if(x>=0 && x<590)
598                 {
599                     if(cvAdjustDepthImg.at<uchar>(y,x)>GRAYTH)
600                     {
601                         n++;
602                         sum=sum+cvAdjustDepthImg.at<uchar>(y,x);
603                     }
604                 }
605             }
606         }
607     }
608     if(n<=repaireRange*2)
609     {
610         return false;
611     }
612     else
613     {
614         cvAdjustDepthImg.at<uchar>(j,i)=(uchar)(sum/n);
615         return true;
616     }
617 }
618 //
619 void RepaireTest()
620 {
621     cvAdjustColorImg=imread("colorimage005.jpg",0);
622     cvAdjustDepthImg=imread("depthimage005.jpg",0);
623     Mat oriDepthImg=cvAdjustDepthImg.clone();
624     clock_t start,finish;
625     double totaltime=0.0;
626     start=clock();
627     for(int j=(cvAdjustDepthImg.rows-1);j>=0;j--)//depthImage.rows,行数
628     {
629         const uchar* mj=cvAdjustDepthImg.ptr<uchar>(j);
630         for(int i=(cvAdjustDepthImg.cols-1);i>=0;i--)//depthImage.cols,列数
631         {
632             //修复空洞
633             if(mj[i]<=GRAYTH)
634             {
635                 uchar colorpixel=cvAdjustColorImg.at<uchar>(j,i);
636                 bool reResult=false;
637                 //分黑色和非黑色区域分开处理
638                 if(colorpixel<GRAYTH*5)
639                 {
640                     //像素点修复
641                     for(int k=1;k<REPAIRERANGE*3;k++)
642                     {
643                         reResult=pixelRepaire(i,j,k);
644                         if(reResult)
645                             break;
646                     }
647                     //go down
648                     if(!reResult)
649                     {
650                         for(int k=1;k<=30;k++)
651                         {
652                             if((j+k)<440)
653                             {
654                                 if(cvAdjustDepthImg.at<uchar>(j+k,i)>GRAYTH)
655                                 {
656                                     cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j+k,i);
657                                     reResult=true;
658                                     break;
659                                 }
660                             }
661                             else
662                             {
663                                 break;
664                             }
665                         }
666                     }
667                     //go up
668                     if(!reResult)
669                     {
670                         for(int k=1;k<=30;k++)
671                         {
672                             if((j-k)>=0)
673                             {
674                                 if(cvAdjustDepthImg.at<uchar>(j-k,i)>GRAYTH)
675                                 {
676                                     cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j-k,i);
677                                     reResult=true;
678                                     break;
679                                 }
680                             }
681                             else
682                             {
683                                 break;
684                             }
685                         }
686                     }
687                 }
688                 else
689                 {
690                     //消除物体边界的噪音
691                     for(int k=1;k<30;k++)
692                     {
693                         if((i+k)<590 && !reResult)
694                         {
695                             if(abs(cvAdjustColorImg.at<uchar>(j,i+k)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(j,i+k)>GRAYTH)
696                             {
697                                 cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j,i+k);
698                                 reResult=true;
699                             }
700                         }
701                         else
702                         {
703                             break;
704                         }
705                     }    
706                 }
707                 //像素点周围寻找可利用点(考虑彩色图像是否匹配)
708                 if(!reResult)
709                 {
710                     for(int k=1;k<REPAIRERANGE;k++)
711                     {
712                         reResult=pixelRepaire(i,j,k);
713                         if(reResult)
714                             break;
715                     }
716                 }
717                 //范围寻找可利用点(不管彩色图像是否匹配)
718                 if(!reResult)
719                 {
720                     for(int k=0;k<REPAIRERANGE*3;k++)
721                     {
722                         reResult=rangeRepaire(i,j,k);
723                         if(reResult)
724                             break;
725                     }
726                 }
727              }
728         }
729     }
730 //     for(int j=(cvAdjustDepthImg.rows-1);j>=0;j--)//depthImage.rows,行数
731 //     {
732 //         const uchar* mj=cvAdjustDepthImg.ptr<uchar>(j);
733 //         for(int i=(cvAdjustDepthImg.cols-1);i>=0;i--)//depthImage.cols,列数
734 //         {
735 //             //修复空洞
736 //             if(mj[i]<=GRAYTH)
737 //             {
738 //                 uchar colorpixel=cvAdjustColorImg.at<uchar>(j,i);
739 //                 bool reResult=false;
740 //                 //从右向左寻找可利用点 
741 //                 for(int k=1;k<30;k++)
742 //                 {
743 //                     if((i+k)<590 && !reResult)
744 //                     {
745 //                         if(abs(cvAdjustColorImg.at<uchar>(j,i+k)-colorpixel)<=COLORTH && cvAdjustDepthImg.at<uchar>(j,i+k)>GRAYTH)
746 //                         {
747 //                             cvAdjustDepthImg.at<uchar>(j,i)=cvAdjustDepthImg.at<uchar>(j,i+k);
748 //                             reResult=true;
749 //                         }
750 //                     }
751 //                     else
752 //                     {
753 //                         break;
754 //                     }
755 //                 }    
756 //                 //像素点修复
757 //                 if(!reResult)
758 //                 {
759 //                     for(int k=1;k<REPAIRERANGE;k++)
760 //                     {
761 //                         reResult=pixelRepaire(i,j,k);
762 //                         if(reResult)
763 //                             break;
764 //                     }
765 //                 }
766 //             }
767 //         }
768 //     }
769     //ccc=cvAdjustDepthImg.clone();
770     /*
771     for(int j=(cvAdjustDepthImg.rows-1);j>=0;j--)//depthImage.rows,行数
772     {
773         const uchar* mj=cvAdjustDepthImg.ptr<uchar>(j);
774         for(int i=(cvAdjustDepthImg.cols-1);i>=0;i--)//depthImage.cols,列数
775         {
776             //修复空洞
777             if(mj[i]<=GRAYTH)
778             {
779                 bool reResult;
780                 for(int k=1;k<=REPAIRERANGE;k++)
781                 {
782                     reResult=rangeRepaire(i,j,k);
783                     if(reResult)
784                         break;
785                 }
786             }
787         }
788     }
789     */
790     //Mat xxx;
791     //Mat zzz=cvAdjustDepthImg.clone();
792     //GaussianBlur(cvAdjustDepthImg,xxx,Size(3,3),0,0);
793     //bilateralFilter(cvAdjustDepthImg,xxx,3,5*2,5*2);
794     finish=clock();
795     totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
796     cout<<"
此帧图像的去噪时间为"<< totaltime << "秒!"<< endl;
797     imshow("去噪深度",cvAdjustDepthImg);
798     imshow("深度",oriDepthImg);
799     imshow("彩色",cvAdjustColorImg);
800     waitKey();
801     //system("PAUSE");
802 
803 }
804 //
805 void Test()
806 {
807     //cvColorImg=imread("colorimage003.jpg",0);
808     //cout << (int)cvColorImg.at<uchar>(369,272) << endl ;
809     //cout << (int)cvColorImg.at<uchar>(73,394) << endl ;
810     //cout << (int)cvColorImg.at<uchar>(428,256) << endl ;
811     //cout << (int)cvColorImg.at<uchar>(326,553) << endl ;
812     cvAdjustColorImg=imread("colorimage003.jpg",0);
813     cvAdjustDepthImg=imread("depthimage003.jpg",0);
814     Mat t;
815     addWeighted(cvAdjustDepthImg,0.8,cvAdjustColorImg,0.2,0,t);
816     imshow("1",t);
817     waitKey();
818 }

在录视频中遇到的一个问题是录成的视频只有6K大小,花了我半天时间,原来是窗口大小的问题。

如果录取的原视频的大小也是要调整为

 RECORDRESOLUTION 590,440的话colorVideoWriter里的参数不能为se,应该为 recordse否则就会有6k问题



本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yuliyang/p/3368711.html