confidence check has been added; structure cleanup;
This commit is contained in:
parent
e1cbdbae7d
commit
b595e557be
64
src/Rectangle/Confidence.cpp
Executable file
64
src/Rectangle/Confidence.cpp
Executable file
@ -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<cv::Rect> 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<cv::Rect> 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
|
||||||
|
}
|
26
src/Rectangle/Confidence.h
Executable file
26
src/Rectangle/Confidence.h
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef RECTANGLE_CONFIDENCE_H_
|
||||||
|
#define RECTANGLE_CONFIDENCE_H_
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
#include "../UI.h"
|
||||||
|
|
||||||
|
class Confidence {
|
||||||
|
unsigned queueLength = 0;
|
||||||
|
int *lastKey;
|
||||||
|
UI *ui;
|
||||||
|
bool drawBounds = false;
|
||||||
|
cv::Scalar boundColor {30, 30, 255};
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::deque<std::vector<cv::Rect>> confidenceQueue;
|
||||||
|
std::vector<cv::Rect> confident;
|
||||||
|
|
||||||
|
Confidence(int *lastKey, UI *ui, unsigned history = 5);
|
||||||
|
bool check(std::vector<cv::Rect> contours);
|
||||||
|
std::vector<cv::Rect> rectangleBounds() const;
|
||||||
|
void drawBoundsOnMat(cv::Mat &mat);
|
||||||
|
virtual ~Confidence();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* RECTANGLE_CONFIDENCE_H_ */
|
@ -1,6 +1,6 @@
|
|||||||
#include "ContourAnalyzer.h"
|
#include "ContourAnalyzer.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "../../UI.h"
|
#include "../UI.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
ContourAnalyzer::ContourAnalyzer(UI *ui, int *lastKey) {
|
ContourAnalyzer::ContourAnalyzer(UI *ui, int *lastKey) {
|
||||||
@ -64,11 +64,11 @@ void ContourAnalyzer::analyze() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (drawContours) {
|
if (drawContours) {
|
||||||
drawContoursOntoMat(prunedContours, *(ui->originalFrame()));
|
drawContoursOntoMat(*(ui->originalFrame()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContourAnalyzer::drawContoursOntoMat(std::vector<std::vector<cv::Point>> &contours, cv::Mat &frame) {
|
void ContourAnalyzer::drawContoursOntoMat(cv::Mat &frame) {
|
||||||
for (unsigned i = 0; i < prunedContours.size(); i++) {
|
for (unsigned i = 0; i < prunedContours.size(); i++) {
|
||||||
cv::drawContours(frame, prunedContours, i, selectColor, 1);
|
cv::drawContours(frame, prunedContours, i, selectColor, 1);
|
||||||
}
|
}
|
@ -9,8 +9,8 @@
|
|||||||
#define RECTANGLE_DETECTION_CONTOURANALYZER_H_
|
#define RECTANGLE_DETECTION_CONTOURANALYZER_H_
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <opencv2/opencv.hpp>
|
#include <opencv2/opencv.hpp>
|
||||||
#include "../../UI.h"
|
#include "../UI.h"
|
||||||
#include "../../UIListener.hpp"
|
#include "../UIListener.hpp"
|
||||||
|
|
||||||
class ContourAnalyzer: public UIListener {
|
class ContourAnalyzer: public UIListener {
|
||||||
int *lastKey;
|
int *lastKey;
|
||||||
@ -37,7 +37,7 @@ public:
|
|||||||
cv::Scalar selectColor { 0, 255, 0 };
|
cv::Scalar selectColor { 0, 255, 0 };
|
||||||
cv::Scalar fontColor { 0, 0, 255 };
|
cv::Scalar fontColor { 0, 0, 255 };
|
||||||
void analyze();
|
void analyze();
|
||||||
void drawContoursOntoMat(std::vector<std::vector<cv::Point>> &contours, cv::Mat &frame);
|
void drawContoursOntoMat(cv::Mat &frame);
|
||||||
std::string contourToString(std::vector<cv::Point>);
|
std::string contourToString(std::vector<cv::Point>);
|
||||||
void convertContoursToBounds();
|
void convertContoursToBounds();
|
||||||
std::vector<cv::Rect> getBounds() const;
|
std::vector<cv::Rect> getBounds() const;
|
@ -1,14 +1,17 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "Rectangle/Detection/ContourAnalyzer.h"
|
#include "Rectangle/ContourAnalyzer.h"
|
||||||
#include "webcam/Webcam.h"
|
#include "webcam/Webcam.h"
|
||||||
#include "UI.h"
|
#include "UI.h"
|
||||||
|
#include "Rectangle/Confidence.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
Webcam webcam = 0;
|
Webcam webcam = 0;
|
||||||
int *lastKey = new int(0);
|
int *lastKey = new int(0);
|
||||||
UI *ui = new UI(lastKey);
|
UI *ui = new UI(lastKey);
|
||||||
ContourAnalyzer ca{ui, lastKey};
|
ContourAnalyzer ca{ui, lastKey};
|
||||||
|
Confidence conf{lastKey, ui, 6};
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
webcam.update();
|
webcam.update();
|
||||||
cv::Mat frame = webcam.getFrame();
|
cv::Mat frame = webcam.getFrame();
|
||||||
@ -25,6 +28,8 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
|
|
||||||
ca.analyze();
|
ca.analyze();
|
||||||
|
ca.convertContoursToBounds();
|
||||||
|
conf.check(ca.getBounds());
|
||||||
ui->render();
|
ui->render();
|
||||||
|
|
||||||
*lastKey = 255;
|
*lastKey = 255;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user