confidence check has been added; structure cleanup;
This commit is contained in:
		
							
								
								
									
										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 <cmath>
 | 
			
		||||
#include "../../UI.h"
 | 
			
		||||
#include "../UI.h"
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
 | 
			
		||||
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<std::vector<cv::Point>> &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);
 | 
			
		||||
	}
 | 
			
		||||
@@ -9,8 +9,8 @@
 | 
			
		||||
#define RECTANGLE_DETECTION_CONTOURANALYZER_H_
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <opencv2/opencv.hpp>
 | 
			
		||||
#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<std::vector<cv::Point>> &contours, cv::Mat &frame);
 | 
			
		||||
	void drawContoursOntoMat(cv::Mat &frame);
 | 
			
		||||
	std::string contourToString(std::vector<cv::Point>);
 | 
			
		||||
	void convertContoursToBounds();
 | 
			
		||||
	std::vector<cv::Rect> getBounds() const;
 | 
			
		||||
@@ -1,14 +1,17 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
#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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user