dynamic lower bin changing based on brightness
This commit is contained in:
parent
1a4101e800
commit
00d045c304
@ -49,7 +49,7 @@ bool Confidence::check(std::vector<cv::Rect> bounds) {
|
|||||||
if (drawBounds) {
|
if (drawBounds) {
|
||||||
drawBoundsOnMat(*(ui->drawnFrame()));
|
drawBoundsOnMat(*(ui->drawnFrame()));
|
||||||
cv::putText(*(ui->drawnFrame()), "Showing confident rectangles.",
|
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);
|
return (confident.size() > 0);
|
||||||
|
@ -13,18 +13,23 @@ void ContourAnalyzer::analyze() {
|
|||||||
preAnalyze();
|
preAnalyze();
|
||||||
|
|
||||||
cv::cvtColor(*(ui->currentFrame(-1)), *(ui->nextFrame()), cv::COLOR_BGR2GRAY);
|
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::threshold(*(ui->currentFrame(-1)), *(ui->nextFrame()), binLower, 255, cv::THRESH_BINARY);
|
||||||
cv::Canny(*(ui->currentFrame(-1)), *(ui->nextFrame()), cannyLower, cannyUpper);
|
cv::Canny(*(ui->currentFrame(-1)), *(ui->nextFrame()), cannyLower, cannyUpper);
|
||||||
cv::findContours(*(ui->currentFrame()), contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
|
cv::findContours(*(ui->currentFrame()), contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
|
||||||
|
|
||||||
removeRedundancy();
|
findRectangles();
|
||||||
|
|
||||||
if (drawContours) {
|
if (drawContours) {
|
||||||
drawContoursOntoMat(*(ui->drawnFrame()));
|
drawContoursOntoMat(*(ui->drawnFrame()));
|
||||||
cv::putText(*(ui->drawnFrame()), "Showing possible rectangles.",
|
cv::putText(*(ui->drawnFrame()), "Showing possible rectangles.",
|
||||||
cv::Point{5, 30}, cv::FONT_HERSHEY_PLAIN, 0.8, selectColor);
|
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) {
|
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::threshold(cropped, cropped, binLower, 255, cv::THRESH_BINARY);
|
||||||
cv::Canny(cropped, cropped, cannyLower, cannyUpper);
|
cv::Canny(cropped, cropped, cannyLower, cannyUpper);
|
||||||
cv::findContours(cropped, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
|
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++) {
|
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)) {
|
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);
|
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() {
|
void ContourAnalyzer::preAnalyze() {
|
||||||
prunedContours.clear();
|
prunedContours.clear();
|
||||||
if (*lastKey == 99) {
|
if (*lastKey == 99) {
|
||||||
cannyRec = !cannyRec;
|
cannyRec = !cannyRec;
|
||||||
} else if (*lastKey == 118) {
|
} else
|
||||||
|
if (*lastKey == 109) {
|
||||||
|
bindBrightnessTobinLower = !bindBrightnessTobinLower;
|
||||||
|
} else
|
||||||
|
if (*lastKey == 118) {
|
||||||
drawContours = !drawContours;
|
drawContours = !drawContours;
|
||||||
}
|
}
|
||||||
|
|
||||||
minSize = ui->getWidth()*ui->getHeight()*(minSizeScale/(float)10000);
|
minSize = ui->getWidth()*ui->getHeight()*(minSizeScale/(float)10000);
|
||||||
|
|
||||||
if (cannyRec) {
|
if (cannyRec) {
|
||||||
cannyUpper = std::min(3*cannyLower, 255);
|
cannyUpper = std::min(3*cannyLower, 255);
|
||||||
cv::setTrackbarPos("Can. Upper", UI::DEBUG_WINDOW, cannyUpper);
|
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) {
|
void ContourAnalyzer::drawContoursOntoMat(cv::Mat &frame) {
|
||||||
@ -109,6 +144,10 @@ std::vector<cv::RotatedRect> ContourAnalyzer::getRotatedBounds() const {
|
|||||||
return rotatedBounds;
|
return rotatedBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double ContourAnalyzer::getBrightness() const {
|
||||||
|
return brightness;
|
||||||
|
}
|
||||||
|
|
||||||
void ContourAnalyzer::componentSetup() {
|
void ContourAnalyzer::componentSetup() {
|
||||||
cv::createTrackbar("Bin. Lower", UI::DEBUG_WINDOW, &binLower , 255);
|
cv::createTrackbar("Bin. Lower", UI::DEBUG_WINDOW, &binLower , 255);
|
||||||
cv::createTrackbar("Can. Lower", UI::DEBUG_WINDOW, &cannyLower , 255);
|
cv::createTrackbar("Can. Lower", UI::DEBUG_WINDOW, &cannyLower , 255);
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
class ContourAnalyzer: public UIListener {
|
class ContourAnalyzer: public UIListener {
|
||||||
std::shared_ptr<int> lastKey;
|
std::shared_ptr<int> lastKey;
|
||||||
bool cannyRec = false, drawContours = false;
|
bool cannyRec = false, bindBrightnessTobinLower = true, drawContours = false;
|
||||||
std::shared_ptr<UI> ui;
|
std::shared_ptr<UI> ui;
|
||||||
std::vector<std::vector<cv::Point>> contours;
|
std::vector<std::vector<cv::Point>> contours;
|
||||||
std::vector<std::vector<cv::Point>> inconsistentContours;
|
std::vector<std::vector<cv::Point>> inconsistentContours;
|
||||||
@ -16,8 +16,11 @@ class ContourAnalyzer: public UIListener {
|
|||||||
std::vector<std::vector<cv::Point>> prunedContours;
|
std::vector<std::vector<cv::Point>> prunedContours;
|
||||||
std::vector<cv::Rect> bounds;
|
std::vector<cv::Rect> bounds;
|
||||||
std::vector<cv::RotatedRect> rotatedBounds;
|
std::vector<cv::RotatedRect> rotatedBounds;
|
||||||
|
cv::Mat luminescenceMat;
|
||||||
|
double brightness = 0;
|
||||||
|
int lastBrightnessCheck = 60;
|
||||||
|
|
||||||
void removeRedundancy();
|
void findRectangles();
|
||||||
void preAnalyze();
|
void preAnalyze();
|
||||||
public:
|
public:
|
||||||
int binLower = 54;
|
int binLower = 54;
|
||||||
@ -39,6 +42,8 @@ public:
|
|||||||
std::string contourToString(std::vector<cv::Point>);
|
std::string contourToString(std::vector<cv::Point>);
|
||||||
void convertContoursToBounds();
|
void convertContoursToBounds();
|
||||||
void convertContoursToRotatedBounds();
|
void convertContoursToRotatedBounds();
|
||||||
|
void suggestCalcBrightness();
|
||||||
|
double getBrightness() const;
|
||||||
std::vector<cv::Rect> getBounds() const;
|
std::vector<cv::Rect> getBounds() const;
|
||||||
std::vector<cv::RotatedRect> getRotatedBounds() const;
|
std::vector<cv::RotatedRect> getRotatedBounds() const;
|
||||||
void componentSetup();
|
void componentSetup();
|
||||||
|
@ -16,7 +16,7 @@ class Ruler: public UIListener {
|
|||||||
float distance = 0;
|
float distance = 0;
|
||||||
float givenWidth = 0;
|
float givenWidth = 0;
|
||||||
float focalLength = 0;
|
float focalLength = 0;
|
||||||
int HFOV = 54;
|
int HFOV = 40;
|
||||||
bool inputtingVal = false;
|
bool inputtingVal = false;
|
||||||
std::deque<char> numBuild;
|
std::deque<char> numBuild;
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ void run() {
|
|||||||
case detection:
|
case detection:
|
||||||
cv::putText(*(ui->drawnFrame()), "Looking for rectangle...",
|
cv::putText(*(ui->drawnFrame()), "Looking for rectangle...",
|
||||||
statusTextPos, cv::FONT_HERSHEY_PLAIN, 0.8, textFontColor);
|
statusTextPos, cv::FONT_HERSHEY_PLAIN, 0.8, textFontColor);
|
||||||
|
ca.suggestCalcBrightness();
|
||||||
ca.analyze();
|
ca.analyze();
|
||||||
ca.convertContoursToBounds();
|
ca.convertContoursToBounds();
|
||||||
if (conf.check(ca.getBounds())) {
|
if (conf.check(ca.getBounds())) {
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
#include "Webcam.h"
|
#include "Webcam.h"
|
||||||
|
|
||||||
Webcam::Webcam() {
|
Webcam::Webcam() {
|
||||||
stream->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
|
|
||||||
stream->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Webcam::update() {
|
void Webcam::update() {
|
||||||
|
Loading…
Reference in New Issue
Block a user