16位深度图像损坏

时间:2020-03-17 06:29:00

标签: c++ multithreading opencv queue realsense

我正在使用realsense D435并获取16位深度数据并将其转换为opencv 16位Mat,并且我用c ++编写了自定义队列,在其中推送所有帧,并从队列16中弹出时位数据已损坏。在将帧推入队列时,我正在将其保存到磁盘中,而当从队列中弹出帧时,我也在将帧存入disk.so,所以从那里我知道在弹出时帧已损坏。 我尝试使用8位数据的同一件事意味着在转换为opencv mat时,我转换为8位Mat(而不是16位Mat),并弹出8位数据。数据不会损坏。

代码:

rs2::frameset data = pipe.wait_for_frames(); 
data = align.process(data);         
rs2::frame depth = data.get_depth_frame();
rs2::frame color = data.get_color_frame();

cv::Mat depth_image;        
cv::Mat fin_depth_image(cv::Size(WIDTH,HEIGHT), CV_16UC1, (void*)depth.get_data(), cv::Mat::AUTO_STEP);
cv::Mat color_image(cv::Size(WIDTH,HEIGHT), CV_8UC3, (void*)color.get_data(), cv::Mat::AUTO_STEP);
fin_depth_image.convertTo(depth_image,CV_8UC1);

cv::split(color_image,channels);        
channels.push_back(depth_image);
cv::merge(channels,fin_rgbd_img);           

camera_queue.push(std::make_pair(fin_rgbd_img,fin_depth_image));

自定义队列代码:

inline void push(const T& elem) {
    {
        std::unique_lock<std::mutex> lock(_mutex);

        // wait for timeout while the queue is full
        _not_full.wait_for(lock,std::chrono::milliseconds(5));

        // If the queue is full, remove the old item and add the new item
        if (_queue.size() >= _capacity)
        {
            _queue.pop();
        }
        _queue.push(elem);
    }
    _not_empty.notify_all();
}

inline bool pop(T &elem) {
    bool status = false;
    {
        std::unique_lock<std::mutex> lock(_mutex);

        // wait for timeout while the queue is empty
        _not_empty.wait_for(lock,std::chrono::milliseconds(5));

        if (_queue.size() > 0)
        {
            elem = _queue.front();
            _queue.pop();
            status = true;
        }
    }
    _not_full.notify_one();
    return status;
}

因此,从上方您可以看到我正在队列中推送fin_rgbd_img,fin_depth_image。 fin_rgbd_img是8位4通道Mat,fin_depth_image是16位Mat。所以在推动的同时,我还通过写入功能保存了这个16位深度的Mat,这时候我就拥有了适当的图像。弹出此16位Mat时损坏了。

下面是进入队列时的png图像,我正在保存到磁盘,这是正确的,同时弹出充满0的完整黑色图像时, enter image description here

0 个答案:

没有答案