Skip to content

Commit

Permalink
add getScaleFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrPanov committed Jul 28, 2022
1 parent 7ada289 commit 7f8d4c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
15 changes: 13 additions & 2 deletions modules/wechat_qrcode/include/opencv2/wechat_qrcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,21 @@ class CV_EXPORTS_W WeChatQRCode {
CV_WRAP std::vector<std::string> detectAndDecode(InputArray img, OutputArrayOfArrays points = noArray());

/**
* @brief set scaling factor
*/
* @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;
Ptr<Impl> p;
Expand Down
18 changes: 12 additions & 6 deletions modules/wechat_qrcode/src/wechat_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,16 @@ vector<string> WeChatQRCode::detectAndDecode(InputArray img, OutputArrayOfArrays
return ret;
}

void WeChatQRCode::setScaleFactor(float _scaleFactor) {
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,
vector<Mat>& points) {
Expand Down Expand Up @@ -178,10 +185,9 @@ int WeChatQRCode::Impl::applyDetector(const Mat& img, vector<Mat>& points) {

const float targetArea = 400.f * 400.f;
// hard code input size
if (scaleFactor <= 0 || scaleFactor > 1)
scaleFactor = sqrt(targetArea/(img_w*img_h));
int detect_width = img_w * scaleFactor;
int detect_height = img_h * scaleFactor;
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
14 changes: 4 additions & 10 deletions modules/wechat_qrcode/test/test_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,10 @@ INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Curved, testing::ValuesIn(qrcode_

TEST(Objdetect_QRCode_Big, regression) {
string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
try {
path_detect_prototxt = findDataFile("../dnn/wechat/detect.prototxt");
path_detect_caffemodel = findDataFile("../dnn/wechat/detect.caffemodel");
path_sr_prototxt = findDataFile("../dnn/wechat/sr.prototxt");
path_sr_caffemodel = findDataFile("../dnn/wechat/sr.caffemodel");
}
catch (...) {
// unsuccessful model reading
return;
}
path_detect_prototxt = findDataFile("../dnn/wechat/detect.prototxt");
path_detect_caffemodel = findDataFile("../dnn/wechat/detect.caffemodel");
path_sr_prototxt = findDataFile("../dnn/wechat/sr.prototxt");
path_sr_caffemodel = findDataFile("../dnn/wechat/sr.caffemodel");

auto detector = wechat_qrcode::WeChatQRCode(path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt,
path_sr_caffemodel);
Expand Down

0 comments on commit 7f8d4c1

Please sign in to comment.