Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ namespace {
const int LUMINANCE_BITS = 5;
const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
const ArrayRef<char> EMPTY(0);
} // namespace

GlobalHistogramBinarizer::GlobalHistogramBinarizer(Ref<LuminanceSource> source)
: Binarizer(source), luminances(EMPTY), buckets(LUMINANCE_BUCKETS) {
: Binarizer(source), luminances(), buckets(LUMINANCE_BUCKETS) {
filtered = false;
}

GlobalHistogramBinarizer::~GlobalHistogramBinarizer() {}

void GlobalHistogramBinarizer::initArrays(int luminanceSize) {
if (luminances->size() < luminanceSize) {
if (!luminances || luminances->size() < luminanceSize) {
luminances = ArrayRef<char>(luminanceSize);
}
for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
Expand Down
47 changes: 47 additions & 0 deletions modules/wechat_qrcode/test/test_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,52 @@ TEST(Objdetect_QRCode_bug, issue_3478) {
ASSERT_EQ("KFCVW50 ", outs[0]);
}

class ScopedThreadsGuard
{
public:
ScopedThreadsGuard() : old_threads(getNumThreads()) {}
~ScopedThreadsGuard() { setNumThreads(old_threads); }
void set(int n) { setNumThreads(n); }
private:
int old_threads;
};

TEST(Objdetect_QRCode_bug, issue_3405_multithread) {
const std::string expect_msg = "OpenCV_multithread_3405";
QRCodeEncoder::Params params;
params.version = 4;
Ptr<QRCodeEncoder> qrcode_enc = cv::QRCodeEncoder::create(params);
Mat qrRaw;
qrcode_enc->encode(expect_msg, qrRaw);

Mat qrImg;
cv::resize(qrRaw, qrImg, Size(0, 0), 4.0, 4.0, INTER_NEAREST);
cv::copyMakeBorder(qrImg, qrImg, 16, 16, 16, 16, BORDER_CONSTANT, Scalar(255));

wechat_qrcode::WeChatQRCode detector;
const int numCalls = 512;
std::vector<std::string> decoded(numCalls);
Mat blank(32, 32, CV_8UC1, Scalar(128));

// the race needs calls running at the same time
ScopedThreadsGuard threadsGuard;
threadsGuard.set(std::max(2, getNumThreads()));

cv::parallel_for_(Range(0, numCalls), [&](const Range& range) {
for (int i = range.start; i < range.end; ++i) {
std::vector<std::string> emptyRes = detector.detectAndDecode(blank);
EXPECT_TRUE(emptyRes.empty());
std::vector<std::string> res = detector.detectAndDecode(qrImg);
if (!res.empty()) {
decoded[i] = res[0];
}
}
}, numCalls);

for (int i = 0; i < numCalls; ++i) {
EXPECT_EQ(expect_msg, decoded[i]) << "Mismatch at iteration " << i;
}
}

} // namespace
} // namespace opencv_test