基于opencv的HSV跟踪有色目标

import cv2 as cv
import numpy as np

#加载视频
def extract_object_demo1():
    capture = cv.VideoCapture('slow.mp4') #按Esc退出视频
    while True:
        ret, frame = capture.read()
        if ret == False:
            break
        cv.imshow('video', frame)
        c = cv.waitKey(40)
        if c == 27 :
            break

#
def extract_object_demo():
    capture = cv.VideoCapture('线齿轮搓制视频2.mp4') #按Esc退出视频
    #src=cv.imread(r'E:\imageload\example.png') 
    while True:
        ret, frame = capture.read()
        if ret == False:
            break
        hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        # lower_hsv = np.array([37,43,46])          #查阅根目录下的图1,这里表示绿色
        # upper_hsv = np.array([77,255,255])

        #lower_hsv = np.array([0,0,221])            #查阅根目录下的图1,这里表示绿白色
        #upper_hsv = np.array([180,30,255])

        lower_hsv = np.array([160,43,46])           #查阅根目录下的图1,这里表示绿白色
        upper_hsv = np.array([165,255,255])


        mask = cv.inRange(hsv,lowerb = lower_hsv,upperb = upper_hsv) #获得二值图像
        #cv.imshow('video',frame)
        cv.imshow('mark',mask)


        #cv.imshow('video', frame)
        c = cv.waitKey(40)
        if c == 27 :
            break

# def color_space_demo(image):
#   gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
#   cv.imshow('gray',gray)
#   hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV)
#   cv.imshow('hsv',hsv)
#   yuv = cv.cvtColor(image,cv.COLOR_BGR2YUV)
#   cv.imshow('yuv',yuv)
#   Ycrcb = cv.cvtColor(image,cv.COLOR_BGR2YCrCb)
#   cv.imshow('Ycrcb',Ycrcb)




# src = cv.imread('./1.jpg')
# color_space_demo(src)

extract_object_demo()
cv.waitKey(0)
image.png

你可能感兴趣的:(基于opencv的HSV跟踪有色目标)