HED 深度学习边缘提取 C++接口测试程序


HED深度学习边缘提取的c++接口测试程序
资源截图
代码片段和文件信息
#include 
#include 
#include 
#include 
#include 
#include “caffe/data_transformer.hpp“
#include “caffe/util/io.hpp“
#include “caffe/util/math_functions.hpp“
#include “caffe/util/rng.hpp“

using namespace caffe;
using namespace cv;
using namespace std;


#define PRINT_SHAPE1(x) 
    std::cout << (x).num() << “ “ << (x).channels() << “ “ << (x).height() << “ “ << (x).width() << “
“;
#define PRINT_SHAPE2(x) 
    std::cout << (x)->num() << “ “ << (x)->channels() << “ “ << (x)->height() << “ “ << (x)->width() << “
“;
#define PRINT_DATA(x) 
    std::cout << (x)[0] << “ “ << (x)[1] << “ “<<(x)[2]<<“
“;



class EdgeDetect
{
public:
    Mat mean_;
    Size input_geometry;
    int num_channels;
    shared_ptr  > net;

public:
    EdgeDetect(const string& model_file const string& trained_fileconst string& mean_file);
    void WrapInputlayer(std::vector* input_channels);
    void Preprocess(const cv::Mat& imgstd::vector* input_channels);
    void SetMean(const string& mean_file);
};



EdgeDetect::EdgeDetect(const string& model_fileconst string& trained_fileconst string& mean_file)
{
  Caffe::set_mode(Caffe::CPU);



  net.reset(new Net(model_file TEST));

  net->CopyTrainedlayersFrom(trained_file);

  CHECK_EQ(net->num_inputs() 1) << “Network should have exactly one input.“;
  //CHECK_EQ(net->num_outputs() 1) << “Network should have exactly one output.“;

  Blob* input_layer = net->input_blobs()[0];
  num_channels = input_layer->channels();

  CHECK(num_channels == 3 || num_channels == 1)<< “Input layer should have 1 or 3 channels.“;

  input_geometry = cv::Size(input_layer->width() input_layer->height());


  SetMean(mean_file);
}




void EdgeDetect::Preprocess(const cv::Mat& imgstd::vector* input_channels)
{

  cv::Mat sample;

  if (img.channels() == 3 && num_channels == 1)
    cv::cvtColor(img sample CV_BGR2GRAY);
  else if (img.channels() == 4 && num_channels == 1)
    cv::cvtColor(img sample CV_BGRA2GRAY);

  else if (img.channels() == 4 && num_channels == 3)
    cv::cvtColor(img sample CV_BGRA2BGR);
  else if (img.channels() == 1 && num_channels == 3)
    cv::cvtColor(img sample CV_GRAY2BGR);
  else
    sample = img;

  cv::Mat sample_resized;
  if (sample.size() != input_geometry)
    cv::resize(sample sample_resized input_geometry);
  else
    sample_resized = sample;

  imshow(“resize“sample_resized);

  cv::Mat sample_float;
  if (num_channels == 3)
    sample_resized.convertTo(sample_float CV_32FC3);
  else
    sample_resized.convertTo(sample_float CV_32FC1);

  cv::Mat sample_normalized;
  cv::subtract(sample_float mean_ sample_normalized);

  /* 3通道数据分开存储 */
  cv::split(sample_normalized *input_channels);

  CHECK(reinterpret_cast(input_channels->at(0).data) == net->input_blobs()[0]->cpu_data()) << “Input channels are not wrapping the input layer of the network.“;
}

//加载均值文件
void

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      47609  2015-10-01 10:38  HEDC++3063.jpg

     文件       8186  2017-02-28 11:50  HEDC++deploy.prototxt

     文件   58876104  2017-03-31 09:17  HEDC++hed_pretrained_bsds.caffemodel

     文件     786446  2014-02-25 16:24  HEDC++imagenet_mean.binaryproto

     文件      10130  2017-06-21 15:34  HEDC++main.cpp

     目录          0  2017-06-21 15:34  HEDC++

----------- ---------  ---------- -----  ----

             59728475                    6


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)