opencv轨迹条的运用

简介: 轨迹条运用

1.创造窗口,创造轨迹条

cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",179,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",0,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",255,255,empty)
cv2.createTrackbar("Val Min","TrackBars",85,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)

2.把轨迹条的参数改变和图片关联起来

    h_min = cv2.getTrackbarPos("Hue Min", "TrackBars")
    h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
    s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
    s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
    v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
    v_max = cv2.getTrackbarPos("Val Max", "TrackBars")

3.运用于图片

    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])
    mask = cv2.inRange(imgHSV, lower, upper)
    imgResult=cv2.bitwise_and(img,img,mask=mask)

4.完整代码

import cv2
import numpy as np

def empty(a):
    pass

path="D:/shijue/bij.jpg"
#创建轨迹条
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",179,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",0,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",255,255,empty)
cv2.createTrackbar("Val Min","TrackBars",85,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)

while True:
    img=cv2.imread(path)
    imgHSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    #应用于图片
    h_min = cv2.getTrackbarPos("Hue Min", "TrackBars")
    h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
    s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
    s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
    v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
    v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
    print(h_max,h_min,s_min,s_max,v_max,v_min)
    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])
    mask = cv2.inRange(imgHSV, lower, upper)
    imgResult=cv2.bitwise_and(img,img,mask=mask)
    cv2.namedWindow("Orginal",0)
    cv2.resizeWindow("Orginal",600,400)
    cv2.namedWindow("rest",0)
    cv2.resizeWindow("rest",600,400)
    cv2.imshow("Orginal", img)
    cv2.imshow("rest",imgResult)
    if cv2.waitKey(1)&0xFF==ord('p'):  #按任意键停止
        break

5.运行结果

在这里插入图片描述
在这里插入图片描述

目录
相关文章
|
计算机视觉 C++ Python
OpenCV人脸检测打卡系统
OpenCV人脸检测打卡系统
96 0
|
5月前
|
计算机视觉
OpenCV轮廓分析
OpenCV轮廓分析
|
6月前
|
存储 计算机视觉 流计算
【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
263 0
|
6月前
|
计算机视觉
OpenCV HSV物体跟踪
OpenCV HSV物体跟踪
41 0
|
C++ 容器
OpenCV-获取图像中圆线上的数据
OpenCV-获取图像中圆线上的数据
|
存储 定位技术 C++
OpenCV-获取图像中直线上的数据
OpenCV-获取图像中直线上的数据
106 0
|
XML 机器学习/深度学习 计算机视觉
利用OpenCV检测人脸(python实现)
利用OpenCV检测人脸(python实现)
174 0
|
API 计算机视觉
OpenCV 轨迹条Trackba应用
OpenCV 轨迹条Trackba应用
139 0
OpenCV 轨迹条Trackba应用
|
计算机视觉
OpenCV | OpenCV检测图像轮廓
OpenCV | OpenCV检测图像轮廓
157 0
OpenCV | OpenCV检测图像轮廓
|
计算机视觉
基于人脸关键点的姿态定位【OpenCV】【OpenGL】
基于人脸关键点的姿态定位【OpenCV】【OpenGL】
168 0
基于人脸关键点的姿态定位【OpenCV】【OpenGL】