summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 97704a7)
raw | patch | inline | side by side (parent: 97704a7)
author | Yangqing Jia <jiayq84@gmail.com> | |
Fri, 27 Sep 2013 17:54:29 +0000 (10:54 -0700) | ||
committer | Yangqing Jia <jiayq84@gmail.com> | |
Fri, 27 Sep 2013 17:54:29 +0000 (10:54 -0700) |
src/Makefile | patch | blob | history | |
src/caffe/util/io.cpp | [new file with mode: 0644] | patch | blob |
src/caffe/util/io.hpp | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile b/src/Makefile
index 315eaa4dcca42b62f1d74a681c9309b2318dc734..f86db8f28b32a43f23f740f6e27f08ab83123c09 100644 (file)
--- a/src/Makefile
+++ b/src/Makefile
INCLUDE_DIRS := . /usr/local/include $(CUDA_INCLUDE_DIR) $(MKL_INCLUDE_DIR)
LIBRARY_DIRS := . /usr/local/lib $(CUDA_LIB_DIR) $(MKL_LIB_DIR)
-LIBRARIES := cuda cudart cublas protobuf glog mkl_rt mkl_intel_thread curand leveldb snappy
+LIBRARIES := cuda cudart cublas protobuf glog mkl_rt mkl_intel_thread curand \
+ leveldb snappy opencv_core opencv_highgui
WARNINGS := -Wall
CXXFLAGS += -fPIC $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp
--- /dev/null
+++ b/src/caffe/util/io.cpp
@@ -0,0 +1,56 @@
+#include <stdint.h>
+#include <string>
+#include <opencv2/core/core.hpp>
+#include <opencv2/highgui/highgui.hpp>
+
+#include "caffe/common.hpp"
+#include "caffe/util/io.hpp"
+#include "caffe/proto/caffe.pb.h"
+
+using cv::Mat;
+using cv::Vec3b;
+using std::string;
+
+namespace caffe {
+
+void ReadImageToProto(const string& filename, BlobProto* proto) {
+ Mat cv_image;
+ cv_image = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
+ CHECK(cv_image.data) << "Could not open or find the image.";
+ DCHECK_EQ(cv_image.channels(), 3);
+ proto->set_num(1);
+ proto->set_channels(3);
+ proto->set_height(cv_image.rows);
+ proto->set_width(cv_image.cols);
+ proto->clear_data();
+ proto->clear_diff();
+ for (int c = 0; c < 3; ++c) {
+ for (int h = 0; h < cv_image.rows; ++h) {
+ for (int w = 0; w < cv_image.cols; ++w) {
+ proto->add_data(float(cv_image.at<Vec3b>(h, w)[c]) / 255.);
+ }
+ }
+ }
+}
+
+void WriteProtoToImage(const string& filename, const BlobProto& proto) {
+ CHECK_EQ(proto.num(), 1);
+ CHECK_EQ(proto.channels(), 3);
+ CHECK_GT(proto.height(), 0);
+ CHECK_GT(proto.width(), 0);
+ Mat cv_image(proto.height(), proto.width(), CV_8UC3);
+ // TODO: copy the blob data to image.
+ for (int c = 0; c < 3; ++c) {
+ for (int h = 0; h < cv_image.rows; ++h) {
+ for (int w = 0; w < cv_image.cols; ++w) {
+ cv_image.at<Vec3b>(h, w)[c] =
+ uint8_t(proto.data((c * cv_image.rows + h) * cv_image.cols + w)
+ * 255.);
+ }
+ }
+ }
+ CHECK(cv::imwrite(filename, cv_image));
+}
+
+
+} // namespace caffe
diff --git a/src/caffe/util/io.hpp b/src/caffe/util/io.hpp
--- /dev/null
+++ b/src/caffe/util/io.hpp
@@ -0,0 +1,17 @@
+#ifndef CAFFE_UTIL_IO_H_
+#define CAFFE_UTIL_IO_H_
+
+#include <string>
+#include "caffe/proto/caffe.pb.h"
+
+using std::string;
+
+namespace caffe {
+
+void ReadImageToProto(const string& filename, BlobProto* proto);
+void WriteProtoToImage(const string& filename, const BlobProto& proto);
+
+
+} // namespace caffe
+
+#endif // CAFFE_UTIL_IO_H_
\ No newline at end of file