Search code examples
c++opencvwebots

How can I encode and decode the depth image with opencv? I use the following code to get a depth image in webots, but fail. Thanks a lot


    // `s` is the webots's rangefinder
    auto buffer = (float*)s->getRangeImage();
    cv::Mat img(cv::Size(width, height), CV_32FC1, buffer);  // image data is float*

    // check and success.
    // imshow("pic", img); 
    // cvWaitKey(0);

    // encode:
    std::vector<uchar> newBuffer;
    cv::imencode(".jpg", img, newBuffer);

    // decode
    auto img_decode = cv::imdecode(newBuffer, cv::IMREAD_UNCHANGED);

    // fail and get a black image
    imshow("pic", img_decode);
    cvWaitKey(0);

I have try to use other params in function cv::imdecode like cv::IMREAD_ANYDEPTH but fail. I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.


Solution

  • If your data is real, i.e. floating point, and single channel, you could try writing to a PFM or TIFF format image which will support that data type.

    As @wohlstad mentions in the comments, JPEG is uint8 so only supports integers in the range 0..255.