案例-使用python实现基于opencv的进度条控制图像

该案例是一个基础的opencv交互案例,主要过程是使用TrackBar()功能实现。具体而言就是将RGB图片转为HSV格式。然后使用进度条调整数值。来取出想要的颜色区域。可以为后期颜色屏幕笔案例做基础。具体代码如下:

import cv2
import numpy as np

def empty(v):
    pass

img = cv2.imread("pyopencv_again/XiWinnie.jpg")
img = cv2.resize(img,(0,0),fx=0.5,fy=0.5)
cv2.namedWindow('TrackBar')
cv2.resizeWindow('TrackBar',640,320)

cv2.createTrackbar('Hue Min','TrackBar',0,179,empty)
cv2.createTrackbar('Hue Max','TrackBar',179,179,empty)
cv2.createTrackbar('Sat Min','TrackBar',0,255,empty)
cv2.createTrackbar('Sat Max','TrackBar',255,255,empty)
cv2.createTrackbar('Val Min','TrackBar',0,255,empty)
cv2.createTrackbar('Val Max','TrackBar',255,255,empty)

hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
while True:
    #获取进度条上设置的值
    h_min = cv2.getTrackbarPos('Hue Min','TrackBar')
    h_max = cv2.getTrackbarPos('Hue Max','TrackBar')
    s_min = cv2.getTrackbarPos('Sat Min','TrackBar')
    s_max = cv2.getTrackbarPos('Sat Max','TrackBar')
    v_min = cv2.getTrackbarPos('Val Min','TrackBar')
    v_max = cv2.getTrackbarPos('Val Max','TrackBar')
    #print(h_min,h_max,s_min,s_max,v_min,v_max)
    #将数值存为数组
    lower = np.array([h_min,s_min,v_min])
    upper = np.array([h_max,s_max,v_max])
    #print(lower)  #[0,0,0]
    #inRange()函数可实现二值化功能(这点类似threshold()函数),更关键的是可以同时针对多通道进行操作,使用起来非常方便!
    mask = cv2.inRange(hsv,lower,upper)
    result = cv2.bitwise_and(img,img,mask=mask)

    cv2.imshow('img',img)
    cv2.imshow('hsv',hsv)
    cv2.imshow('mask',mask)
    cv2.imshow('result',result)
    cv2.waitKey(10)
上一篇:POJ 3585 Accumulation Degree


下一篇:用array来做一个stack