單目深度估計-基於Caffe

本文的思路主要來源於[1],模型和參數來源於[2],這裏只是在Caffe的框架下用C++實現了,效果還能夠,目前打算作個三維點雲顯示,作好了再上傳吧.html

這裏先貼代碼吧,最近比較忙,就先不講具體的,你們先看吧,有時間了再寫思路吧linux

#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]裏面的.c++

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 
  )

參考文獻git

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

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


 

2017年9月20日更新:效果演示,下圖分別原圖和估計出的深度圖ui

最後參考高翔博士博客[3]上的代碼繪製三維點雲,可是彷佛有些問題(還在解決),下面是代碼和效果spa

 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;

你們若是瞭解的話,但願能給出建議.net

參考文獻:3d

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

相關文章
相關標籤/搜索