// 简单实现
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

cv::inRange的更多相关文章

  1. opencv函数之cv.InRange函数

    2018-03-0421:22:46 (1)cv.InRange函数 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像const CvArr ...

  2. 『OpenCV3』基于色彩分割图片

    一.遍历图像实现色彩掩码 本节我们实现这样一个算法,我们指定某种颜色和一个阈值,根据输入图片生成一张掩码,标记符合的像素(和指定颜色的差异在阈值容忍内). 源代码如下,我们使用一个class完成这个目 ...

  3. openCV 色彩空间

    ---恢复内容开始--- 1.使用cv2.inrange()获取某个范围内的图像取值,指定某个通道的最小值和最大值 import numpy as np def color_space(image): ...

  4. Python+OpenCV图像处理(四)—— 色彩空间

    一.色彩空间的转换 代码如下: #色彩空间转换 import cv2 as cv def color_space_demo(img): gray = cv.cvtColor(img, cv.COLOR ...

  5. 车道线识别/Opencv/传统方法

    车道检测(Advanced Lane Finding Project) 实现步骤: 使用提供的一组棋盘格图片计算相机校正矩阵(camera calibration matrix)和失真系数(disto ...

  6. OpenCV---像素运算

    像素运算 分为算术运算和逻辑运算 算术运算: 加减乘除 调节亮度 调整对比度 逻辑运算: 与或非 遮罩层控制 一:算术运算 import cv2 as cv import numpy as np de ...

  7. OpenCV---色彩空间(二)HSV追踪颜色对象和通道分离与合并

    一:HSV追踪有颜色对象 def inRange(src, lowerb, upperb, dst=None) #lowerb是上面每个颜色分段的最小值,upperb是上面每个颜色分段的最大值,都是列 ...

  8. 3、OpenCV Python 色彩空间

    __author__ = "WSX" import cv2 as cv import numpy as np def color_space( img ): gray_img = ...

  9. [视觉识别]OpenCV + CNN 大神符识别

    数据集 Mnist数据集:http://yann.lecun.com/exdb/mnist/ 训练 import numpy as np from keras.datasets import mnis ...

  10. 用Camshift算法对指定目标进行跟踪

    原理 Camshift算法是Continuously Adaptive Mean Shift algorithm的简称. 它是一个基于MeanSift的改进算法.它首次由Gary R.Bradski等 ...

随机推荐

  1. shiyansi

    #include <stdio.h> #include <stdlib.h> #define N 1000 int fun(int n,int m,int bb[N]) { i ...

  2. Linux与Windows对比

    1. 前言 Windows是微软为个人台式机/设备或电脑(PC)开发的一系列操作系统.计算机操作系统(OS).每个操作系统都有一个图形用户界面(GUI),桌面允许用户查看所有文件.视频等.Window ...

  3. mapreduce和yarn集群

    mapreduce : 先分再合,分而治之 分布式计算概念: 计算方式,与集中式计算相对.将应用拆分成小的部分,分配给多台计算机处理,mapreduce是分布式的计算框架. MR的特点:易于编程,良好 ...

  4. k8s安装metrics-server

    Kubernetes Metrics Server: Kubernetes Metrics Server 是 Cluster 的核心监控数据的聚合器,kubeadm 默认是不部署的. Metrics ...

  5. 宝塔Linux定时shell

    定时清除缓存文件 rm -rfv /www/wwwroot/www.xxx.com/runtime 定时解压,常用于定时恢复站点 解压到当前 cd /www/wwwroot/www.xxx.com t ...

  6. java报错 SLF4J:Failed to load class "org.slf4j.impl.StaticLoggerBinder"

    <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artif ...

  7. django的模版

    django的模版 1模版语法  views.py 书写方式 def index(request): ''' 模版语法分: 变量: {{ }} 1. 深度查询 句点符 2. 过滤器 {{val|fil ...

  8. Qt项目移植

    从Qt低版本到高版本 一开始使用的是Qt Creator 4.6.2(Enterprise) 后面下载了Qt Creator 4.13.3(Enterprise) 一开始在Qt Creator 4.1 ...

  9. Win7+VS2010 环境配置

    最后再次总结一些,Win7下的VS2010总共有三个变量配置: 1. 变量名:path 变量值:D:\Program Files\Microsoft Visual Studio 10.0\VC\bin ...

  10. Android中动态添加tab

    来源过于啰嗦,这里只有简化后的. 转载请注明出处  http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html 建立对应的布 ...