Skip to content

Commit

Permalink
add scaleFactor/setScaleFactor/getScaleFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrPanov committed Jul 28, 2022
1 parent 9d0a451 commit ee9ef17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 17 additions & 2 deletions modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,23 @@ class CV_EXPORTS_W WeChatQRCode {
* empty if not found.
* @return list of decoded string.
*/
CV_WRAP std::vector<std::string> detectAndDecode(InputArray img,
OutputArrayOfArrays points = noArray());
CV_WRAP std::vector<std::string> 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;
Expand Down
20 changes: 16 additions & 4 deletions modules/wechat_qrcode/src/wechat_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class WeChatQRCode::Impl {
std::shared_ptr<SSDDetector> detector_;
std::shared_ptr<SuperScale> super_resolution_model_;
bool use_nn_detector_, use_nn_sr_;
float scaleFactor = -1.f;
};

WeChatQRCode::WeChatQRCode(const String& detector_prototxt_path,
Expand Down Expand Up @@ -109,6 +110,17 @@ vector<string> 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<string> WeChatQRCode::Impl::decode(const Mat& img, vector<Mat>& candidate_points,
Expand Down Expand Up @@ -173,11 +185,11 @@ int WeChatQRCode::Impl::applyDetector(const Mat& img, vector<Mat>& 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);

Expand Down

0 comments on commit ee9ef17

Please sign in to comment.