diff --git a/src/Rectangle/Confidence.cpp b/src/Rectangle/Confidence.cpp new file mode 100755 index 0000000..5caeab7 --- /dev/null +++ b/src/Rectangle/Confidence.cpp @@ -0,0 +1,64 @@ +#include "Confidence.h" + +Confidence::Confidence(int *lastKey, UI *ui, unsigned validityLength) { + queueLength = validityLength; + Confidence::lastKey = lastKey; + Confidence::ui = ui; +} + +bool Confidence::check(std::vector contours) { + bool consistent; + confident.clear(); + + if (*lastKey == 98) { + drawBounds = !drawBounds; + } + + for (unsigned rectangleID = 0; rectangleID < contours.size(); rectangleID++) { + consistent = true; + cv::Rect current = contours[rectangleID]; + + for (unsigned frameID = 0; frameID < confidenceQueue.size(); frameID++) { + if (confidenceQueue[frameID].size() == 0) { + consistent = false; + break; + } + for (unsigned frameRectID = 0; frameRectID < confidenceQueue[frameID].size(); frameRectID++) { + cv::Rect previous = confidenceQueue[frameID][frameRectID]; + if ((current & previous).area() < 0.78*(current.area())) { + consistent = false; + break; + } + } + } + if (consistent) { + confident.push_back(contours[rectangleID]); + } + } + + confidenceQueue.push_front(contours); + if (confidenceQueue.size() > queueLength) { + confidenceQueue.pop_back(); + } + + if (drawBounds) { + drawBoundsOnMat(*(ui->originalFrame())); + } + + return confident.size() != 0; +} + +std::vector Confidence::rectangleBounds() const { + return confident; +} + +void Confidence::drawBoundsOnMat(cv::Mat &mat) { + for (unsigned i = 0; i < confident.size(); i++) { + cv::rectangle(mat, confident[i], boundColor, 1); + } +} + + +Confidence::~Confidence() { + // TODO Auto-generated destructor stub +} diff --git a/src/Rectangle/Confidence.h b/src/Rectangle/Confidence.h new file mode 100755 index 0000000..d853906 --- /dev/null +++ b/src/Rectangle/Confidence.h @@ -0,0 +1,26 @@ +#ifndef RECTANGLE_CONFIDENCE_H_ +#define RECTANGLE_CONFIDENCE_H_ +#include +#include +#include +#include "../UI.h" + +class Confidence { + unsigned queueLength = 0; + int *lastKey; + UI *ui; + bool drawBounds = false; + cv::Scalar boundColor {30, 30, 255}; + +public: + std::deque> confidenceQueue; + std::vector confident; + + Confidence(int *lastKey, UI *ui, unsigned history = 5); + bool check(std::vector contours); + std::vector rectangleBounds() const; + void drawBoundsOnMat(cv::Mat &mat); + virtual ~Confidence(); +}; + +#endif /* RECTANGLE_CONFIDENCE_H_ */ diff --git a/src/Rectangle/Detection/ContourAnalyzer.cpp b/src/Rectangle/ContourAnalyzer.cpp similarity index 91% rename from src/Rectangle/Detection/ContourAnalyzer.cpp rename to src/Rectangle/ContourAnalyzer.cpp index 4d505a7..b88d86f 100755 --- a/src/Rectangle/Detection/ContourAnalyzer.cpp +++ b/src/Rectangle/ContourAnalyzer.cpp @@ -1,6 +1,6 @@ #include "ContourAnalyzer.h" #include -#include "../../UI.h" +#include "../UI.h" #include ContourAnalyzer::ContourAnalyzer(UI *ui, int *lastKey) { @@ -64,11 +64,11 @@ void ContourAnalyzer::analyze() { } if (drawContours) { - drawContoursOntoMat(prunedContours, *(ui->originalFrame())); + drawContoursOntoMat(*(ui->originalFrame())); } } -void ContourAnalyzer::drawContoursOntoMat(std::vector> &contours, cv::Mat &frame) { +void ContourAnalyzer::drawContoursOntoMat(cv::Mat &frame) { for (unsigned i = 0; i < prunedContours.size(); i++) { cv::drawContours(frame, prunedContours, i, selectColor, 1); } diff --git a/src/Rectangle/Detection/ContourAnalyzer.h b/src/Rectangle/ContourAnalyzer.h similarity index 85% rename from src/Rectangle/Detection/ContourAnalyzer.h rename to src/Rectangle/ContourAnalyzer.h index 76602ae..29cc61a 100755 --- a/src/Rectangle/Detection/ContourAnalyzer.h +++ b/src/Rectangle/ContourAnalyzer.h @@ -9,8 +9,8 @@ #define RECTANGLE_DETECTION_CONTOURANALYZER_H_ #include #include -#include "../../UI.h" -#include "../../UIListener.hpp" +#include "../UI.h" +#include "../UIListener.hpp" class ContourAnalyzer: public UIListener { int *lastKey; @@ -37,7 +37,7 @@ public: cv::Scalar selectColor { 0, 255, 0 }; cv::Scalar fontColor { 0, 0, 255 }; void analyze(); - void drawContoursOntoMat(std::vector> &contours, cv::Mat &frame); + void drawContoursOntoMat(cv::Mat &frame); std::string contourToString(std::vector); void convertContoursToBounds(); std::vector getBounds() const; diff --git a/src/Rectangle/Tracking/RectangleTracker.cpp b/src/Rectangle/RectangleTracker.cpp similarity index 100% rename from src/Rectangle/Tracking/RectangleTracker.cpp rename to src/Rectangle/RectangleTracker.cpp diff --git a/src/Rectangle/Tracking/RectangleTracker.h b/src/Rectangle/RectangleTracker.h similarity index 100% rename from src/Rectangle/Tracking/RectangleTracker.h rename to src/Rectangle/RectangleTracker.h diff --git a/src/main.cpp b/src/main.cpp index f7e5509..51577e5 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,17 @@ #include -#include "Rectangle/Detection/ContourAnalyzer.h" +#include "Rectangle/ContourAnalyzer.h" #include "webcam/Webcam.h" #include "UI.h" +#include "Rectangle/Confidence.h" int main(int argc, char** argv) { Webcam webcam = 0; int *lastKey = new int(0); UI *ui = new UI(lastKey); ContourAnalyzer ca{ui, lastKey}; + Confidence conf{lastKey, ui, 6}; + while (true) { webcam.update(); cv::Mat frame = webcam.getFrame(); @@ -25,6 +28,8 @@ int main(int argc, char** argv) { ca.analyze(); + ca.convertContoursToBounds(); + conf.check(ca.getBounds()); ui->render(); *lastKey = 255;