summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_pt/point_extractor.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-10-02 14:48:19 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-10-02 14:48:34 +0200
commit6dd3c13390a69add2c92fb5d69a5ab8367b12e9a (patch)
tree8f339c2ceb033df412517f54997159b6808bb8ab /ftnoir_tracker_pt/point_extractor.cpp
parent4a2f89042595a5caefc1ee286b6ddca8e7714adc (diff)
pt: add histogram-based thresholding
Otsu-based thresholding doesn't work in normal lighting conditions. Sponsored-by: TrackHat
Diffstat (limited to 'ftnoir_tracker_pt/point_extractor.cpp')
-rw-r--r--ftnoir_tracker_pt/point_extractor.cpp33
1 files changed, 27 insertions, 6 deletions
diff --git a/ftnoir_tracker_pt/point_extractor.cpp b/ftnoir_tracker_pt/point_extractor.cpp
index 0501a5c6..0ac2fc32 100644
--- a/ftnoir_tracker_pt/point_extractor.cpp
+++ b/ftnoir_tracker_pt/point_extractor.cpp
@@ -65,12 +65,33 @@ std::vector<cv::Vec2f> PointExtractor::extract_points(cv::Mat& frame)
}
else
{
- cv::Mat frame_bin_, tmp;
- int scale = frame_gray.cols > 400 ? 2 : 1;
- constexpr int size = 3;
- static cv::Mat kernel = cv::getGaussianKernel(size * scale + 1, CV_32F);
- cv::sepFilter2D(frame_gray, tmp, -1, kernel, kernel);
- cv::threshold(tmp, frame_bin_, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);
+ cv::Mat hist;
+ cv::calcHist(std::vector<cv::Mat> { frame_gray },
+ std::vector<int> { 0 },
+ cv::Mat(),
+ hist,
+ std::vector<int> { 256 },
+ std::vector<float> { 0, 256 },
+ false);
+ const int sz = hist.rows*hist.cols;
+ int val = 0;
+ int cnt = 0;
+ constexpr int min_pixels = 2000;
+ const int pixels_to_include = std::max(0, static_cast<int>(min_pixels * (1. - s.threshold / 100.)));
+ for (int i = sz-1; i >= 0; i--)
+ {
+ cnt += hist.at<float>(i);
+ if (cnt >= pixels_to_include)
+ {
+ val = i;
+ break;
+ }
+ }
+ val *= .95;
+ //qDebug() << "cnt" << cnt << "val" << val;
+
+ cv::Mat frame_bin_;
+ cv::threshold(frame_gray, frame_bin_, val, 255, CV_THRESH_BINARY);
frame_bin.setTo(170, frame_bin_);
cv::findContours(frame_bin_, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
}