1. 程式人生 > >單目深度估計-基於Caffe

單目深度估計-基於Caffe

add normal mea opencv2 and git lease 來源 layers

本文的思路主要來源於[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