Skip to content

Commit 10d0758

Browse files
committed
alter cpp NormalizeImage
1 parent d322909 commit 10d0758

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

deploy/cpp/include/preprocess_op.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class NormalizeImage : public PreprocessOp {
6565
virtual void Init(const YAML::Node& item) {
6666
mean_ = item["mean"].as<std::vector<float>>();
6767
scale_ = item["std"].as<std::vector<float>>();
68-
is_scale_ = item["is_scale"].as<bool>();
68+
if (item["is_scale"]) is_scale_ = item["is_scale"].as<bool>();
69+
if (item["norm_type"]) norm_type_ = item["norm_type"].as<std::string>();
6970
}
7071

7172
virtual void Run(cv::Mat* im, ImageBlob* data);
@@ -75,6 +76,7 @@ class NormalizeImage : public PreprocessOp {
7576
std::vector<float> mean_;
7677
std::vector<float> scale_;
7778
bool is_scale_ = true;
79+
std::string norm_type_ = "mean_std";
7880
};
7981

8082
class Permute : public PreprocessOp {

deploy/cpp/src/preprocess_op.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ void NormalizeImage::Run(cv::Mat* im, ImageBlob* data) {
3434
e /= 255.0;
3535
}
3636
(*im).convertTo(*im, CV_32FC3, e);
37-
for (int h = 0; h < im->rows; h++) {
38-
for (int w = 0; w < im->cols; w++) {
39-
im->at<cv::Vec3f>(h, w)[0] =
40-
(im->at<cv::Vec3f>(h, w)[0] - mean_[0]) / scale_[0];
41-
im->at<cv::Vec3f>(h, w)[1] =
42-
(im->at<cv::Vec3f>(h, w)[1] - mean_[1]) / scale_[1];
43-
im->at<cv::Vec3f>(h, w)[2] =
44-
(im->at<cv::Vec3f>(h, w)[2] - mean_[2]) / scale_[2];
37+
if (norm_type_ == "mean_std"){
38+
for (int h = 0; h < im->rows; h++) {
39+
for (int w = 0; w < im->cols; w++) {
40+
im->at<cv::Vec3f>(h, w)[0] =
41+
(im->at<cv::Vec3f>(h, w)[0] - mean_[0]) / scale_[0];
42+
im->at<cv::Vec3f>(h, w)[1] =
43+
(im->at<cv::Vec3f>(h, w)[1] - mean_[1]) / scale_[1];
44+
im->at<cv::Vec3f>(h, w)[2] =
45+
(im->at<cv::Vec3f>(h, w)[2] - mean_[2]) / scale_[2];
46+
}
4547
}
4648
}
4749
}

0 commit comments

Comments
 (0)