dynamic lower bin changing based on brightness

This commit is contained in:
Harrison Deng 2018-05-25 07:47:34 -05:00
parent 1a4101e800
commit 00d045c304
6 changed files with 54 additions and 11 deletions

View File

@ -49,7 +49,7 @@ bool Confidence::check(std::vector<cv::Rect> bounds) {
if (drawBounds) {
drawBoundsOnMat(*(ui->drawnFrame()));
cv::putText(*(ui->drawnFrame()), "Showing confident rectangles.",
cv::Point{5, 45}, cv::FONT_HERSHEY_PLAIN, 0.8, boundColor);
cv::Point{5, 50}, cv::FONT_HERSHEY_PLAIN, 0.8, boundColor);
}
return (confident.size() > 0);

View File

@ -13,18 +13,23 @@ void ContourAnalyzer::analyze() {
preAnalyze();
cv::cvtColor(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::COLOR_BGR2GRAY);
cv::GaussianBlur(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::Size(5, 5), 0);
cv::GaussianBlur(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::Size(3, 3), 0);
cv::threshold(*(ui->currentFrame(-1)), *(ui->nextFrame()), binLower, 255, cv::THRESH_BINARY);
cv::Canny(*(ui->currentFrame(-1)), *(ui->nextFrame()), cannyLower, cannyUpper);
cv::findContours(*(ui->currentFrame()), contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
removeRedundancy();
findRectangles();
if (drawContours) {
drawContoursOntoMat(*(ui->drawnFrame()));
cv::putText(*(ui->drawnFrame()), "Showing possible rectangles.",
cv::Point{5, 30}, cv::FONT_HERSHEY_PLAIN, 0.8, selectColor);
}
cv::putText(*(ui->drawnFrame()), "Current brightness value: " + std::to_string(brightness),
cv::Point{5, 40}, cv::FONT_HERSHEY_PLAIN, 0.8, selectColor);
lastBrightnessCheck ++;
}
void ContourAnalyzer::analyze(cv::Rect rect) {
@ -38,10 +43,10 @@ void ContourAnalyzer::analyze(cv::Rect rect) {
cv::threshold(cropped, cropped, binLower, 255, cv::THRESH_BINARY);
cv::Canny(cropped, cropped, cannyLower, cannyUpper);
cv::findContours(cropped, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
removeRedundancy();
findRectangles();
}
void ContourAnalyzer::removeRedundancy() {
void ContourAnalyzer::findRectangles() {
for (unsigned i = 0; i < contours.size(); i++) {
if ((cv::contourArea(contours[i]) > minSize) && (cv::contourArea(contours[i]) < (ui->currentFrame(0)->cols * ui->currentFrame(0)->rows) -25)) {
double perimeter = cv::arcLength(contours[i], true);
@ -54,19 +59,49 @@ void ContourAnalyzer::removeRedundancy() {
}
}
void ContourAnalyzer::suggestCalcBrightness() {
if (lastBrightnessCheck >= 60) {
lastBrightnessCheck = 0;
cv::cvtColor(ui->getOriginalFrame(), luminescenceMat, cv::COLOR_BGR2GRAY);
int total = 0;
for (int i = 0; i < luminescenceMat.rows; i++) {
for (int j = 0; j < luminescenceMat.cols; j++) {
total += luminescenceMat.at<uchar>(i, j);
}
}
brightness = (double)total/(luminescenceMat.rows*luminescenceMat.cols);
}
}
void ContourAnalyzer::preAnalyze() {
prunedContours.clear();
if (*lastKey == 99) {
cannyRec = !cannyRec;
} else if (*lastKey == 118) {
} else
if (*lastKey == 109) {
bindBrightnessTobinLower = !bindBrightnessTobinLower;
} else
if (*lastKey == 118) {
drawContours = !drawContours;
}
minSize = ui->getWidth()*ui->getHeight()*(minSizeScale/(float)10000);
if (cannyRec) {
cannyUpper = std::min(3*cannyLower, 255);
cv::setTrackbarPos("Can. Upper", UI::DEBUG_WINDOW, cannyUpper);
}
if (bindBrightnessTobinLower) {
binLower = (0.5)*brightness + 4;
if (binLower > 255) {
binLower = 255;
} else if (binLower < 0) {
binLower = 60;
}
cv::setTrackbarPos("Bin. Lower", UI::DEBUG_WINDOW, binLower);
}
}
void ContourAnalyzer::drawContoursOntoMat(cv::Mat &frame) {
@ -109,6 +144,10 @@ std::vector<cv::RotatedRect> ContourAnalyzer::getRotatedBounds() const {
return rotatedBounds;
}
double ContourAnalyzer::getBrightness() const {
return brightness;
}
void ContourAnalyzer::componentSetup() {
cv::createTrackbar("Bin. Lower", UI::DEBUG_WINDOW, &binLower , 255);
cv::createTrackbar("Can. Lower", UI::DEBUG_WINDOW, &cannyLower , 255);

View File

@ -8,7 +8,7 @@
class ContourAnalyzer: public UIListener {
std::shared_ptr<int> lastKey;
bool cannyRec = false, drawContours = false;
bool cannyRec = false, bindBrightnessTobinLower = true, drawContours = false;
std::shared_ptr<UI> ui;
std::vector<std::vector<cv::Point>> contours;
std::vector<std::vector<cv::Point>> inconsistentContours;
@ -16,8 +16,11 @@ class ContourAnalyzer: public UIListener {
std::vector<std::vector<cv::Point>> prunedContours;
std::vector<cv::Rect> bounds;
std::vector<cv::RotatedRect> rotatedBounds;
cv::Mat luminescenceMat;
double brightness = 0;
int lastBrightnessCheck = 60;
void removeRedundancy();
void findRectangles();
void preAnalyze();
public:
int binLower = 54;
@ -39,6 +42,8 @@ public:
std::string contourToString(std::vector<cv::Point>);
void convertContoursToBounds();
void convertContoursToRotatedBounds();
void suggestCalcBrightness();
double getBrightness() const;
std::vector<cv::Rect> getBounds() const;
std::vector<cv::RotatedRect> getRotatedBounds() const;
void componentSetup();

View File

@ -16,7 +16,7 @@ class Ruler: public UIListener {
float distance = 0;
float givenWidth = 0;
float focalLength = 0;
int HFOV = 54;
int HFOV = 40;
bool inputtingVal = false;
std::deque<char> numBuild;

View File

@ -31,6 +31,7 @@ void run() {
case detection:
cv::putText(*(ui->drawnFrame()), "Looking for rectangle...",
statusTextPos, cv::FONT_HERSHEY_PLAIN, 0.8, textFontColor);
ca.suggestCalcBrightness();
ca.analyze();
ca.convertContoursToBounds();
if (conf.check(ca.getBounds())) {

View File

@ -1,8 +1,6 @@
#include "Webcam.h"
Webcam::Webcam() {
stream->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
stream->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
}
void Webcam::update() {