modify the mean file of images using

pull/276/head
Wangyida 10 years ago
parent 736638710e
commit ba770cd524
  1. 39
      modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp
  2. 81
      modules/cnn_3dobj/samples/classifyIMG_demo.cpp
  3. 17
      modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp
  4. 94
      modules/cnn_3dobj/src/cnn_feature.cpp
  5. 24
      modules/cnn_3dobj/src/cnn_sphereview.cpp

@ -91,7 +91,7 @@ namespace cnn_3dobj
The class create some sphere views of camera towards a 3D object meshed from .ply files @cite hinterstoisser2008panter . The class create some sphere views of camera towards a 3D object meshed from .ply files @cite hinterstoisser2008panter .
*/ */
class CV_EXPORTS_W IcoSphere class CV_EXPORTS_W icoSphere
{ {
private: private:
float X; float X;
@ -104,7 +104,7 @@ The class create some sphere views of camera towards a 3D object meshed from .pl
std::vector<cv::Point3d> CameraPos_temp; std::vector<cv::Point3d> CameraPos_temp;
float radius; float radius;
float diff; float diff;
IcoSphere(float radius_in, int depth_in); icoSphere(float radius_in, int depth_in);
/** @brief Make all view points having the some distance from the focal point used by the camera view. /** @brief Make all view points having the some distance from the focal point used by the camera view.
*/ */
CV_WRAP void norm(float v[]); CV_WRAP void norm(float v[]);
@ -116,7 +116,7 @@ The class create some sphere views of camera towards a 3D object meshed from .pl
CV_WRAP void subdivide(float v1[], float v2[], float v3[], int depth); CV_WRAP void subdivide(float v1[], float v2[], float v3[], int depth);
/** @brief Make all view points having the some distance from the focal point used by the camera view. /** @brief Make all view points having the some distance from the focal point used by the camera view.
*/ */
CV_WRAP static uint32_t swap_endian(uint32_t val); CV_WRAP static uint32_t swapEndian(uint32_t val);
/** @brief Suit the position of bytes in 4 byte data structure for particular system. /** @brief Suit the position of bytes in 4 byte data structure for particular system.
*/ */
CV_WRAP cv::Point3d getCenter(cv::Mat cloud); CV_WRAP cv::Point3d getCenter(cv::Mat cloud);
@ -133,52 +133,47 @@ The class create some sphere views of camera towards a 3D object meshed from .pl
*/ */
}; };
class CV_EXPORTS_W DescriptorExtractor class CV_EXPORTS_W descriptorExtractor
{ {
private: private:
caffe::Net<float>* net_; caffe::Net<float>* net_;
cv::Size input_geometry_; cv::Size input_geometry_;
int num_channels_; int num_channels_;
cv::Mat mean_; cv::Mat mean_;
std::vector<string> labels_; void setMean(const string& mean_file);
void SetMean(const string& mean_file);
/** @brief Load the mean file in binaryproto format. /** @brief Load the mean file in binaryproto format.
*/ */
void WrapInputLayer(std::vector<cv::Mat>* input_channels); void wrapInputLayer(std::vector<cv::Mat>* input_channels);
/** @brief Wrap the input layer of the network in separate cv::Mat objects(one per channel). This way we save one memcpy operation and we don't need to rely on cudaMemcpy2D. The last preprocessing operation will write the separate channels directly to the input layer. /** @brief Wrap the input layer of the network in separate cv::Mat objects(one per channel). This way we save one memcpy operation and we don't need to rely on cudaMemcpy2D. The last preprocessing operation will write the separate channels directly to the input layer.
*/ */
void Preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels, bool mean_subtract); void preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels, int net_ready);
/** @brief Convert the input image to the input image format of the network. /** @brief Convert the input image to the input image format of the network.
*/ */
public: public:
DescriptorExtractor(); std::vector<string> labels_;
void list_dir(const char *path,std::vector<string>& files,bool r); descriptorExtractor();
void listDir(const char *path,std::vector<string>& files,bool r);
/** @brief Get the file name from a root dictionary. /** @brief Get the file name from a root dictionary.
*/ */
bool SetNet(const string& cpu_only, int device_id); bool setNet(const string& cpu_only, int device_id);
/** @brief Initiate a classification structure. /** @brief Initiate a classification structure.
*/ */
bool LoadNet(bool netsetter, const string& model_file, const string& trained_file, const string& mean_file); int loadNet(bool netsetter, const string& model_file, const string& trained_file);
/** @brief Initiate a classification structure. /** @brief Initiate a classification structure.
*/ */
void GetLabellist(const std::vector<string>& name_gallery); int loadNet(bool netsetter, const string& model_file, const string& trained_file, const string& mean_file);
/** @brief Get the label of the gallery images for result displaying in prediction. /** @brief Initiate a classification structure.
*/ */
std::vector<std::pair<string, float> > Classify(const cv::Mat& reference, const cv::Mat& target, int N); void getLabellist(const std::vector<string>& name_gallery);
/** @brief Make a classification. /** @brief Get the label of the gallery images for result displaying in prediction.
*/ */
void Extract(bool net_ready, InputArray inputimg, OutputArray feature, bool mean_subtract, std::string feature_blob); void extract(int net_ready, InputArray inputimg, OutputArray feature, std::string feature_blob);
/** @brief Extract a single featrue of one image. /** @brief Extract a single featrue of one image.
*/ */
std::vector<int> Argmax(const std::vector<float>& v, int N);
/** @brief Find the N largest number.
*/
}; };
//! @} //! @}
} }
} }
#endif /* CNN_3DOBJ_HPP_ */ #endif /* CNN_3DOBJ_HPP_ */
#endif #endif

@ -38,18 +38,53 @@
using namespace cv; using namespace cv;
using namespace std; using namespace std;
using namespace cv::cnn_3dobj; using namespace cv::cnn_3dobj;
/* Return the indices of the top N values of vector v. */
std::vector<int> argmax(const std::vector<float>& v, int N)
{
std::vector<std::pair<float, int> > pairs;
for (size_t i = 0; i < v.size(); ++i)
pairs.push_back(std::make_pair(v[i], i));
std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end());
std::vector<int> result;
for (int i = 0; i < N; ++i)
result.push_back(pairs[i].second);
return result;
};
/* Return the indices of the top N values of vector v. */
std::vector<std::pair<string, float> > classify(const cv::Mat& reference, const cv::Mat& target, int N, std::vector<string> labels_)
{
std::vector<float> output;
for (int i = 0; i < reference.rows; i++)
{
cv::Mat f1 = reference.row(i);
cv::Mat f2 = target;
cv::Mat output_temp = f1-f2;
output.push_back(cv::norm(output_temp));
}
std::vector<int> maxN = argmax(output, N);
std::vector<std::pair<string, float> > predictions;
for (int i = 0; i < N; ++i)
{
int idx = maxN[i];
predictions.push_back(std::make_pair(labels_[idx], output[idx]));
}
return predictions;
};
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
const String keys = "{help | | this demo will convert a set of images in a particular path into leveldb database for feature extraction using Caffe.}" const String keys = "{help | | this demo will convert a set of images in a particular path into leveldb database for feature extraction using Caffe. If there little variance in data such as human faces, you can add a mean_file, otherwise it is not so useful}"
"{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}" "{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}"
"{caffemodel | ../data/3d_triplet_iter_20000.caffemodel | caffe model for feature exrtaction.}" "{caffemodel | ../data/3d_triplet_iter_20000.caffemodel | caffe model for feature exrtaction.}"
"{network_forIMG | ../data/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}" "{network_forIMG | ../data/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}"
"{mean_file | ../data/images_mean/triplet_mean.binaryproto | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images.}" "{mean_file | no | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images. If you want to use the mean file, you can set this as ../data/images_mean/triplet_mean.binaryproto.}"
"{target_img | ../data/images_all/3_13.png | Path of image waiting to be classified.}" "{target_img | ../data/images_all/3_13.png | Path of image waiting to be classified.}"
"{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}" "{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}"
"{num_candidate | 6 | Number of candidates in gallery as the prediction result.}" "{num_candidate | 6 | Number of candidates in gallery as the prediction result.}"
"{device | CPU | device}" "{device | CPU | device}"
"{dev_id | 0 | dev_id}"; "{dev_id | 0 | dev_id}";
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
parser.about("Demo for object data classification and pose estimation"); parser.about("Demo for object data classification and pose estimation");
if (parser.has("help")) if (parser.has("help"))
@ -67,12 +102,16 @@ int main(int argc, char** argv)
string device = parser.get<string>("device"); string device = parser.get<string>("device");
int dev_id = parser.get<int>("dev_id"); int dev_id = parser.get<int>("dev_id");
cv::cnn_3dobj::DescriptorExtractor descriptor; cv::cnn_3dobj::descriptorExtractor descriptor;
bool set_succeed = descriptor.SetNet(device, dev_id); bool set_succeed = descriptor.setNet(device, dev_id);
descriptor.LoadNet(set_succeed, network_forIMG, caffemodel, mean_file); int net_ready;
if (strcmp(mean_file.c_str(), "no") == 0)
net_ready = descriptor.loadNet(set_succeed, network_forIMG, caffemodel);
else
net_ready = descriptor.loadNet(set_succeed, network_forIMG, caffemodel, mean_file);
std::vector<string> name_gallery; std::vector<string> name_gallery;
descriptor.list_dir(src_dir.c_str(), name_gallery, false); descriptor.listDir(src_dir.c_str(), name_gallery, false);
descriptor.GetLabellist(name_gallery); descriptor.getLabellist(name_gallery);
for (unsigned int i = 0; i < name_gallery.size(); i++) { for (unsigned int i = 0; i < name_gallery.size(); i++) {
name_gallery[i] = src_dir + name_gallery[i]; name_gallery[i] = src_dir + name_gallery[i];
} }
@ -81,10 +120,9 @@ int main(int argc, char** argv)
for (unsigned int i = 0; i < name_gallery.size(); i++) { for (unsigned int i = 0; i < name_gallery.size(); i++) {
img_gallery.push_back(cv::imread(name_gallery[i], -1)); img_gallery.push_back(cv::imread(name_gallery[i], -1));
} }
descriptor.FeatureExtract(img_gallery, feature_reference, false, feature_blob); descriptor.extract(net_ready, img_gallery, feature_reference, feature_blob);
std::cout << std::endl << "---------- Prediction for " std::cout << std::endl << "---------- Prediction for " << target_img << " ----------" << std::endl;
<< target_img << " ----------" << std::endl;
cv::Mat img = cv::imread(target_img, -1); cv::Mat img = cv::imread(target_img, -1);
// CHECK(!img.empty()) << "Unable to decode image " << target_img; // CHECK(!img.empty()) << "Unable to decode image " << target_img;
@ -93,15 +131,14 @@ int main(int argc, char** argv)
for (unsigned int i = 0; i < feature_reference.rows; i++) for (unsigned int i = 0; i < feature_reference.rows; i++)
std::cout << feature_reference.row(i) << endl; std::cout << feature_reference.row(i) << endl;
cv::Mat feature_test; cv::Mat feature_test;
descriptor.FeatureExtract(img, feature_test, false, feature_blob); descriptor.extract(net_ready, img, feature_test, feature_blob);
std::cout << std::endl << "---------- Featrue of target image: " << target_img << "----------" << endl << feature_test << std::endl; std::cout << std::endl << "---------- Featrue of target image: " << target_img << "----------" << endl << feature_test << std::endl;
prediction = descriptor.Classify(feature_reference, feature_test, num_candidate); prediction = classify(feature_reference, feature_test, num_candidate, descriptor.labels_);
// Print the top N prediction. // Print the top N prediction.
std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl; std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl;
for (size_t i = 0; i < prediction.size(); ++i) { for (size_t i = 0; i < prediction.size(); ++i) {
std::pair<string, float> p = prediction[i]; std::pair<string, float> p = prediction[i];
std::cout << std::fixed << std::setprecision(2) << p.second << " - \"" std::cout << std::fixed << std::setprecision(2) << p.second << " - \"" << p.first << "\"" << std::endl;
<< p.first << "\"" << std::endl;
} }
return 0; return 0;
} }

@ -40,14 +40,15 @@
using namespace cv; using namespace cv;
using namespace std; using namespace std;
using namespace cv::cnn_3dobj; using namespace cv::cnn_3dobj;
int main(int argc, char *argv[]){ int main(int argc, char *argv[])
{
const String keys = "{help | | demo :$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/ape.ply -imagedir=../data/images_ape/ -labeldir=../data/label_ape.txt -num_class=4 -label_class=0, then press 'q' to run the demo for images generation when you see the gray background and a coordinate.}" const String keys = "{help | | demo :$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/ape.ply -imagedir=../data/images_ape/ -labeldir=../data/label_ape.txt -num_class=4 -label_class=0, then press 'q' to run the demo for images generation when you see the gray background and a coordinate.}"
"{ite_depth | 2 | Iteration of sphere generation.}" "{ite_depth | 2 | Iteration of sphere generation.}"
"{plymodel | ../3Dmodel/ape.ply | path of the '.ply' file for image rendering. }" "{plymodel | ../3Dmodel/ape.ply | path of the '.ply' file for image rendering. }"
"{imagedir | ../data/images_all/ | path of the generated images for one particular .ply model. }" "{imagedir | ../data/images_all/ | path of the generated images for one particular .ply model. }"
"{labeldir | ../data/label_all.txt | path of the generated images for one particular .ply model. }" "{labeldir | ../data/label_all.txt | path of the generated images for one particular .ply model. }"
"{num_class | 4 | total number of classes of models}" "{num_class | 4 | total number of classes of models}"
"{label_class | 0 | class label of current .ply model}"; "{label_class | 0 | class label of current .ply model}";
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
parser.about("Demo for Sphere View data generation"); parser.about("Demo for Sphere View data generation");
if (parser.has("help")) if (parser.has("help"))
@ -61,7 +62,7 @@ int main(int argc, char *argv[]){
string labeldir = parser.get<string>("labeldir"); string labeldir = parser.get<string>("labeldir");
int num_class = parser.get<int>("num_class"); int num_class = parser.get<int>("num_class");
int label_class = parser.get<int>("label_class"); int label_class = parser.get<int>("label_class");
cv::cnn_3dobj::IcoSphere ViewSphere(10,ite_depth); cv::cnn_3dobj::icoSphere ViewSphere(10,ite_depth);
std::vector<cv::Point3d> campos = ViewSphere.CameraPos; std::vector<cv::Point3d> campos = ViewSphere.CameraPos;
std::fstream imglabel; std::fstream imglabel;
char* p=(char*)labeldir.data(); char* p=(char*)labeldir.data();

@ -6,8 +6,8 @@ namespace cv
{ {
namespace cnn_3dobj namespace cnn_3dobj
{ {
DescriptorExtractor::DescriptorExtractor(){}; descriptorExtractor::descriptorExtractor(){};
void DescriptorExtractor::list_dir(const char *path,vector<string>& files,bool r) void descriptorExtractor::listDir(const char *path,vector<string>& files,bool r)
{ {
DIR *pDir; DIR *pDir;
struct dirent *ent; struct dirent *ent;
@ -25,7 +25,7 @@ namespace cnn_3dobj
if(r) if(r)
{ {
sprintf(childpath, "%s/%s", path, ent->d_name); sprintf(childpath, "%s/%s", path, ent->d_name);
DescriptorExtractor::list_dir(childpath,files,false); descriptorExtractor::listDir(childpath,files,false);
} }
} }
else else
@ -36,7 +36,7 @@ namespace cnn_3dobj
sort(files.begin(),files.end()); sort(files.begin(),files.end());
}; };
bool DescriptorExtractor::SetNet(const string& cpu_only, int device_id) bool descriptorExtractor::setNet(const string& cpu_only, int device_id)
{ {
if (strcmp(cpu_only.c_str(), "CPU") == 0 || strcmp(cpu_only.c_str(), "GPU") == 0) if (strcmp(cpu_only.c_str(), "CPU") == 0 || strcmp(cpu_only.c_str(), "GPU") == 0)
{ {
@ -59,9 +59,9 @@ namespace cnn_3dobj
} }
}; };
bool DescriptorExtractor::LoadNet(bool netsetter, const string& model_file, const string& trained_file, const string& mean_file) int descriptorExtractor::loadNet(bool netsetter, const string& model_file, const string& trained_file, const string& mean_file)
{ {
bool net_ready = false; int net_ready = 0;
if (netsetter) if (netsetter)
{ {
/* Load the network. */ /* Load the network. */
@ -77,58 +77,50 @@ namespace cnn_3dobj
std::cout << "Input layer should have 1 or 3 channels." << std::endl; std::cout << "Input layer should have 1 or 3 channels." << std::endl;
input_geometry_ = cv::Size(input_layer->width(), input_layer->height()); input_geometry_ = cv::Size(input_layer->width(), input_layer->height());
/* Load the binaryproto mean file. */ /* Load the binaryproto mean file. */
SetMean(mean_file); setMean(mean_file);
net_ready = true; net_ready = 2;
return net_ready;
} }
else else
{ {
std::cout << "Error: Device must be set in advance using SetNet function" << std::endl; std::cout << "Error: Device must be set in advance using SetNet function" << std::endl;
return net_ready;
} }
return net_ready;
}; };
void DescriptorExtractor::GetLabellist(const std::vector<string>& name_gallery) int descriptorExtractor::loadNet(bool netsetter, const string& model_file, const string& trained_file)
{
for (unsigned int i = 0; i < name_gallery.size(); ++i)
labels_.push_back(name_gallery[i]);
};
/* Return the indices of the top N values of vector v. */
std::vector<int> DescriptorExtractor::Argmax(const std::vector<float>& v, int N)
{
std::vector<std::pair<float, int> > pairs;
for (size_t i = 0; i < v.size(); ++i)
pairs.push_back(std::make_pair(v[i], i));
std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end());
std::vector<int> result;
for (int i = 0; i < N; ++i)
result.push_back(pairs[i].second);
return result;
};
std::vector<std::pair<string, float> > DescriptorExtractor::Classify(const cv::Mat& reference, const cv::Mat& target, int N)
{ {
std::vector<float> output; int net_ready = 0;
for (int i = 0; i < reference.rows; i++) if (netsetter)
{ {
cv::Mat f1 = reference.row(i); /* Load the network. */
cv::Mat f2 = target; net_ = new Net<float>(model_file, TEST);
cv::Mat output_temp = f1-f2; net_->CopyTrainedLayersFrom(trained_file);
output.push_back(cv::norm(output_temp)); if (net_->num_inputs() != 1)
std::cout << "Network should have exactly one input." << std::endl;
if (net_->num_outputs() != 1)
std::cout << "Network should have exactly one output." << std::endl;
Blob<float>* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
if (num_channels_ != 3 && num_channels_ != 1)
std::cout << "Input layer should have 1 or 3 channels." << std::endl;
input_geometry_ = cv::Size(input_layer->width(), input_layer->height());
net_ready = 1;
} }
std::vector<int> maxN = Argmax(output, N); else
std::vector<std::pair<string, float> > predictions;
for (int i = 0; i < N; ++i)
{ {
int idx = maxN[i]; std::cout << "Error: Device must be set in advance using SetNet function" << std::endl;
predictions.push_back(std::make_pair(labels_[idx], output[idx]));
} }
return predictions; return net_ready;
};
void descriptorExtractor::getLabellist(const std::vector<string>& name_gallery)
{
for (unsigned int i = 0; i < name_gallery.size(); ++i)
labels_.push_back(name_gallery[i]);
}; };
/* Load the mean file in binaryproto format. */ /* Load the mean file in binaryproto format. */
void DescriptorExtractor::SetMean(const string& mean_file) void descriptorExtractor::setMean(const string& mean_file)
{ {
BlobProto blob_proto; BlobProto blob_proto;
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto); ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
@ -156,7 +148,7 @@ namespace cnn_3dobj
mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean); mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}; };
void DescriptorExtractor::Extract(bool net_ready, InputArray inputimg, OutputArray feature, bool mean_subtract, std::string featrue_blob) void descriptorExtractor::extract(int net_ready, InputArray inputimg, OutputArray feature, std::string featrue_blob)
{ {
if (net_ready) if (net_ready)
{ {
@ -166,11 +158,11 @@ namespace cnn_3dobj
/* Forward dimension change to all layers. */ /* Forward dimension change to all layers. */
net_->Reshape(); net_->Reshape();
std::vector<cv::Mat> input_channels; std::vector<cv::Mat> input_channels;
WrapInputLayer(&input_channels); wrapInputLayer(&input_channels);
if (inputimg.kind() == 65536) if (inputimg.kind() == 65536)
{/* this is a Mat */ {/* this is a Mat */
Mat img = inputimg.getMat(); Mat img = inputimg.getMat();
Preprocess(img, &input_channels, mean_subtract); preprocess(img, &input_channels, net_ready);
net_->ForwardPrefilled(); net_->ForwardPrefilled();
/* Copy the output layer to a std::vector */ /* Copy the output layer to a std::vector */
Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get(); Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get();
@ -187,7 +179,7 @@ namespace cnn_3dobj
Mat feature_vector; Mat feature_vector;
for (unsigned int i = 0; i < img.size(); ++i) for (unsigned int i = 0; i < img.size(); ++i)
{ {
Preprocess(img[i], &input_channels, mean_subtract); preprocess(img[i], &input_channels, net_ready);
net_->ForwardPrefilled(); net_->ForwardPrefilled();
/* Copy the output layer to a std::vector */ /* Copy the output layer to a std::vector */
Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get(); Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get();
@ -214,7 +206,7 @@ namespace cnn_3dobj
* don't need to rely on cudaMemcpy2D. The last preprocessing * don't need to rely on cudaMemcpy2D. The last preprocessing
* operation will write the separate channels directly to the input * operation will write the separate channels directly to the input
* layer. */ * layer. */
void DescriptorExtractor::WrapInputLayer(std::vector<cv::Mat>* input_channels) void descriptorExtractor::wrapInputLayer(std::vector<cv::Mat>* input_channels)
{ {
Blob<float>* input_layer = net_->input_blobs()[0]; Blob<float>* input_layer = net_->input_blobs()[0];
int width = input_layer->width(); int width = input_layer->width();
@ -228,8 +220,8 @@ namespace cnn_3dobj
} }
}; };
void DescriptorExtractor::Preprocess(const cv::Mat& img, void descriptorExtractor::preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels, bool mean_subtract) std::vector<cv::Mat>* input_channels, int net_ready)
{ {
/* Convert the input image to the input image format of the network. */ /* Convert the input image to the input image format of the network. */
cv::Mat sample; cv::Mat sample;
@ -254,7 +246,7 @@ std::vector<cv::Mat>* input_channels, bool mean_subtract)
else else
sample_resized.convertTo(sample_float, CV_32FC1); sample_resized.convertTo(sample_float, CV_32FC1);
cv::Mat sample_normalized; cv::Mat sample_normalized;
if (mean_subtract) if (net_ready == 2)
cv::subtract(sample_float, mean_, sample_normalized); cv::subtract(sample_float, mean_, sample_normalized);
else else
sample_normalized = sample_float; sample_normalized = sample_float;

@ -6,7 +6,7 @@ namespace cv
{ {
namespace cnn_3dobj namespace cnn_3dobj
{ {
IcoSphere::IcoSphere(float radius_in, int depth_in) icoSphere::icoSphere(float radius_in, int depth_in)
{ {
X = 0.5f; X = 0.5f;
@ -35,7 +35,7 @@ namespace cnn_3dobj
{ {
for (int k = 0; k<j; k++) for (int k = 0; k<j; k++)
{ {
if (CameraPos.at(k).x-CameraPos.at(j).x<diff && CameraPos.at(k).y-CameraPos.at(j).y<diff && CameraPos.at(k).z-CameraPos.at(j).z<diff) if (CameraPos.at(k).x-CameraPos.at(j).x < diff && CameraPos.at(k).y-CameraPos.at(j).y < diff && CameraPos.at(k).z-CameraPos.at(j).z < diff)
break; break;
if(k == j-1) if(k == j-1)
CameraPos_temp.push_back(CameraPos[j]); CameraPos_temp.push_back(CameraPos[j]);
@ -49,7 +49,7 @@ namespace cnn_3dobj
cout << CameraPos.at(i).x <<' '<< CameraPos.at(i).y << ' ' << CameraPos.at(i).z << endl; cout << CameraPos.at(i).x <<' '<< CameraPos.at(i).y << ' ' << CameraPos.at(i).z << endl;
} }
}; };
void IcoSphere::norm(float v[]) void icoSphere::norm(float v[])
{ {
float len = 0; float len = 0;
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
@ -63,7 +63,7 @@ namespace cnn_3dobj
} }
}; };
void IcoSphere::add(float v[]) void icoSphere::add(float v[])
{ {
Point3f temp_Campos; Point3f temp_Campos;
std::vector<float>* temp = new std::vector<float>; std::vector<float>* temp = new std::vector<float>;
@ -77,7 +77,7 @@ namespace cnn_3dobj
CameraPos.push_back(temp_Campos); CameraPos.push_back(temp_Campos);
}; };
void IcoSphere::subdivide(float v1[], float v2[], float v3[], int depth) void icoSphere::subdivide(float v1[], float v2[], float v3[], int depth)
{ {
norm(v1); norm(v1);
norm(v2); norm(v2);
@ -107,13 +107,13 @@ namespace cnn_3dobj
subdivide(v12, v23, v31, depth - 1); subdivide(v12, v23, v31, depth - 1);
}; };
uint32_t IcoSphere::swap_endian(uint32_t val) uint32_t icoSphere::swapEndian(uint32_t val)
{ {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16); return (val << 16) | (val >> 16);
}; };
cv::Point3d IcoSphere::getCenter(cv::Mat cloud) cv::Point3d icoSphere::getCenter(cv::Mat cloud)
{ {
Point3f* data = cloud.ptr<cv::Point3f>(); Point3f* data = cloud.ptr<cv::Point3f>();
Point3d dataout; Point3d dataout;
@ -129,7 +129,7 @@ namespace cnn_3dobj
return dataout; return dataout;
}; };
float IcoSphere::getRadius(cv::Mat cloud, cv::Point3d center) float icoSphere::getRadius(cv::Mat cloud, cv::Point3d center)
{ {
float radiusCam = 0; float radiusCam = 0;
Point3f* data = cloud.ptr<cv::Point3f>(); Point3f* data = cloud.ptr<cv::Point3f>();
@ -149,7 +149,7 @@ namespace cnn_3dobj
return radiusCam; return radiusCam;
}; };
void IcoSphere::createHeader(int num_item, int rows, int cols, const char* headerPath) void icoSphere::createHeader(int num_item, int rows, int cols, const char* headerPath)
{ {
char* a0 = (char*)malloc(1024); char* a0 = (char*)malloc(1024);
strcpy(a0, headerPath); strcpy(a0, headerPath);
@ -165,17 +165,17 @@ namespace cnn_3dobj
std::ofstream headerLabel(headerPathlab, ios::out|ios::binary); std::ofstream headerLabel(headerPathlab, ios::out|ios::binary);
int headerimg[4] = {2051,num_item,rows,cols}; int headerimg[4] = {2051,num_item,rows,cols};
for (int i=0; i<4; i++) for (int i=0; i<4; i++)
headerimg[i] = swap_endian(headerimg[i]); headerimg[i] = swapEndian(headerimg[i]);
int headerlabel[2] = {2050,num_item}; int headerlabel[2] = {2050,num_item};
for (int i=0; i<2; i++) for (int i=0; i<2; i++)
headerlabel[i] = swap_endian(headerlabel[i]); headerlabel[i] = swapEndian(headerlabel[i]);
headerImg.write(reinterpret_cast<const char*>(headerimg), sizeof(int)*4); headerImg.write(reinterpret_cast<const char*>(headerimg), sizeof(int)*4);
headerImg.close(); headerImg.close();
headerLabel.write(reinterpret_cast<const char*>(headerlabel), sizeof(int)*2); headerLabel.write(reinterpret_cast<const char*>(headerlabel), sizeof(int)*2);
headerLabel.close(); headerLabel.close();
}; };
void IcoSphere::writeBinaryfile(string filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z) void icoSphere::writeBinaryfile(string filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z)
{ {
int isrgb = 0; int isrgb = 0;
cv::Mat ImgforBin = cv::imread(filenameImg, isrgb); cv::Mat ImgforBin = cv::imread(filenameImg, isrgb);

Loading…
Cancel
Save