Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Support float and integer input types with OpenCV #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/caffe/data_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
CHECK_LE(width, img_width);
CHECK_GE(num, 1);

CHECK(cv_img.depth() == CV_8U) << "Image data type must be unsigned byte";
CHECK(cv_img.depth() == CV_8U || cv_img.depth() == CV_32F ||
cv_img.depth() == CV_32S) <<
"Image data type must be unsigned byte, float or integer";

const Dtype scale = param_.scale();

Expand Down Expand Up @@ -422,7 +424,9 @@ void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
Dtype* transformed_data = transformed_blob->mutable_cpu_data();
int top_index;
for (int h = 0; h < height; ++h) {
const uchar* ptr = cv_cropped_img.ptr<uchar>(h);
const uchar* ptrUchar = cv_cropped_img.ptr<uchar>(h);
const float* ptrFloat = cv_cropped_img.ptr<float>(h);
const int32_t* ptrInt = cv_cropped_img.ptr<int32_t>(h);
int img_index = 0;
for (int w = 0; w < width; ++w) {
for (int c = 0; c < img_channels; ++c) {
Expand All @@ -432,7 +436,14 @@ void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
top_index = (c * height + h) * width + w;
}
// int top_index = (c * height + h) * width + w;
Dtype pixel = static_cast<Dtype>(ptr[img_index++]);
Dtype pixel = 0;

if (cv_img.depth() == CV_8U)
pixel = static_cast<Dtype>(ptrUchar[img_index++]);
if (cv_img.depth() == CV_32F)
pixel = static_cast<Dtype>(ptrFloat[img_index++]);
if (cv_img.depth() == CV_32S)
pixel = static_cast<Dtype>(ptrInt[img_index++]);
if (has_mean_file) {
int mean_index = (c * img_height + h_off + h) * img_width + w_off + w;
transformed_data[top_index] =
Expand Down