单目深度估计-基于Caffe

本文的思路主要来源于[1],模型和参数来源于[2],这里只是在Caffe的框架下用C++实现了,效果还可以,目前打算做个三维点云显示,做好了再上传吧.

这里先贴代码吧,最近比较忙,就先不讲具体的,大家先看吧,有时间了再写思路吧

#include <vector>
#include <string.h>
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace caffe;
using namespace std;

int main(){ 
  
  /*Load the network*/
  string proto = "scripts/deploy_resnet50by2_pool.prototxt";
  Phase phase = TEST;
  Caffe::set_mode(Caffe::GPU);
  Caffe::SetDevice(0);
  boost::shared_ptr< Net<float> > net(new caffe::Net<float>(proto, phase));
  string model = "model/train_iter_40000.caffemodel";
  net->CopyTrainedLayersFrom(model);
  
  CHECK_EQ(net->num_inputs(), 1) << "Network should have exactly one input.";
  CHECK_EQ(net->num_outputs(), 1) << "Network should have exactly one output.";
  
  /*Convert image to input blob. Blob: num, channel, height, width*/
  Blob<float>* input_blob = net->input_blobs()[0];
  int input_channel = input_blob->channels();
  int input_height = input_blob->height();
  int input_width = input_blob->width();
  cout << "The size of input image should be " << input_width << "*" << input_height << endl;
  
  vector<cv::Mat> input_channels;  //The value of input image's each channel  
  float* input_data = input_blob->mutable_cpu_data();
  for(int i=0;i<input_channel;i++){
    cv::Mat channel(input_height, input_width, CV_32FC1, input_data);
    input_channels.push_back(channel);
    input_data += input_width*input_height;
  }
  
  string imgPath = "images/1.png";
  cv::Mat image = cv::imread(imgPath);
  
  cv::Size input_size = cv::Size(input_width, input_height);
  cv::Mat image_resized;  
  cv::resize(image, image_resized, input_size);
  cv::imshow("image", image_resized);
  cv::waitKey(0);
  
  cv::Mat image_float;
  image_resized.convertTo(image_float, CV_32FC3);
  cv::Mat image_normalized;
  cv::Mat mean(input_height, input_width, CV_32FC3, cv::Scalar(104,117,123));
  cv::subtract(image_float, mean, image_normalized);
  cv::split(image_normalized, input_channels);
  
  net->Forward();
  
  /*Convert output blob to image*/
  Blob<float>* output_blob = net->output_blobs()[0];
  int output_height = output_blob->height();
  int output_width = output_blob->width();
  float *output_data = output_blob->mutable_cpu_data();
  cv::Mat depth(output_height, output_width, CV_32FC1, output_data);
  cout << depth.at<float>(5,5);
  
  cv::Mat depth_uint8;
  depth.convertTo(depth_uint8, CV_8UC1);
  
  cv::imshow("depth", depth_uint8);
  cv::waitKey(0);
  
  /*Generate 3D image with rgb image and estimated depth*/
  
  
  return 0;
}

下面是我的CmakeList.txt,代码中需要的模型.参数,和图片都是包含在[2]里面的.

cmake_minimum_required( VERSION 2.8 )
project( resTest )

add_executable( resTest resTest.cpp )
set( CMAKE_CXX_FLAGS "-std=c++11" )

include_directories( /home/ai/Programs/Caffe/include 
  /home/ai/Programs/Caffe/.build_release/src
  /usr/local/cuda/include
  /usr/local/include
  /usr/include
  
  )
target_link_libraries( resTest
  /home/ai/Programs/Caffe/.build_release/lib/libcaffe.so 
  /usr/lib/x86_64-linux-gnu/libopencv_highgui.so
  /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so
  /usr/lib/x86_64-linux-gnu/libopencv_core.so
  /usr/lib/x86_64-linux-gnu/libboost_system.so  
  /usr/lib/x86_64-linux-gnu/libglog.so 
  )

参考文献

[1]http://blog.csdn.net/jiongnima/article/details/70197866

[2]https://github.com/Ravi-Garg/Unsupervised_Depth_Estimation


 

2017年9月20日更新:效果演示,下图分别原图和估计出的深度图

最后参考高翔博士博客[3]上的代码绘制三维点云,但是似乎有些问题(还在解决),下面是代码和效果

 typedef pcl::PointXYZRGBA PointT;
 typedef pcl::PointCloud
<PointT> PointCloud; const double camera_factor = 1000.00; const double camera_cx = 690.00; const double camera_cy = 233.19; const double camera_fx = 984.24; const double camera_fy = 980.81; PointCloud::Ptr cloud( new PointCloud); for(int i=0;i<depth.rows;i++){ for(int j=0;j<depth.cols;j++){ float d = depth.ptr<float>(i)[j]; PointT p; p.z = double(d) / camera_factor; p.x = (i - camera_cx)*p.z / camera_fx; p.y = (j - camera_cy)*p.z / camera_fy; p.b = image.ptr<uchar>(i)[j*3]; p.g = image.ptr<uchar>(i)[j*3+1]; p.r = image.ptr<uchar>(i)[j*3+2]; cloud->points.push_back( p ); } } cloud->height = 1; cloud->width = cloud->points.size(); cout << "Point cloud size = " << cloud->points.size() << endl; cloud->is_dense = false; pcl::io::savePCDFile("./pointcloud.pcd", *cloud); cloud->points.clear(); cout << "Point cloud saved." << endl;

大家如果了解的话,希望能给出建议

参考文献:

[3]http://www.cnblogs.com/gaoxiang12/p/4652478.html

posted on 2017-09-19 16:50  萝卜丶爱  阅读(1808)  评论(5编辑  收藏  举报

导航