diff --git a/modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp b/modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp index 7400d2b0889..676104cd022 100644 --- a/modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp +++ b/modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp @@ -48,8 +48,23 @@ class CV_EXPORTS_W WeChatQRCode { * empty if not found. * @return list of decoded string. */ - CV_WRAP std::vector detectAndDecode(InputArray img, - OutputArrayOfArrays points = noArray()); + CV_WRAP std::vector detectAndDecode(InputArray img, OutputArrayOfArrays points = noArray()); + + /** + * @brief set scale factor + * QR code detector use neural network to detect QR. + * Before running the neural network, the input image is pre-processed by scaling. + * By default, the input image is scaled to an image with an area of 160000 pixels. + * The scale factor allows to use custom scale the input image: + * width = scaleFactor*width + * height = scaleFactor*width + * + * scaleFactor valuse must be > 0 and <= 1, otherwise the scaleFactor value is set to -1 + * and use default scaled to an image with an area of 160000 pixels. + */ + CV_WRAP void setScaleFactor(float _scalingFactor); + + CV_WRAP float getScaleFactor(); protected: class Impl; diff --git a/modules/wechat_qrcode/src/wechat_qrcode.cpp b/modules/wechat_qrcode/src/wechat_qrcode.cpp index 7a4037c9c61..df5e8bbcc93 100644 --- a/modules/wechat_qrcode/src/wechat_qrcode.cpp +++ b/modules/wechat_qrcode/src/wechat_qrcode.cpp @@ -43,6 +43,7 @@ class WeChatQRCode::Impl { std::shared_ptr detector_; std::shared_ptr super_resolution_model_; bool use_nn_detector_, use_nn_sr_; + float scaleFactor = -1.f; }; WeChatQRCode::WeChatQRCode(const String& detector_prototxt_path, @@ -109,6 +110,17 @@ vector WeChatQRCode::detectAndDecode(InputArray img, OutputArrayOfArrays points.assign(tmp_points); } return ret; +} + +void WeChatQRCode::setScaleFactor(float _scaleFactor) { + if (_scaleFactor > 0 && _scaleFactor <= 1.f) + p->scaleFactor = _scaleFactor; + else + p->scaleFactor = -1.f; +}; + +float WeChatQRCode::getScaleFactor() { + return p->scaleFactor; }; vector WeChatQRCode::Impl::decode(const Mat& img, vector& candidate_points, @@ -173,11 +185,11 @@ int WeChatQRCode::Impl::applyDetector(const Mat& img, vector& points) { int img_w = img.cols; int img_h = img.rows; + const float targetArea = 400.f * 400.f; // hard code input size - int minInputSize = 400; - float resizeRatio = sqrt(img_w * img_h * 1.0 / (minInputSize * minInputSize)); - int detect_width = img_w / resizeRatio; - int detect_height = img_h / resizeRatio; + const float tmpScaleFactor = scaleFactor == -1.f ? sqrt(targetArea / (img_w * img_h)) : scaleFactor; + int detect_width = img_w * tmpScaleFactor; + int detect_height = img_h * tmpScaleFactor; points = detector_->forward(img, detect_width, detect_height);