// 简单实现
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. PO培训

    30M 的step by step.带notes  这个文档把常用的包含java都有 不止是配置,反正各种处理都有.. 各种高清无码截图,还不算附件内容

  2. java的%d和%f 是什么意思

    Java中,%d和%f分别用来表示输出时,替换整型输出和浮点型输出的占位符. 如: int a=28; float b = 13.0f; System.out.printf("整数是:%d% ...

  3. 如何使用visual studio code的插件remote ssh远程操作virtual box虚拟机

    0 Remote-SSH是什么?为什么要用它? The Remote-SSH extension lets you use any remote machine with a SSH server a ...

  4. Visual Studio 2017(vs2017)绿色便携版-北桃特供

    原版的VisualStudio2017有几十G,安装起来特别慢,不少用户叫苦连天.该版本是精简过的vs2017,且简化了原来的安装程序,特别适用于教学.个人开发者.某些要求不高的企业. 该绿色便携版是 ...

  5. vs2019中添加rdlc的报表设计器

    在Visual studio 2019中,不会默认安装rdlc的报表设计器,所以需要自行添加. 1. 打开VS2019, 找到扩展-->管理扩展 2. 在扩展管理中,点击"联机&quo ...

  6. API 文档

    API 文档 Java类的组织形式 使用API查找方法: 包 -> 类 -> 方法 直接检索:Math

  7. 虚拟机中 Linux 提示“设备上没有空间”,扩容磁盘

    查看一下磁盘空间使用情况 #df -hl 已使用100% ls /dev/sd*  先查看一下现有sd系统硬盘 关机,存个快照(存不存无所谓),然后在虚拟机设置里添加扩展磁盘容量,选择SCSI类型 重 ...

  8. 复习第一点-1.跑通一个helloworld

    创建项目 导入需要的jar包 对编译出现的jar包处理 整理项目架构 编写配置文件中的内容 web.xml <?xml version="1.0" encoding=&quo ...

  9. 收缩SqlServer数据库日志语句

    收缩SqlServer数据库日志SQL语句 USE[master] ALTER DATABASE XXX SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE ...

  10. Typora的一些基础用法

    Typora的简单实用技巧 标题 标题分为h1~h6六个等级,数字越小标题越大. 定义标题有一下几种方式. 方式一:这个标题手敲就在文本前边敲#号,#和文本中间需又空格隔开. 方式二:快捷键,Ctrl ...