HSV色彩空间筛选 2021-10-06

在这里插入图片描述
运行效果:(筛选蓝色)
白色为符合颜色空间的区域在这里插入图片描述
运行代码:

# -*- coding: utf-8 -*-

import cv2
import numpy as np

camera = cv2.VideoCapture(0)

def nothing(x):
    pass


# 创建一副黑色图像
img = np.zeros((300, 512, 3), np.uint8)

# 设置滑动条组件
cv2.namedWindow('HSV_Window')
cv2.createTrackbar('H_L', 'HSV_Window', 0, 255, nothing)
cv2.createTrackbar('H_H', 'HSV_Window', 0, 255, nothing)
cv2.createTrackbar('S_L', 'HSV_Window', 0, 255, nothing)
cv2.createTrackbar('S_H', 'HSV_Window', 0, 255, nothing)
cv2.createTrackbar('V_L', 'HSV_Window', 0, 255, nothing)
cv2.createTrackbar('V_H', 'HSV_Window', 0, 255, nothing)

while (1):

    success, frame = camera.read()
    cv2.imshow("HSV_Window", frame)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # 将读取的BGR转换为HSV

    H_L = cv2.getTrackbarPos('H_L', 'HSV_Window')
    H_H = cv2.getTrackbarPos('H_H', 'HSV_Window')
    S_L = cv2.getTrackbarPos('S_L', 'HSV_Window')
    S_H = cv2.getTrackbarPos('S_H', 'HSV_Window')
    V_L = cv2.getTrackbarPos('V_L', 'HSV_Window')
    V_H = cv2.getTrackbarPos('V_H', 'HSV_Window')


    lower = np.array([H_L, S_L, V_L])  # 所要检测的像素范围
    upper = np.array([H_H, S_H, V_H])  # 此处检测绿色区域

    mask = cv2.inRange(hsv, lowerb=lower, upperb=upper)
    cv2.imshow("mask", mask)

    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # sw = cv2.getTrackbarPos(switch, 'HSV_Window')

    # if s == 0:
    #     img[:] = 0
    # else:
    #     img[:] = [b, g, r]

# 销毁窗口
cv2.destroyAllWindows()

intel.py

## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.

###############################################
##      Open CV and Numpy integration        ##
###############################################

import pyrealsense2 as rs
import numpy as np
import cv2

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

found_rgb = False
for s in device.sensors:
    if s.get_info(rs.camera_info.name) == 'RGB Camera':
        found_rgb = True
        break
if not found_rgb:
    print("The demo requires Depth camera with Color sensor")
    exit(0)

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

if device_product_line == 'L500':
    config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# Start streaming
pipeline.start(config)

try:
    while True:

        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        # depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        # if not depth_frame or not color_frame:
        #     continue

        # Convert images to numpy arrays
        # depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        # depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

        # depth_colormap_dim = depth_colormap.shape
        color_colormap_dim = color_image.shape

        # If depth and color resolutions are different, resize color image to match depth image for display
        # if depth_colormap_dim != color_colormap_dim:
        #     resized_color_image = cv2.resize(color_image, dsize=(depth_colormap_dim[1], depth_colormap_dim[0]), interpolation=cv2.INTER_AREA)
        #     images = np.hstack((resized_color_image, depth_colormap))
        # else:
        #     images = np.hstack((color_image, depth_colormap))

        # Show images
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', color_colormap_dim)
        cv2.waitKey(1)

finally:

    # Stop streaming
    pipeline.stop()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值