单目深度估计-基于Caffe

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单目深度估计-基于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

以上是关于单目深度估计-基于Caffe的主要内容,如果未能解决你的问题,请参考以下文章

基于单目RGB的3D人体姿态估计

单目深度估计与伪雷达点云可视化

单目稠密深度图(vins mono可用)

室内单目深度估计-4

[算法整理]可能是最全的无监督/自监督单目深度估计方法汇总 - Part1:视频序列篇

ICCV 2021 | R-MSFM: 用于单目深度估计的循环多尺度特征调制