structure change
This commit is contained in:
parent
df23c44b77
commit
092ae56e00
1
.gitignore
vendored
Normal file → Executable file
1
.gitignore
vendored
Normal file → Executable file
@ -96,3 +96,4 @@ local.properties
|
||||
|
||||
|
||||
# End of https://www.gitignore.io/api/eclipse
|
||||
/Debug/
|
||||
|
@ -5,25 +5,22 @@
|
||||
* Author: Yunyang
|
||||
*/
|
||||
|
||||
#include "../Shape/ContourAnalyzer.h"
|
||||
#include "ContourAnalyzer.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
ContourAnalyzer::ContourAnalyzer() {
|
||||
|
||||
}
|
||||
|
||||
void ContourAnalyzer::setFrame(cv::Mat frame) {
|
||||
ContourAnalyzer::originFrame = frame;
|
||||
ContourAnalyzer::ContourAnalyzer(UI *ui) {
|
||||
ContourAnalyzer::ui = ui;
|
||||
}
|
||||
|
||||
int ContourAnalyzer::analyze() {
|
||||
prunedContours.clear();
|
||||
|
||||
cv::cvtColor(originFrame, stepFrames[0], cv::COLOR_BGR2GRAY);
|
||||
cv::GaussianBlur(stepFrames[0], stepFrames[1], cv::Size(5, 5), 0);
|
||||
cv::Canny(stepFrames[1], stepFrames[2], 35, 125);
|
||||
cv::findContours(stepFrames[2], contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
|
||||
cv::cvtColor(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::COLOR_BGR2GRAY);
|
||||
cv::GaussianBlur(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::Size(5, 5), 0);
|
||||
cv::threshold(*(ui->currentFrame(-1)), *(ui->nextFrame()), 123, 255, cv::THRESH_BINARY);
|
||||
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++) {
|
||||
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) {
|
||||
switch (contour.size()) {
|
||||
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 {
|
||||
return bounds;
|
||||
}
|
39
src/Rectangle/Detection/ContourAnalyzer.h
Executable file
39
src/Rectangle/Detection/ContourAnalyzer.h
Executable 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_ */
|
@ -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_ */
|
55
src/main.cpp
55
src/main.cpp
@ -1,58 +1,35 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Rectangle/Detection/ContourAnalyzer.h"
|
||||
#include "webcam/Webcam.h"
|
||||
#include "detection/shape/ContourAnalyzer.h"
|
||||
#include "UI.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Webcam webcam = 0;
|
||||
ContourAnalyzer ca;
|
||||
bool debug = false;
|
||||
int debugFrame = 0;
|
||||
int lastKey;
|
||||
int state = 0;
|
||||
int *lastKey = new int(0);
|
||||
UI *ui = new UI(lastKey);
|
||||
ContourAnalyzer ca{ui};
|
||||
while (true) {
|
||||
webcam.update();
|
||||
cv::Mat frame = webcam.getFrame();
|
||||
ca.setFrame(frame);
|
||||
ui->setOriginalFrame(frame);
|
||||
ca.analyze();
|
||||
ca.drawShapePositions(frame);
|
||||
|
||||
|
||||
if (debug) {
|
||||
cv::imshow("debug", ca.getDebugFrame(debugFrame));
|
||||
}
|
||||
|
||||
cv::imshow("Normal", frame);
|
||||
if (lastKey > 0) {
|
||||
std::cout << "key pressed: " << lastKey << std::endl;
|
||||
if (lastKey == 27) {
|
||||
*lastKey = cv::waitKey(33);
|
||||
if (*lastKey != 255) {
|
||||
std::cout << "key pressed: " << *lastKey << std::endl;
|
||||
if (*lastKey == 27) {
|
||||
break;
|
||||
} else {
|
||||
if (lastKey < 52 && lastKey > 48) {
|
||||
debugFrame = lastKey - 49;
|
||||
}
|
||||
|
||||
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);
|
||||
ui->render();
|
||||
|
||||
*lastKey = 255;
|
||||
}
|
||||
|
||||
delete ui;
|
||||
delete lastKey;
|
||||
cv::destroyAllWindows();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user