diff --git a/src/mo_detect.cpp b/src/mo_detect.cpp index 1ec2d20..83b5ba2 100644 --- a/src/mo_detect.cpp +++ b/src/mo_detect.cpp @@ -14,22 +14,15 @@ bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share) { - auto diff = 0; + if (pixA > pixB) return true; + if (pixB > pixA) return true; - if (pixA > pixB) diff = pixA - pixB; - if (pixB > pixA) diff = pixB - pixA; - - if (diff < share->colorThresh) - { - diff = 0; - } - - return diff != 0; + return false; } -void secDiff(const Mat &imgA, const 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 id, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share) { - auto pnts = 0; + auto diff = 0; for (auto y = rowOffs; y < (rowOffs + rows); y++) { @@ -38,20 +31,21 @@ void secDiff(const Mat &imgA, const Mat &imgB, int rows, int cols, int rowOffs, auto pixA = imgA.at(Point(x, y)); auto pixB = imgB.at(Point(x, y)); - if (pixDiff(pixA, pixB, share)) - { - pnts += 1; - } + if (pixDiff(pixA, pixB, share)) diff += 1; + else diff -= 1; } } + if (diff < 0) diff = 0; + struct sec_t res; res.x = colOffs; res.y = rowOffs; res.xSize = cols; res.ySize = rows; - res.pixDiff = pnts; + res.pixDiff = diff; + res.id = id; lock_guard guard(*secMutex); @@ -64,12 +58,14 @@ bool imgDiff(const Mat &prev, const Mat &next, shared_t *share) vector results; mutex secMutex; + auto id = 0; + for (auto x = 0; x < prev.cols; x += share->blockX) { // spawn all of the block motion detection threads. - for (auto y = 0; y < prev.rows; y += share->blockY) + for (auto y = 0; y < prev.rows; y += share->blockY, id += 1) { - threads.push_back(thread(secDiff, prev, next, share->blockY, share->blockX, y, x, &results, &secMutex, share)); + threads.push_back(thread(secDiff, prev, next, id, share->blockY, share->blockX, y, x, &results, &secMutex, share)); } } @@ -89,12 +85,9 @@ bool imgDiff(const Mat &prev, const Mat &next, shared_t *share) auto x = results[i].x; auto y = results[i].y; auto diff = results[i].pixDiff; + auto id = results[i].id; - share->stat += string("block_thread:") - + " x=" + to_string(x) - + " y=" + to_string(y) - + " pixdiff=" + to_string(diff) - + "\n"; + share->stat += string("block_thread:") + " id=" + to_string(id) + " diff=" + to_string(diff) + "\n"; if ((results[i].pixDiff >= share->blockThresh) && (results[i].pixDiff > maxPixDiff)) { @@ -103,19 +96,7 @@ bool imgDiff(const Mat &prev, const Mat &next, shared_t *share) } } - if (maxPixDiff >= share->blockThresh) - { - // run optical flow calcuations on the block with the highest - // pixDiff. - auto res = results[blockPick]; - auto block = Rect(res.x, res.y, res.xSize, res.ySize); - - return objectFlowInImage(prev, next, block, share); - } - else - { - return false; - } + return maxPixDiff >= share->blockThresh; } bool moDetect(const string &buffFile, shared_t *share) @@ -129,8 +110,6 @@ bool moDetect(const string &buffFile, shared_t *share) Mat prev; Mat next; - share->stat += "motion_detection-- clip_file - " + buffFile + "\n"; - while (capPair(prev, next, capture, share)) { if (imgDiff(toGray(prev), toGray(next), share)) diff --git a/src/mo_detect.h b/src/mo_detect.h index 575ef53..fd35186 100644 --- a/src/mo_detect.h +++ b/src/mo_detect.h @@ -14,10 +14,10 @@ // GNU General Public License for more details. #include "common.h" -#include "obj_detect.h" struct sec_t { + int id; int x; int y; int xSize; @@ -25,7 +25,7 @@ struct sec_t int pixDiff; }; -void secDiff(const Mat &imgA, const 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 id, int rows, int cols, int rowOffs, int colOffs, vector *results, mutex *secMutex, shared_t *share); bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share); bool imgDiff(const Mat &prev, const Mat &next, shared_t *share); bool moDetect(const string &buffFile, shared_t *share);