From cd4a2d0f985d2fd065ad21d2074468df1e05cc3f Mon Sep 17 00:00:00 2001 From: Maurice ONeal Date: Fri, 30 Sep 2022 16:18:39 -0400 Subject: [PATCH] v1.4.t4 potentially fixed what was apparently a long standing bug that caused motion detection to look at just the first block. this bug was found thanks to the stats output. --- src/common.cpp | 6 ++++++ src/common.h | 2 +- src/mo_detect.cpp | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index f7d7fbe..818d775 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -252,6 +252,12 @@ bool rdConf(shared_t *share) } while(!line.empty()); + // it's imperative that blockX/Y are not zero or it will cause + // an infinte loop. if bad data is read from the conf, default + // values will be used. + if (share->blockX == 0) share->blockX = 32; + if (share->blockY == 0) share->blockY = 32; + if (share->init) { remove_all(share->buffDir.c_str()); diff --git a/src/common.h b/src/common.h index 7a42f38..60e727d 100644 --- a/src/common.h +++ b/src/common.h @@ -35,7 +35,7 @@ using namespace std; using namespace std::filesystem; #define BUF_SZ 10 -#define APP_VER "1.4.t3" +#define APP_VER "1.4.t4" struct shared_t { diff --git a/src/mo_detect.cpp b/src/mo_detect.cpp index 15653d4..950182c 100644 --- a/src/mo_detect.cpp +++ b/src/mo_detect.cpp @@ -27,7 +27,7 @@ bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share) return diff != 0; } -void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share) +void secDiff(const Mat &imgA, const Mat &imgB, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share) { auto pnts = 0; @@ -60,18 +60,16 @@ void secDiff(Mat imgA, Mat imgB, int rows, int cols, int rowOffs, int colOffs, v bool imgDiff(Mat prev, Mat next, Rect *block, shared_t *share) { - auto numOfXBlocks = prev.cols / share->blockX; - auto numOfYBlocks = prev.rows / share->blockY; - auto moInBlock = false; + auto moInBlock = false; vector threads; vector results; mutex secMutex; - for (auto x = 0; x < numOfXBlocks; x += share->blockX) + for (auto x = 0; x < prev.cols; x += share->blockX) { // spawn all of the block motion detection threads. - for (auto y = 0; y < numOfYBlocks; y += share->blockY) + for (auto y = 0; y < prev.rows; y += share->blockY) { threads.push_back(thread(secDiff, prev, next, share->blockY, share->blockX, y, x, &results, &secMutex, share)); } @@ -92,9 +90,16 @@ bool imgDiff(Mat prev, Mat next, Rect *block, shared_t *share) // the block with the highest amount of pixDiff. auto x = results[i].x; auto y = results[i].y; + auto xSz = results[i].xSize; + auto ySz = results[i].ySize; auto diff = results[i].pixDiff; - share->stat += "block_thread:" + string(" x=") + to_string(x) + " y=" + to_string(y) + " pixdiff=" + to_string(diff) + "\n"; + share->stat += "block_thread:" + + string(" x=") + to_string(x) + + " y=" + to_string(y) + + " x_len=" + to_string(xSz) + + " y_len=" + to_string(ySz) + + " pixdiff=" + to_string(diff) + "\n"; if ((results[i].pixDiff >= share->blockThresh) && (results[i].pixDiff > maxPixDiff)) { @@ -132,8 +137,12 @@ bool moDetect(const string &buffFile, Rect *block, Mat *img, shared_t *share) Mat prev; Mat next; - while (capPair(prev, next, capture, share)) + share->stat += "motion_detection -- clip_file - " + buffFile + "\n"; + + for (auto i = 0; capPair(prev, next, capture, share); ++i) { + share->stat += "frame_pair-- " + to_string(i) + "\n"; + if (imgDiff(toGray(prev), toGray(next), block, share)) { *img = next;