optical flow calculations use up a lot of processing power even at the
block level so I decided to take it back out. once again, no objection
detection is going to be used and will fall back to pixel diffs only.
also modified pixel diffs to decrement pixel diffs of no diff is
detected, going test how this works out.
This commit is contained in:
Maurice ONeal 2022-10-14 11:42:59 -04:00
parent 17c136b6bc
commit 9ecace7e4b
2 changed files with 20 additions and 41 deletions

View File

@ -13,24 +13,17 @@
#include "mo_detect.h" #include "mo_detect.h"
bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share) bool pixDiff(const uchar &pixA, const uchar &pixB, shared_t *share)
{
if (pixA > pixB) return true;
if (pixB > pixA) return true;
return false;
}
void secDiff(const Mat &imgA, const Mat &imgB, int id, int rows, int cols, int rowOffs, int colOffs, vector<sec_t> *results, mutex *secMutex, shared_t *share)
{ {
auto diff = 0; auto diff = 0;
if (pixA > pixB) diff = pixA - pixB;
if (pixB > pixA) diff = pixB - pixA;
if (diff < share->colorThresh)
{
diff = 0;
}
return diff != 0;
}
void secDiff(const Mat &imgA, const Mat &imgB, int rows, int cols, int rowOffs, int colOffs, vector<sec_t> *results, mutex *secMutex, shared_t *share)
{
auto pnts = 0;
for (auto y = rowOffs; y < (rowOffs + rows); y++) for (auto y = rowOffs; y < (rowOffs + rows); y++)
{ {
for (auto x = colOffs; x < (colOffs + cols); x++) for (auto x = colOffs; x < (colOffs + cols); x++)
@ -38,20 +31,21 @@ void secDiff(const Mat &imgA, const Mat &imgB, int rows, int cols, int rowOffs,
auto pixA = imgA.at<uchar>(Point(x, y)); auto pixA = imgA.at<uchar>(Point(x, y));
auto pixB = imgB.at<uchar>(Point(x, y)); auto pixB = imgB.at<uchar>(Point(x, y));
if (pixDiff(pixA, pixB, share)) if (pixDiff(pixA, pixB, share)) diff += 1;
{ else diff -= 1;
pnts += 1;
}
} }
} }
if (diff < 0) diff = 0;
struct sec_t res; struct sec_t res;
res.x = colOffs; res.x = colOffs;
res.y = rowOffs; res.y = rowOffs;
res.xSize = cols; res.xSize = cols;
res.ySize = rows; res.ySize = rows;
res.pixDiff = pnts; res.pixDiff = diff;
res.id = id;
lock_guard<mutex> guard(*secMutex); lock_guard<mutex> guard(*secMutex);
@ -64,12 +58,14 @@ bool imgDiff(const Mat &prev, const Mat &next, shared_t *share)
vector<sec_t> results; vector<sec_t> results;
mutex secMutex; mutex secMutex;
auto id = 0;
for (auto x = 0; x < prev.cols; x += share->blockX) for (auto x = 0; x < prev.cols; x += share->blockX)
{ {
// spawn all of the block motion detection threads. // 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 x = results[i].x;
auto y = results[i].y; auto y = results[i].y;
auto diff = results[i].pixDiff; auto diff = results[i].pixDiff;
auto id = results[i].id;
share->stat += string("block_thread:") share->stat += string("block_thread:") + " id=" + to_string(id) + " diff=" + to_string(diff) + "\n";
+ " x=" + to_string(x)
+ " y=" + to_string(y)
+ " pixdiff=" + to_string(diff)
+ "\n";
if ((results[i].pixDiff >= share->blockThresh) && (results[i].pixDiff > maxPixDiff)) 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) return 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;
}
} }
bool moDetect(const string &buffFile, shared_t *share) bool moDetect(const string &buffFile, shared_t *share)
@ -129,8 +110,6 @@ bool moDetect(const string &buffFile, shared_t *share)
Mat prev; Mat prev;
Mat next; Mat next;
share->stat += "motion_detection-- clip_file - " + buffFile + "\n";
while (capPair(prev, next, capture, share)) while (capPair(prev, next, capture, share))
{ {
if (imgDiff(toGray(prev), toGray(next), share)) if (imgDiff(toGray(prev), toGray(next), share))

View File

@ -14,10 +14,10 @@
// GNU General Public License for more details. // GNU General Public License for more details.
#include "common.h" #include "common.h"
#include "obj_detect.h"
struct sec_t struct sec_t
{ {
int id;
int x; int x;
int y; int y;
int xSize; int xSize;
@ -25,7 +25,7 @@ struct sec_t
int pixDiff; int pixDiff;
}; };
void secDiff(const Mat &imgA, const Mat &imgB, int rows, int cols, int rowOffs, int colOffs, vector<sec_t> *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<sec_t> *results, mutex *secMutex, shared_t *share);
bool pixDiff(const uchar &pixA, const uchar &pixB, 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 imgDiff(const Mat &prev, const Mat &next, shared_t *share);
bool moDetect(const string &buffFile, shared_t *share); bool moDetect(const string &buffFile, shared_t *share);