From 7bcea8d0db52efbb972904149b2d9e0e2a22cf9e Mon Sep 17 00:00:00 2001 From: littletomatodonkey <2120160898@bit.edu.cn> Date: Tue, 22 Jun 2021 13:12:01 +0800 Subject: [PATCH] fix normalize (#3147) --- deploy/cpp_infer/src/preprocess_op.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/deploy/cpp_infer/src/preprocess_op.cpp b/deploy/cpp_infer/src/preprocess_op.cpp index 28590e18..23c51c20 100644 --- a/deploy/cpp_infer/src/preprocess_op.cpp +++ b/deploy/cpp_infer/src/preprocess_op.cpp @@ -47,16 +47,13 @@ void Normalize::Run(cv::Mat *im, const std::vector &mean, e /= 255.0; } (*im).convertTo(*im, CV_32FC3, e); - for (int h = 0; h < im->rows; h++) { - for (int w = 0; w < im->cols; w++) { - im->at(h, w)[0] = - (im->at(h, w)[0] - mean[0]) * scale[0]; - im->at(h, w)[1] = - (im->at(h, w)[1] - mean[1]) * scale[1]; - im->at(h, w)[2] = - (im->at(h, w)[2] - mean[2]) * scale[2]; - } + std::vector bgr_channels(3); + cv::split(*im, bgr_channels); + for (auto i = 0; i < bgr_channels.size(); i++) { + bgr_channels[i].convertTo(bgr_channels[i], CV_32FC1, 1.0 * scale[i], + (0.0 - mean[i]) * scale[i]); } + cv::merge(bgr_channels, *im); } void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img,