From 00d045c3046eec78618b70dee56b4f9922c8960d Mon Sep 17 00:00:00 2001 From: Zer01HD Date: Fri, 25 May 2018 07:47:34 -0500 Subject: [PATCH] dynamic lower bin changing based on brightness --- src/Rectangle/Confidence.cpp | 2 +- src/Rectangle/ContourAnalyzer.cpp | 49 +++++++++++++++++++++++++++---- src/Rectangle/ContourAnalyzer.h | 9 ++++-- src/Rectangle/Ruler.h | 2 +- src/main.cpp | 1 + src/webcam/Webcam.cpp | 2 -- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/Rectangle/Confidence.cpp b/src/Rectangle/Confidence.cpp index 81d7036..f246f5b 100755 --- a/src/Rectangle/Confidence.cpp +++ b/src/Rectangle/Confidence.cpp @@ -49,7 +49,7 @@ bool Confidence::check(std::vector 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); diff --git a/src/Rectangle/ContourAnalyzer.cpp b/src/Rectangle/ContourAnalyzer.cpp index 919d77d..0c0061b 100755 --- a/src/Rectangle/ContourAnalyzer.cpp +++ b/src/Rectangle/ContourAnalyzer.cpp @@ -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(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 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); diff --git a/src/Rectangle/ContourAnalyzer.h b/src/Rectangle/ContourAnalyzer.h index cbf7b15..69f4b7a 100755 --- a/src/Rectangle/ContourAnalyzer.h +++ b/src/Rectangle/ContourAnalyzer.h @@ -8,7 +8,7 @@ class ContourAnalyzer: public UIListener { std::shared_ptr lastKey; - bool cannyRec = false, drawContours = false; + bool cannyRec = false, bindBrightnessTobinLower = true, drawContours = false; std::shared_ptr ui; std::vector> contours; std::vector> inconsistentContours; @@ -16,8 +16,11 @@ class ContourAnalyzer: public UIListener { std::vector> prunedContours; std::vector bounds; std::vector 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); void convertContoursToBounds(); void convertContoursToRotatedBounds(); + void suggestCalcBrightness(); + double getBrightness() const; std::vector getBounds() const; std::vector getRotatedBounds() const; void componentSetup(); diff --git a/src/Rectangle/Ruler.h b/src/Rectangle/Ruler.h index 5840b58..ee219dd 100755 --- a/src/Rectangle/Ruler.h +++ b/src/Rectangle/Ruler.h @@ -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 numBuild; diff --git a/src/main.cpp b/src/main.cpp index 2259690..924f32f 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -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())) { diff --git a/src/webcam/Webcam.cpp b/src/webcam/Webcam.cpp index 6b6033e..885009e 100755 --- a/src/webcam/Webcam.cpp +++ b/src/webcam/Webcam.cpp @@ -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() {