使用kinect相机保存数据,为了减少保存的数据集量,对图像进行压缩。将彩色图像直接压缩成.mp4格式,此时图像上的一些高频信息会被损失掉。
为了能够让深度图有比较高的保真度,减少深度图上高频信息的损失,我们将16位的深度图,分成高8位和第8位,将低8位保存成mp4视频的形式
将高8位直接保存成单通道的深度图。
下面的函数实现了将一幅CV_16UC1类型的深度图拆分成高8位和低8位,并且将低8位的数据写入三通道的图像中,便于压缩为mp4格式。
int depth2HighLow(cv::Mat Full_Depth, cv::Mat &High_Depth, cv::Mat &Low_Depth)
{if (Full_Depth.type() != CV_16UC1){std::cout << "the input depth must be CV_16UC1" << std::endl;return false;}uint16_t* Pdepth = Full_Depth.ptr<uint16_t>(0);uchar* Phigh = High_Depth.data;uchar* Plow = Low_Depth.data;for (int i = 0; i < Full_Depth.cols*Full_Depth.rows; i++){*Phigh = (*Pdepth) >> 8;Phigh++;*Plow = (*Pdepth) & 0xff;Plow++;*Plow = (*Pdepth) & 0xff;Plow++;*Plow = (*Pdepth) & 0xff;Plow++;Pdepth++;}return true;
}
我们在使用深度图时,需要将高8位和第8位再组合到一起。
void Gray2uint16(cv::Mat High,cv::Mat low,cv::Mat &Depth)
{uint16_t *Pdepth=Depth.ptr<uint16_t>(0);uchar *Phigh=High.data;uchar *Plow=low.data;for (size_t i = 0; i < High.cols*High.rows; i++){*Pdepth=uint16_t((uint16_t(*Phigh)<<8)+(*Plow));Pdepth++;Phigh++;Plow++;}}
下面是关于读取视频的相关代码
int main ( int argc, char** argv )
{std::string dataset_folder = std::string(argv[3]);std::string depth_color_video = dataset_folder + "/" + "color_depth.mp4";std::string high_depth_images = dataset_folder + "/" + "color_depth/";cv::VideoCapture cap(depth_color_video);if(!cap.isOpened()){std::cout<<"opencv mp4 file failed"<<std::endl;return false;}bool have_data = true;int framenum = 0;while(have_data){cv::Mat ImgRead;cap>>ImgRead;if(ImgRead.empty()){std::cout<<"read finished"<<std::endl;have_data = false;continue;}cv::Mat Color_img, Low_depth;cv::Rect rgb_rect(0, 0, ImgRead.cols/2, ImgRead.rows);cv::Rect low_depth_rect(ImgRead.cols/2, 0, ImgRead.cols/2, ImgRead.rows);ImgRead(rgb_rect).copyTo(Color_img);ImgRead(low_depth_rect).copyTo(Low_depth);// cv::imshow("Color_img", Color_img);// cv::imshow("low_depth", Low_depth);// cv::waitKey(2);//read the high depth imagesstd::string HighPath = high_depth_images + "/" + std::to_string(framenum) + ".png";cv::Mat High_depth = cv::imread(HighPath, cv::IMREAD_UNCHANGED);std::cout<<"frame name: "<<HighPath<<std::endl;// cv::imshow("high_depth", High_depth);// cv::waitKey(2);cv::Mat Low_gray;//the type of Low_depth is 8UC3, we change it into 8UC1cv::cvtColor(Low_depth, Low_gray, cv::COLOR_BGR2GRAY);cv::Mat full_depth_gray(Low_gray.size(), CV_16UC1);Gray2uint16(High_depth, Low_gray, full_depth_gray);// cv::imshow("full_depth", full_depth_gray);// cv::waitKey(2);framenum++;}return 0;
}