| 117 | } |
| 118 | |
| 119 | bool ReadImageToDatum(const string& filename, const int label, |
| 120 | const int height, const int width, const bool is_color, |
| 121 | const std::string & encoding, Datum* datum) { |
| 122 | cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color); |
| 123 | if (cv_img.data) { |
| 124 | if (encoding.size()) { |
| 125 | if ( (cv_img.channels() == 3) == is_color && !height && !width && |
| 126 | matchExt(filename, encoding) ) |
| 127 | return ReadFileToDatum(filename, label, datum); |
| 128 | std::vector<uchar> buf; |
| 129 | cv::imencode("."+encoding, cv_img, buf); |
| 130 | datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]), |
| 131 | buf.size())); |
| 132 | datum->set_label(label); |
| 133 | datum->set_encoded(true); |
| 134 | return true; |
| 135 | } |
| 136 | CVMatToDatum(cv_img, datum); |
| 137 | datum->set_label(label); |
| 138 | return true; |
| 139 | } else { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | #endif // USE_OPENCV |
| 144 | |
| 145 | bool ReadFileToDatum(const string& filename, const int label, |