PCD文件无法正确查看颜色信息

时间:2017-11-03 19:35:03

标签: opencv point-cloud-library point-clouds openni

操作系统和版本:Linux 14.04

我尝试从Kinect v2录制PCD文件。

我使用的3个主要库是OpenCV,PCL,OpenNi

我可以成功记录PCD文件,但是,当我检查PCD文件时,颜色信息只显示在一个小范围内,无法覆盖所有图像。深度信息似乎没问题。

我的部分代码如下所示:



 // open device      
        Device device;    
        result = device.open( openni::ANY_DEVICE );   
        CheckOpenNIError( result, "open device" );  
      
      
        // create depth stream     
        VideoStream oniDepthStream;    
        result = oniDepthStream.create( device, openni::SENSOR_DEPTH );  
        CheckOpenNIError( result, "create depth stream" );  
        
        // set depth video mode    
        VideoMode modeDepth;    
        modeDepth.setResolution( 640, 480 );    
        modeDepth.setFps( 30 );    
        modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );    
        oniDepthStream.setVideoMode(modeDepth);    
        // start depth stream    
        result = oniDepthStream.start();   
        CheckOpenNIError( result, "start depth stream" );  
         
        // create color stream    
        VideoStream oniColorStream;    
        result = oniColorStream.create( device, openni::SENSOR_COLOR );    
        CheckOpenNIError( result, "create color stream" );  
        // set color video mode    
        VideoMode modeColor;    
        modeColor.setResolution( 640, 480 );    
        modeColor.setFps( 30 );    
        modeColor.setPixelFormat( PIXEL_FORMAT_RGB888 );    
        oniColorStream.setVideoMode( modeColor);   
        // start color stream    
        result = oniColorStream.start();   
        CheckOpenNIError( result, "start color stream" );  
          
        int count = 0;  
        while(true)  
        {  
            // read frame    
            if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK )    
            {    
                // convert data into OpenCV type    
                Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() );    
                cvtColor( cvRGBImg, cvBGRImg, CV_RGB2BGR );    
                imshow( "image", cvBGRImg );    
            }    
      
            if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK )    
            {    
                Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() );    
                cvRawImg16U.convertTo( cvDepthImg, CV_8U, 255.0/(oniDepthStream.getMaxPixelValue()));      
                imshow( "depth", cvDepthImg );    
            }   
      
		cout<< oniDepthImg.getWidth() << "  d "<<oniDepthImg.getHeight() <<endl;
		cout<< oniColorImg.getHeight()<< "  c  "<< oniColorImg.getWidth() <<endl;
        char input = waitKey(1);   
   
            // capture  depth and color data     
            if( input == 'c' )  
            {  
                //get data  
                DepthPixel *pDepth = (DepthPixel*)oniDepthImg.getData();  
                //create point cloud  
                cloud.width = oniDepthImg.getWidth();  
                cloud.height = oniDepthImg.getHeight();  
                cloud.is_dense = false;  
                cloud.points.resize(cloud.width * cloud.height); 
 
                color_cloud.width = oniDepthImg.getWidth();  
                color_cloud.height = oniDepthImg.getHeight();  
                color_cloud.is_dense = false;  
                color_cloud.points.resize(color_cloud.width * color_cloud.height);  
      
                //test = cvCreateImage(cvSize(cloud.width,cloud.height),IPL_DEPTH_8U,3);  
                IplImage temp11 = (IplImage)cvBGRImg;  
                //test2 = &IplImage(cvBGRImg);  
                test2 = &temp11;              
      
                for(i=0;i<oniDepthImg.getHeight();i++)  
                {  
                     for(j=0;j<oniDepthImg.getWidth();j++)  
                     {  
                         float k = i;    
                         float m = j;   
                         xx = pDepth[i*oniDepthImg.getWidth()+j];  
                         CoordinateConverter::convertDepthToWorld (oniDepthStream,m,k,xx,&x,&y,&z);   
              /*           cloud[i*cloud.width+j].x = x/1000;  
                         cloud[i*cloud.width+j].y = y/1000;  
                         cloud[i*cloud.width+j].z = z/1000;  
  		*/    
                         color_cloud[i*cloud.width+j].x = x/1000;  
                         color_cloud[i*cloud.width+j].y = y/1000;  
                         color_cloud[i*cloud.width+j].z = z/1000;  
                         color_cloud[i*cloud.width+j].b = (uint8_t)test2->imageData[i*test2->widthStep+j*3+0];  
                         color_cloud[i*cloud.width+j].g = (uint8_t)test2->imageData[i*test2->widthStep+j*3+1];  
                         color_cloud[i*cloud.width+j].r = (uint8_t)test2->imageData[i*test2->widthStep+j*3+2];  
                     }  
                 }  
                  
                cout<<"the "<<count<<" is saved"<<endl;  
       //         sprintf(filename,"./data/%d.pcd",count);  
       //         pcl::io::savePCDFileBinaryCompressed(filename,cloud);  
       //         cerr<<"Saved "<<cloud.points.size()<<" data points to xyz pcd."<<endl;  
                sprintf(filename,"./data/color_%d.pcd",count);  
                pcl::io::savePCDFileBinaryCompressed(filename,color_cloud);  
                cerr<<"Saved "<<color_cloud.points.size()<<" data points to xyzrgb pcd."<<endl;  
                sprintf(filename,"./data/color_%d.jpg",count);  
                imwrite(filename,cvBGRImg);  
                sprintf(filename,"./data/depth_%d.jpg",count++);  
                imwrite(filename,cvDepthImg);   
            }  
        }  
&#13;
&#13;
&#13;

我输出oniDepthImg的宽度和高度,这些是640 480; oniColorImg的宽度和高度,这些是1080 1920.我想也许当我把它们组合到PCD文件时我丢失了一些颜色数据。

有谁知道问题是什么,并给我一些建议? 任何想法都表示赞赏!

0 个答案:

没有答案