diff --git a/src/io/iter_image_recordio-inl.hpp b/src/io/iter_image_recordio-inl.hpp old mode 100644 new mode 100755 index 50f5c287..6b87e9ae --- a/src/io/iter_image_recordio-inl.hpp +++ b/src/io/iter_image_recordio-inl.hpp @@ -163,6 +163,8 @@ class ImageRecordIOParser { dmlc::InputSplit *source_; /*! \brief label information, if any */ ImageLabelMap *label_map_; + /*! \brief forces to load images as grey */ + int as_grey_; }; inline void ImageRecordIOParser::Init(void) { @@ -210,6 +212,9 @@ SetParam(const char *name, const char *val) { if (!strcmp(name, "label_width")) { label_width_ = atoi(val); } + if (!strcmp(name, "as_grey")) { + as_grey_ = atoi(val); + } } inline bool ImageRecordIOParser:: @@ -233,18 +238,36 @@ ParseNext(std::vector *out_vec) { cv::Mat res; rec.Load(blob.dptr, blob.size); cv::Mat buf(1, rec.content_size, CV_8U, rec.content); - res = cv::imdecode(buf, 1); + if (as_grey_ == 1) { + res = cv::imdecode(buf, CV_LOAD_IMAGE_GRAYSCALE); + } else { + res = cv::imdecode(buf, 1); + } res = augmenters_[tid]->Process(res, prnds_[tid]); - out.Push(static_cast(rec.image_index()), - mshadow::Shape3(3, res.rows, res.cols), - mshadow::Shape1(label_width_)); + if (as_grey_ == 1) { + out.Push(static_cast(rec.image_index()), + mshadow::Shape3(1, res.rows, res.cols), + mshadow::Shape1(label_width_)); + } else { + out.Push(static_cast(rec.image_index()), + mshadow::Shape3(3, res.rows, res.cols), + mshadow::Shape1(label_width_)); + } DataInst inst = out.Back(); - for (int i = 0; i < res.rows; ++i) { - for (int j = 0; j < res.cols; ++j) { - cv::Vec3b bgr = res.at(i, j); - inst.data[0][i][j] = bgr[2]; - inst.data[1][i][j] = bgr[1]; - inst.data[2][i][j] = bgr[0]; + if (as_grey_ == 1) { + for (int i = 0; i < res.rows; ++i) { + for (int j = 0; j < res.cols; ++j) { + inst.data[0][i][j] = res.at(i, j); + } + } + } else { + for (int i = 0; i < res.rows; ++i) { + for (int j = 0; j < res.cols; ++j) { + cv::Vec3b bgr = res.at(i, j); + inst.data[0][i][j] = bgr[2]; + inst.data[1][i][j] = bgr[1]; + inst.data[2][i][j] = bgr[0]; + } } } if (label_map_ != NULL) { diff --git a/src/io/iter_img-inl.hpp b/src/io/iter_img-inl.hpp old mode 100644 new mode 100755 index ad403dc8..a80ce0c2 --- a/src/io/iter_img-inl.hpp +++ b/src/io/iter_img-inl.hpp @@ -25,6 +25,7 @@ class ImageIterator : public IIterator< DataInst >{ shuffle_ = 0; data_index_ = 0; label_width_ = 1; + as_grey_ = 0; } virtual ~ImageIterator(void) { if(fplst_ != NULL) fclose(fplst_); @@ -35,6 +36,8 @@ class ImageIterator : public IIterator< DataInst >{ if(!strcmp(name, "silent" )) silent_ = atoi(val); if(!strcmp(name, "shuffle" )) shuffle_ = atoi(val); if(!strcmp(name, "label_width" )) label_width_ = atoi(val); + if(!strcmp(name, "as_grey" )) as_grey_ = atoi(val); + } virtual void Init(void) { fplst_ = utils::FopenCheck(path_imglst_.c_str(), "r"); @@ -70,11 +73,11 @@ class ImageIterator : public IIterator< DataInst >{ if (data_index_ < static_cast(order_.size())) { size_t index = order_[data_index_]; if (path_imgdir_.length() == 0) { - LoadImage(img_, out_, filenames_[index].c_str()); + LoadImage(img_, out_, filenames_[index].c_str(), as_grey_); } else { char sname[256]; sprintf(sname, "%s%s", path_imgdir_.c_str(), filenames_[index].c_str()); - LoadImage(img_, out_, sname); + LoadImage(img_, out_, sname, as_grey_); } out_.index = index_list_[index]; mshadow::Tensor label_(&(labels_[0]) + label_width_ * index, @@ -92,16 +95,39 @@ class ImageIterator : public IIterator< DataInst >{ inline static void LoadImage(mshadow::TensorContainer &img, DataInst &out, const char *fname) { - cv::Mat res = cv::imread(fname); + LoadImage(img, out, fname, 0); + } + + inline static void LoadImage(mshadow::TensorContainer &img, + DataInst &out, + const char *fname, int as_grey_) { + cv::Mat res; + + if (as_grey_) { + res = cv::imread(fname, CV_LOAD_IMAGE_GRAYSCALE); + } else { + res = cv::imread(fname); + } CHECK(res.data != NULL) << "LoadImage: Reading image" << fname << "failed."; - img.Resize(mshadow::Shape3(3, res.rows, res.cols)); - for(index_t y = 0; y < img.size(1); ++y) { - for(index_t x = 0; x < img.size(2); ++x) { - cv::Vec3b bgr = res.at(y, x); - // store in RGB order - img[2][y][x] = bgr[0]; - img[1][y][x] = bgr[1]; - img[0][y][x] = bgr[2]; + + if (as_grey_) { + img.Resize(mshadow::Shape3(1, res.rows, res.cols)); + for(index_t y = 0; y < img.size(1); ++y) { + for(index_t x = 0; x < img.size(2); ++x) { + img[0][y][x] = res.at(y, x); + } + } + } + else { + img.Resize(mshadow::Shape3(3, res.rows, res.cols)); + for(index_t y = 0; y < img.size(1); ++y) { + for(index_t x = 0; x < img.size(2); ++x) { + cv::Vec3b bgr = res.at(y, x); + // store in RGB order + img[2][y][x] = bgr[0]; + img[1][y][x] = bgr[1]; + img[0][y][x] = bgr[2]; + } } } out.data = img; @@ -125,6 +151,8 @@ class ImageIterator : public IIterator< DataInst >{ int label_width_; // denotes the current data index int data_index_; + // forces to load images as grey scale + int as_grey_; // stores the reading orders std::vector order_; // stores the labels of data