資源簡介
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()?<“\t“?<(x).channels()?<“\t“?<(x).height()?<“\t“?<(x).width()?<“\n“;
#define?PRINT_SHAPE2(x)?\
????std::cout?<(x)->num()?<“\t“?<(x)->channels()?<“\t“?<(x)->height()?<“\t“?<(x)->width()?<“\n“;
#define?PRINT_DATA(x)?\
????std::cout?<(x)[0]?<“\t“?<(x)[1]?<“\t“<<(x)[2]<<“\n“;
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通道數(shù)據(jù)分開存儲?*/
??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
評論
共有 條評論