diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index 7b6c67cf633..ad835e35d66 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -71,16 +71,18 @@ bool ReadImageToDatum(const string& filename, const int label, cv::Mat cv_img; int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE); - if (height > 0 && width > 0) { - cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag); - cv::resize(cv_img_origin, cv_img, cv::Size(width, height)); - } else { - cv_img = cv::imread(filename, cv_read_flag); - } - if (!cv_img.data) { + + cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag); + if (!cv_img_origin.data) { LOG(ERROR) << "Could not open or find file " << filename; return false; } + if (height > 0 && width > 0) { + cv::resize(cv_img_origin, cv_img, cv::Size(height, width)); + } else { + cv_img = cv_img_origin; + } + int num_channels = (is_color ? 3 : 1); datum->set_channels(num_channels); datum->set_height(cv_img.rows);