diff --git a/alg/libqr.cpp b/alg/libqr.cpp index a268d12..16f1618 100644 --- a/alg/libqr.cpp +++ b/alg/libqr.cpp @@ -240,13 +240,38 @@ bool check_blur_by_laplacian(ProcessState &ps, Mat &gray, string &err) } static -bool check_blur(ProcessState &ps, Mat &gray, string &err) +bool check_blur_by_sobel(ProcessState &ps, Mat &gray, string &err) { - bool use_energy_gradient = false; - if (use_energy_gradient) { - return check_blur_by_energy_gradient(gray, err); + const float thres = 15; + Mat sobel_x, sobel_y; + Sobel(gray, sobel_x, CV_64F, 1, 0, 3); + Sobel(gray, sobel_y, CV_64F, 0, 1, 3); + + Mat magnitude; + magnitude = sobel_x.mul(sobel_x) + sobel_y.mul(sobel_y); // Gx^2 + Gy^2 + sqrt(magnitude, magnitude); // sqrt(Gx^2 + Gy^2) + + Scalar meanMagnitude = mean(magnitude); + double sharpness = meanMagnitude[0] / (gray.rows * gray.cols) * 1000; + if (sharpness < thres) { + err = string_format("image too blurry: %lf < %lf", sharpness, thres); + return false; } - return check_blur_by_laplacian(ps, gray, err); + return true; +} + +static +bool check_sharpness(ProcessState &ps, Mat &gray, int method, string &err) +{ + if (method == 0) { + return check_blur_by_laplacian(ps, gray, err); + } else if (method == 1) { + return check_blur_by_energy_gradient(gray, err); + } else if (method == 2) { + return check_blur_by_sobel(ps, gray, err); + } + err = "unknown sharpness method: " + to_string(method); + return false; } #define COUNT_COMPONENTS 0 @@ -429,7 +454,7 @@ bool emblem_dot_angle(ProcessState &ps, InputArray in, float &angle, string &qrc qrcode = ps.qrcode; - if (!check_blur(ps, ps.dot_area_gray, err)) { + if (!check_sharpness(ps, ps.dot_area_gray, ps.sharpness_method, err)) { return false; } diff --git a/alg/libqr.h b/alg/libqr.h index cd2c4e0..ffc06c1 100644 --- a/alg/libqr.h +++ b/alg/libqr.h @@ -25,6 +25,7 @@ struct ProcessState { string qrcode = ""; double clarity; float laplacian_thres = 0.1; + int sharpness_method = 2; }; bool preprocess(ProcessState &ps); diff --git a/alg/qrtool_wasm.cpp b/alg/qrtool_wasm.cpp index fd8567e..6cfd914 100644 --- a/alg/qrtool_wasm.cpp +++ b/alg/qrtool_wasm.cpp @@ -23,10 +23,12 @@ std::string make_resp(bool ok, string err, int angle = -1, string qrcode = "", d extern "C" { -const char *qrtool_angle(uint8_t *data, int width, int height, uint8_t *dot_area, float camera_sensitivity) { +const char *qrtool_angle(uint8_t *data, + int width, int height, + uint8_t *dot_area, + float camera_sensitivity) { ProcessState ps; ps.laplacian_thres = camera_sensitivity / 10.0; - auto start = std::chrono::system_clock::now(); static char ret[512]; printf("qrtool_angle, width: %d height %d\n", width, height);