Files
opencv-MIRROR/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp
Vincent Rabaud 95844c51cb Remove std using-declarations and directives from public headers.
Re-exporting standard math and algorithm symbols from
modules/core/include/opencv2/core/cvstd.hpp into `namespace cv`
caused overload ambiguity between `cv::` array operations and
`std::` scalar templates, and `using namespace std` in `datasets`
and `hdf` public headers leaked `std` into downstream translation
units. Those conflicts previously required extra `const Mat&` and
`const UMat&` overloads of `cv::min` and `cv::max` in
modules/core/include/opencv2/core.hpp.

Drop `using std::min`, `std::max`, `std::swap`, `std::sqrt`,
`std::exp`, `std::pow`, and `std::log` from `cvstd.hpp` (keeping
`using std::abs`), `using std::nullptr_t` from
`cvstd_wrapper.hpp`, and `using namespace std` from
`track_vot.hpp`, `track_alov.hpp`, and `hdf5.hpp`. Remove the
obsolete `Mat` and `UMat` `min`/`max` overloads, qualify affected
calls across OpenCV modules with `std::`, and drop unused
`using std::*` declarations from module sources. Simplify
`std::pow(x, 0.5)` calls to `std::sqrt(x)`, collapse nested
`std::min`/`std::max` chains to initializer-list calls, and
replace constant-bound `min`/`max` pairs with `std::clamp`.
Existing runtime behavior is unchanged.
2026-11-29 00:00:25 +01:00

106 lines
3.0 KiB
C++

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace cv;
/** Global Variables */
const int max_value_H = 360/2;
const int max_value = 255;
const String window_capture_name = "Video Capture";
const String window_detection_name = "Object Detection";
int low_H = 0, low_S = 0, low_V = 0;
int high_H = max_value_H, high_S = max_value, high_V = max_value;
//! [low]
static void on_low_H_thresh_trackbar(int, void *)
{
low_H = std::min(high_H-1, low_H);
setTrackbarPos("Low H", window_detection_name, low_H);
}
//! [low]
//! [high]
static void on_high_H_thresh_trackbar(int, void *)
{
high_H = std::max(high_H, low_H+1);
setTrackbarPos("High H", window_detection_name, high_H);
}
//! [high]
static void on_low_S_thresh_trackbar(int, void *)
{
low_S = std::min(high_S-1, low_S);
setTrackbarPos("Low S", window_detection_name, low_S);
}
static void on_high_S_thresh_trackbar(int, void *)
{
high_S = std::max(high_S, low_S+1);
setTrackbarPos("High S", window_detection_name, high_S);
}
static void on_low_V_thresh_trackbar(int, void *)
{
low_V = std::min(high_V-1, low_V);
setTrackbarPos("Low V", window_detection_name, low_V);
}
static void on_high_V_thresh_trackbar(int, void *)
{
high_V = std::max(high_V, low_V+1);
setTrackbarPos("High V", window_detection_name, high_V);
}
int main(int argc, char* argv[])
{
//! [cap]
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
//! [cap]
//! [window]
namedWindow(window_capture_name);
namedWindow(window_detection_name);
//! [window]
//! [trackbar]
// Trackbars to set thresholds for HSV values
createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
//! [trackbar]
Mat frame, frame_HSV, frame_threshold;
while (true) {
//! [while]
cap >> frame;
if(frame.empty())
{
break;
}
// Convert from BGR to HSV colorspace
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
// Detect the object based on HSV Range Values
inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
//! [while]
//! [show]
// Show the frames
imshow(window_capture_name, frame);
imshow(window_detection_name, frame_threshold);
//! [show]
char key = (char) waitKey(30);
if (key == 'q' || key == 27)
{
break;
}
}
return 0;
}