From 6dd3c13390a69add2c92fb5d69a5ab8367b12e9a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 2 Oct 2015 14:48:19 +0200 Subject: pt: add histogram-based thresholding Otsu-based thresholding doesn't work in normal lighting conditions. Sponsored-by: TrackHat --- ftnoir_tracker_pt/point_extractor.cpp | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'ftnoir_tracker_pt/point_extractor.cpp') 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 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 { frame_gray }, + std::vector { 0 }, + cv::Mat(), + hist, + std::vector { 256 }, + std::vector { 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(min_pixels * (1. - s.threshold / 100.))); + for (int i = sz-1; i >= 0; i--) + { + cnt += hist.at(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); } -- cgit v1.2.3