densedepth.cpp 1.59 KB
#include "common.h"
#include "cv_func.h"
#include "pcl_func.h"

/*
[input]
argc    = 2
argv[0] = program
argv[1] = densedepth.png (320 x 240)

[output]
interpolated_densedepth.png (640 x 480)
densedepth.pcd
*/

int main(int argc, char** argv)
{
    if(argc != 2)
    {
        std::cout << "[Error] Program Usage: [./densedepth] [densedepth.png]" << std::endl;
        return -1;
    }

    //intrinsic setup
    camInfo cam;
    realsenseSetUp(cam);
    
    cv::Mat img, interpolated_img;
    img = cv::imread(argv[1], cv::IMREAD_ANYDEPTH);
    //std::cout << type2str(img.type()) << std::endl; -> 16UC1
    
    //interpolation to (320, 240) -> (640, 480)
    cv::resize(img, interpolated_img, cv::Size(640, 480), cv::INTER_LINEAR);
    std::string inputPath = argv[1];
    std::size_t div = inputPath.find_last_of("/\\");
    std::string outputPath = inputPath.substr(0, div + 1);
    cv::imwrite(outputPath + "interpolated_densedepth.png", interpolated_img);

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

    for(int i = 0; i < interpolated_img.cols; i++)
    {
        for(int j = 0; j < interpolated_img.rows; j++)
        {
            auto d = (-interpolated_img.at<u_int16_t>(j, i)) / 1000.0; // closer - darker reversed
            double x = (i - cam.cx) * d / cam.fx;
            double y = (j - cam.cy) * d / cam.fy;
            double z = d;

            pcl::PointXYZ pt;
            pt.x = x;
            pt.y = y;
            pt.z = z;
            cloud->push_back(pt);
        }
    }
    pcl::io::savePCDFile(outputPath + "densedepth.pcd", *cloud);

    return 0;
}