// 简单实现
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. css3的的新特性

    1.transform 2.calc 3.transition

  2. antdVue 重置select和input的样式 去掉蓝色换成灰色

    代码实现: <template> <div> <a-select mode="tags" style="width: 200px" ...

  3. VUE学习-条件渲染

    条件渲染 v-if & v-else-if & v-else <div id="app"> <h1 v-if="type == 'VUE ...

  4. iOS ProtocolBuffer使用介绍

    ProtocolBuffer 简介 Protocol Buffer 是google 的一种数据交换的格式 Protocol Buffer 和 XML.JSON一样都是结构数据序列化的工具,但它们的数据 ...

  5. 设置view的圆角和阴影

    1.设置view圆角 self.backView.clipsToBounds = YES; self.backView.layer.cornerRadius = 6.f; 2.设置view阴影 sel ...

  6. 072_关于Dataloader导入Record的创建时间及修改时间并允许owner是Inactive

    1.在User interface 中 启用 Enable "Set Audit Fields upon Record Creation" and "Update Rec ...

  7. Java脚本操作mysql和接口

    一.Java操作MySQL 1.插入insert 1 import java.sql.*; 2 import java.util.UUID; 3 4 public class BigData { 5 ...

  8. snapshot备份

    snapshot C: "h:\esd\$date_$hour_$minute_C.sna" -L0 -R -G snapshot64.exe C: "H:\ESD\$d ...

  9. HCIP-进阶实验06-多实例生成树安全部署

    HCIP-ICT进阶实验06-多实例生成树安全部署 1 实验需求 1.1 实验拓扑 1.2 实验环境说明 IP地址规划表: 设备 接口 IP 地址 备注 SW1 VLANIF10 192.168.10 ...

  10. [OC] UIWebView APIs 的替换 以及转用WKWebView后的部分问题

    一.检查工程中的 UIWebView 1.打开终端,cd + 把项目的工程文件所在文件夹拖入终端(即 得到项目的工程文件所在的路径) 2.输入以下命令: grep -r UIWebView . 注意最 ...