// 简单实现
cv::namedWindow("Example 2-3", cv::WINDOW_AUTOSIZE); cv::VideoCapture cap; cap.open(0);
cout << "Opened file: " << argv[1] << endl; cv::Mat frame, frame_threshold;
int low_r = 30, low_g = 30, low_b = 30;
int high_r = 100, high_g = 100, high_b = 100; for (;;) { cap >> frame; if (frame.empty()) break; // Ran out of film inRange(frame, cv::Scalar(low_b, low_g, low_r), cv::Scalar(high_b, high_g, high_r), frame_threshold);
//-- Show the frames
imshow("Video Capture", frame);
imshow("Object Detection", frame_threshold); if ((char)cv::waitKey(33) >= 0) break; } return 0;

  

// 完整实现
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
void on_low_r_thresh_trackbar(int, void*);
void on_high_r_thresh_trackbar(int, void*);
void on_low_g_thresh_trackbar(int, void*);
void on_high_g_thresh_trackbar(int, void*);
void on_low_b_thresh_trackbar(int, void*);
void on_high_b_thresh_trackbar(int, void*);
int low_r = 30, low_g = 30, low_b = 30;
int high_r = 100, high_g = 100, high_b = 100;
int main()
{
Mat frame, frame_threshold;
VideoCapture cap(0);
namedWindow("Video Capture", WINDOW_NORMAL);
namedWindow("Object Detection", WINDOW_NORMAL);
//-- Trackbars to set thresholds for RGB values
createTrackbar("Low R", "Object Detection", &low_r, 255, on_low_r_thresh_trackbar);
createTrackbar("High R", "Object Detection", &high_r, 255, on_high_r_thresh_trackbar);
createTrackbar("Low G", "Object Detection", &low_g, 255, on_low_g_thresh_trackbar);
createTrackbar("High G", "Object Detection", &high_g, 255, on_high_g_thresh_trackbar);
createTrackbar("Low B", "Object Detection", &low_b, 255, on_low_b_thresh_trackbar);
createTrackbar("High B", "Object Detection", &high_b, 255, on_high_b_thresh_trackbar);
while ((char)waitKey(1) != 'q') {
cap >> frame;
if (frame.empty())
break;
//-- Detect the object based on RGB Range Values
inRange(frame, Scalar(low_b, low_g, low_r), Scalar(high_b, high_g, high_r), frame_threshold);
//-- Show the frames
imshow("Video Capture", frame);
imshow("Object Detection", frame_threshold);
}
return 0;
}
void on_low_r_thresh_trackbar(int, void*)
{
low_r = min(high_r - 1, low_r);
setTrackbarPos("Low R", "Object Detection", low_r);
}
void on_high_r_thresh_trackbar(int, void*)
{
high_r = max(high_r, low_r + 1);
setTrackbarPos("High R", "Object Detection", high_r);
}
void on_low_g_thresh_trackbar(int, void*)
{
low_g = min(high_g - 1, low_g);
setTrackbarPos("Low G", "Object Detection", low_g);
}
void on_high_g_thresh_trackbar(int, void*)
{
high_g = max(high_g, low_g + 1);
setTrackbarPos("High G", "Object Detection", high_g);
}
void on_low_b_thresh_trackbar(int, void*)
{
low_b = min(high_b - 1, low_b);
setTrackbarPos("Low B", "Object Detection", low_b);
}
void on_high_b_thresh_trackbar(int, void*)
{
high_b = max(high_b, low_b + 1);
setTrackbarPos("High B", "Object Detection", high_b);
}

  参考:https://www.w3cschool.cn/opencv/opencv-k1vh2cod.html

最新文章

  1. z-index和transform
  2. 在Windows7下启动MongoDB服务的解决方案
  3. 50 个最棒的 jQuery 日历插件,很齐全了!(转)
  4. 在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现Web端直传,服务端签名直传并设置上传回调的实现流程
  5. tomcat7.0建立新的web服务目录
  6. EXT经验--在调试中通过查看handler的第一个参数的xtype得知该参数信息及该handler的归属
  7. UML2.0统一建模语言
  8. PHPCMS 详情页静态化
  9. 201521123012 《Java程序设计》第七周学习总结
  10. zoj 2136 Longest Ordered Subsequence 最长上升子序列 新思路
  11. MySQL事务-ROLLBACK,COMMIT用法详解
  12. PYTHON-模块 re subprocess
  13. python模块学习之hashlib模块学习
  14. window注册鼠标右键菜单,及子菜单
  15. Confluence 6 企业环境或者网站托管的 Java 配置策略
  16. 使Volley完美支持自定义证书的Https
  17. Python全栈开发-Day6-面向对象编程
  18. 5.2 C++重载操作符的优先级
  19. Linux学习(1)- TCP/IP网络协议基础
  20. Spring Cloud构建微服务架构

热门文章

  1. Rancher 使用介绍(可以通过界面管理 K8s 平台)
  2. ABAP 写入批次特征值以及更新批次特征值
  3. C++ STL中的二分法
  4. 真的,Web安全入门看这个就够了!
  5. hive中对时间处理的函数总结
  6. AR设备使用Vuforia的优化
  7. centos7 elasticsearch集群安装
  8. sequlizejs学习笔记整理
  9. [转并修改]C#编程中跨线程访问控件
  10. ubuntu 16.04升级到18.04 出现apt-get问题解决