<14>. Thresholding Operations using inRange

#include “opencv2/imgproc.hpp”
#include “opencv2/highgui.hpp”
#include “opencv2/videoio.hpp”
#include

using namespace cv;

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;

static void on_low_H_thresh_trackbar(int, void *)
{
low_H = min(high_H-1, low_H);
setTrackbarPos(“Low H”, window_detection_name, low_H);
}
static void on_high_H_thresh_trackbar(int, void *)
{
high_H = max(high_H, low_H+1);
setTrackbarPos(“High H”, window_detection_name, high_H);
}
static void on_low_S_thresh_trackbar(int, void *)
{
low_S = 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 = 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 = 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 = max(high_V, low_V+1);
setTrackbarPos(“High V”, window_detection_name, high_V);
}

int main(int argc, char* argv[])
{
VideoCapture cap(0);
namedWindow(window_capture_name);
namedWindow(window_detection_name);
// 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);
Mat frame, frame_HSV, frame_threshold;
while (true) {
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);
// Show the frames
imshow(window_capture_name, frame);
imshow(window_detection_name, frame_threshold);
char key = (char) waitKey(30);
if (key == ‘q’ || key == 27)
{
break;
}
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值