cv::inRange
// 简单实现
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的更多相关文章
- opencv函数之cv.InRange函数
2018-03-0421:22:46 (1)cv.InRange函数 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像const CvArr ...
- 『OpenCV3』基于色彩分割图片
一.遍历图像实现色彩掩码 本节我们实现这样一个算法,我们指定某种颜色和一个阈值,根据输入图片生成一张掩码,标记符合的像素(和指定颜色的差异在阈值容忍内). 源代码如下,我们使用一个class完成这个目 ...
- openCV 色彩空间
---恢复内容开始--- 1.使用cv2.inrange()获取某个范围内的图像取值,指定某个通道的最小值和最大值 import numpy as np def color_space(image): ...
- Python+OpenCV图像处理(四)—— 色彩空间
一.色彩空间的转换 代码如下: #色彩空间转换 import cv2 as cv def color_space_demo(img): gray = cv.cvtColor(img, cv.COLOR ...
- 车道线识别/Opencv/传统方法
车道检测(Advanced Lane Finding Project) 实现步骤: 使用提供的一组棋盘格图片计算相机校正矩阵(camera calibration matrix)和失真系数(disto ...
- OpenCV---像素运算
像素运算 分为算术运算和逻辑运算 算术运算: 加减乘除 调节亮度 调整对比度 逻辑运算: 与或非 遮罩层控制 一:算术运算 import cv2 as cv import numpy as np de ...
- OpenCV---色彩空间(二)HSV追踪颜色对象和通道分离与合并
一:HSV追踪有颜色对象 def inRange(src, lowerb, upperb, dst=None) #lowerb是上面每个颜色分段的最小值,upperb是上面每个颜色分段的最大值,都是列 ...
- 3、OpenCV Python 色彩空间
__author__ = "WSX" import cv2 as cv import numpy as np def color_space( img ): gray_img = ...
- [视觉识别]OpenCV + CNN 大神符识别
数据集 Mnist数据集:http://yann.lecun.com/exdb/mnist/ 训练 import numpy as np from keras.datasets import mnis ...
- 用Camshift算法对指定目标进行跟踪
原理 Camshift算法是Continuously Adaptive Mean Shift algorithm的简称. 它是一个基于MeanSift的改进算法.它首次由Gary R.Bradski等 ...
随机推荐
- MySQL5.7升级版本到8.0
升级二进制包安装的MySQL In-Place Upgrade(替代升级) 替代升级涉及到shutdown down旧版本的MySQL,用新版本的包替代旧版本的二进制包,用存在的数据文件目录重启MyS ...
- Nodejs杀死本地应用(win)
windows端nodejs检查应用运行并杀死. import {exec, execSync} from "child_process" import {decode} from ...
- 数据库基础day1
数据库基础 MySQL概述 SQL 函数 概念 函数 是指一段可以直接被另一段程序调用的程序或代码. 3.1字符串函数 函数 功能 CONCAT(S1,S2,...Sn) 字符串拼接,将S1,S2,. ...
- Oracle查看用户占用的表空间大小
SELECT owner, tablespace_name, ROUND (SUM (BYTES) / 1024 / 1024, 2) "USED(M)" FROM dba_seg ...
- python 操作配置文件(configparser模块)
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser 配置文件信息: [DEFAULT] ServerAliveInterval = 45 Com ...
- 炫酷 css实现水波纹
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第23天,点击查看活动详情 ui设计的元素有时候需要有一些动画效果,可以直接用css动画来实现. 实现一个中心圆向四周有水波纹的效果 ...
- Oracle UNDO表空间的管理
Oracle UNDO表空间的管理 UNDO表空间的管理是Oracle DBA最重要的日常工作之一,UNDO表空间用来暂时存储DML操作的数据,其主要作用有: 1.事务回滚 2.实例恢复 3.读一致性 ...
- AgilePoin规则执行
我在写窗体规则时,明明默认值已经绑定,但是在页面加载时规则并没有起作用,导致改隐藏的没隐藏,该显示的不显示.找了半天,发现规则设置时可选择执行事件. 设置在页面加载时执行规则后,发现还是不能正确显隐组 ...
- docker容器部署flask单页面应用
本地安装docker,拉取centos镜像. docker pull centos:7 本地文件结构: /usr/local/var/tmp/docker_demo .app ---requireme ...
- Java-Java调用mysqldump进行数据库备份
1 public ResultData backupDatabase(Integer type) { 2 // 构建备份sql的文件名 3 String sqlFileName = "tes ...