Ask Your Question
0

cv::Mat imread to unsigned short*?

asked 2017-10-10 09:55:48 -0500

antithing gravatar image

I am trying to replace the following function with opencv: (from: https://github.com/stevenlovegrove/Pa...)

  uint64_t loadDepthFromPath(std::string filepath, pangolin::Image<unsigned short> & depth)
    {

        pangolin::TypedImage depthRaw = pangolin::LoadImage(filepath, pangolin::ImageFileTypePng);

        pangolin::Image<unsigned short> depthRaw16((unsigned short*)depthRaw.ptr, depthRaw.w, depthRaw.h, depthRaw.w * sizeof(unsigned short));

        for (unsigned int i = 0; i < 480; i++)
        {
            for (unsigned int j = 0; j < 640; j++)
            {
                depth.RowPtr(i)[j] = depthRaw16(j, i) / 5;
            }
        }

        uint64_t time = getCurrTime();
        return time;
    }

This returns a pangolin::Image<unsigned short>, that is then passed to a function:

initModel(depth.ptr);

where depth.ptr is unsigned short *

I am trying to remove pangolin from this, and use opencv instead.

So I load the same image:

     std::string frame1 = "rgbd_dataset_freiburg1_desk/depth/1305031453.374112.png";

Mat img1;
img1 = imread(frame1, 0);

convert it to unsigned short:

    cv::Mat mat_ushort1(img1.cols,img1.rows, CV_16UC1);
img1.convertTo(mat_ushort1, CV_16UC1);

Then use the pointer to the data to pass to the function:

initModel((unsigned short*)mat_ushort1.data);

The problem is, I see a very different result when I use this method. Where am i going wrong here? How can I replicate the pangolin function with openCv?

thank you.

edit retag flag offensive close merge delete

Comments

1
  • make a quick check, if the original images are already 16u:

    img1 = imread(frame1, -1); cout << img1.type();

    (this will import the images "as they are on disk", while now, you're forcing it into 8bit)

    if the answer is 2, you can skip the convertTo (else you're losing precision, importing as 8u, and converting that)

berak gravatar imageberak ( 2017-10-10 10:09:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-10-10 10:14:53 -0500

antithing gravatar image

updated 2017-10-10 10:35:48 -0500

Thanks! The answer was 2. I have switched to imread(frame1, -1) and initModel((unsigned short*)frame1.data);`, And once i add the scaling that is evident in the pangolin function, it looks identical. Thank you very much for your help!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-10 09:55:48 -0500

Seen: 730 times

Last updated: Oct 10 '17