structure change

This commit is contained in:
Harrison Deng 2018-05-06 02:05:21 -05:00
parent df23c44b77
commit 092ae56e00
5 changed files with 71 additions and 91 deletions

1
.gitignore vendored Normal file → Executable file
View File

@ -96,3 +96,4 @@ local.properties
# End of https://www.gitignore.io/api/eclipse # End of https://www.gitignore.io/api/eclipse
/Debug/

View File

@ -5,25 +5,22 @@
* Author: Yunyang * Author: Yunyang
*/ */
#include "../Shape/ContourAnalyzer.h" #include "ContourAnalyzer.h"
#include <cmath> #include <cmath>
ContourAnalyzer::ContourAnalyzer() { ContourAnalyzer::ContourAnalyzer(UI *ui) {
ContourAnalyzer::ui = ui;
}
void ContourAnalyzer::setFrame(cv::Mat frame) {
ContourAnalyzer::originFrame = frame;
} }
int ContourAnalyzer::analyze() { int ContourAnalyzer::analyze() {
prunedContours.clear(); prunedContours.clear();
cv::cvtColor(originFrame, stepFrames[0], cv::COLOR_BGR2GRAY); cv::cvtColor(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::COLOR_BGR2GRAY);
cv::GaussianBlur(stepFrames[0], stepFrames[1], cv::Size(5, 5), 0); cv::GaussianBlur(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::Size(5, 5), 0);
cv::Canny(stepFrames[1], stepFrames[2], 35, 125); cv::threshold(*(ui->currentFrame(-1)), *(ui->nextFrame()), 123, 255, cv::THRESH_BINARY);
cv::findContours(stepFrames[2], contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE); cv::Canny(*(ui->currentFrame(-1)), *(ui->nextFrame()), 35, 125);
cv::findContours(*(ui->currentFrame(-1)), contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
for (unsigned i = 0; i < contours.size(); i++) { for (unsigned i = 0; i < contours.size(); i++) {
double perimeter = cv::arcLength(contours[i], true); double perimeter = cv::arcLength(contours[i], true);
@ -67,6 +64,13 @@ void ContourAnalyzer::drawShapePositions(cv::Mat &frame) {
} }
} }
void ContourAnalyzer::convertContoursToBounds() {
bounds.clear();
for (unsigned i = 0; i < prunedContours.size(); i++) {
bounds.push_back(cv::boundingRect(prunedContours[i]));
}
}
std::string ContourAnalyzer::contourToString(std::vector<cv::Point> contour) { std::string ContourAnalyzer::contourToString(std::vector<cv::Point> contour) {
switch (contour.size()) { switch (contour.size()) {
case 4: case 4:
@ -79,10 +83,6 @@ std::string ContourAnalyzer::contourToString(std::vector<cv::Point> contour) {
} }
} }
cv::Mat ContourAnalyzer::getDebugFrame(int step) const {
return stepFrames[step];
}
std::vector<cv::Rect> ContourAnalyzer::getBounds() const { std::vector<cv::Rect> ContourAnalyzer::getBounds() const {
return bounds; return bounds;
} }

View File

@ -0,0 +1,39 @@
/*
* ContourAnalyzer.h
*
* Created on: Apr 22, 2018
* Author: Yunyang
*/
#ifndef RECTANGLE_DETECTION_CONTOURANALYZER_H_
#define RECTANGLE_DETECTION_CONTOURANALYZER_H_
#include <string>
#include <opencv2/opencv.hpp>
#include "../../UI.h"
class ContourAnalyzer {
UI *ui;
std::vector<std::vector<cv::Point>> contours;
std::vector<std::vector<cv::Point>> prunedContours;
std::vector<cv::Rect> bounds;
public:
int binLowerThresh = 123;
int binUpperThresh = 255;
ContourAnalyzer(UI* ui);
virtual ~ContourAnalyzer();
cv::Scalar selectColor { 0, 255, 0 };
cv::Scalar fontColor { 0, 0, 255 };
int analyze();
void drawShapePositions(cv::Mat &frame);
std::string contourToString(std::vector<cv::Point>);
void convertContoursToBounds();
std::vector<cv::Rect> getBounds() const;
};
#endif /* RECTANGLE_DETECTION_CONTOURANALYZER_H_ */

View File

@ -1,37 +0,0 @@
/*
* ContourAnalyzer.h
*
* Created on: Apr 22, 2018
* Author: Yunyang
*/
#ifndef DETECTION_SHAPE_CONTOURANALYZER_H_
#define DETECTION_SHAPE_CONTOURANALYZER_H_
#include <string>
#include <opencv2/opencv.hpp>
class ContourAnalyzer {
cv::Mat originFrame;
std::vector<cv::Mat> stepFrames{4};
std::vector<std::vector<cv::Point>> contours;
std::vector<std::vector<cv::Point>> prunedContours;
std::vector<cv::Rect> bounds;
public:
ContourAnalyzer();
virtual ~ContourAnalyzer();
cv::Scalar selectColor{0, 255, 0};
cv::Scalar fontColor{0, 0, 255};
int analyze();
void setFrame(cv::Mat frame);
void drawShapePositions(cv::Mat &frame);
std::string contourToString(std::vector<cv::Point>);
void convertContoursToBounds();
cv::Mat getDebugFrame(int step) const;
std::vector<cv::Rect> getBounds() const;
};
#endif /* DETECTION_SHAPE_CONTOURANALYZER_H_ */

View File

@ -1,58 +1,35 @@
#include <iostream> #include <iostream>
#include "Rectangle/Detection/ContourAnalyzer.h"
#include "webcam/Webcam.h" #include "webcam/Webcam.h"
#include "detection/shape/ContourAnalyzer.h" #include "UI.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
Webcam webcam = 0; Webcam webcam = 0;
ContourAnalyzer ca; int *lastKey = new int(0);
bool debug = false; UI *ui = new UI(lastKey);
int debugFrame = 0; ContourAnalyzer ca{ui};
int lastKey;
int state = 0;
while (true) { while (true) {
webcam.update(); webcam.update();
cv::Mat frame = webcam.getFrame(); cv::Mat frame = webcam.getFrame();
ca.setFrame(frame); ui->setOriginalFrame(frame);
ca.analyze(); ca.analyze();
ca.drawShapePositions(frame); ca.drawShapePositions(frame);
*lastKey = cv::waitKey(33);
if (debug) { if (*lastKey != 255) {
cv::imshow("debug", ca.getDebugFrame(debugFrame)); std::cout << "key pressed: " << *lastKey << std::endl;
} if (*lastKey == 27) {
cv::imshow("Normal", frame);
if (lastKey > 0) {
std::cout << "key pressed: " << lastKey << std::endl;
if (lastKey == 27) {
break; break;
} else { }
if (lastKey < 52 && lastKey > 48) { }
debugFrame = lastKey - 49; ui->render();
}
*lastKey = 255;
if (lastKey == 32) {
state++;
if (state >= 3) {
state = 0;
}
printf("state set to: " + state);
}
if (lastKey == 100) {
if (debug) {
cv::destroyWindow("debug");
debug = false;
} else {
debug = true;
}
}
}
lastKey = 0;
}
lastKey = cv::waitKey(33);
} }
delete ui;
delete lastKey;
cv::destroyAllWindows(); cv::destroyAllWindows();
return 0; return 0;
} }