io
authorYangqing Jia <jiayq84@gmail.com>
Fri, 27 Sep 2013 17:54:29 +0000 (10:54 -0700)
committerYangqing Jia <jiayq84@gmail.com>
Fri, 27 Sep 2013 17:54:29 +0000 (10:54 -0700)
src/Makefile
src/caffe/util/io.cpp [new file with mode: 0644]
src/caffe/util/io.hpp [new file with mode: 0644]

index 315eaa4..f86db8f 100644 (file)
@@ -35,7 +35,8 @@ MKL_LIB_DIR := $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
 
 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
new file mode 100644 (file)
index 0000000..929fdd6
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..c3917c3
--- /dev/null
@@ -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