我是靠谱客的博主 悦耳蜗牛,最近开发中收集的这篇文章主要介绍(Opencv)在HSV颜色空间下做目标检测以及目标跟踪,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原视频:https://www.youtube.com/watch?v=3D7O_kZi8-o

我们有一张包含有不同种颜色球的图片,使用HSV颜色空间对某一种颜色的球做检测

大体框架

import cv2
import numpy as np

def nothing(x):
    pass
while True:
	# 读取的为BGR颜色空间
    frame = cv2.imread('smarties.png')
    cv2.imshow("frame", frame)
    # 等待1ms 检测ESC按键
    key = cv2.waitKey(1)
    if key == 27:
        break

cv2.destroyAllWindows()

将图片颜色空间从BGR转换为HSV

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

以蓝色为例,根据经验设定颜色范围的的大小阈值

l_b = np.array([110,50,50])
u_b = np.array([130,255,255])

让图片不在范围内的像素颜色值变为0,范围之内的变为255,制作遮罩

mask = cv2.inRange(hsv, l_b, u_b)

对白色范围进行保留

res = cv2.bitwise_and(frame, frame, mask=mask)

显示图片

cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("res", res)

完整代码

import cv2
import numpy as np
def nothing(x):
    pass
while True:
    # 读取的为BGR颜色空间
    frame = cv2.imread('smarties.png')
    # 将图片颜色空间从BGR转换为HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    l_b = np.array([110,50,50])
    u_b = np.array([130,255,255])
    mask = cv2.inRange(hsv, l_b, u_b)
    res = cv2.bitwise_and(frame, frame, mask=mask)
    cv2.imshow("frame", frame)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    key = cv2.waitKey(1)
    if key == 27:
        break
cv2.destroyAllWindows()

效果

下面我们通过滑动条来改变阈值,达到识别不同颜色的效果

cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)
while True:
    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")
    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")
    l_b = np.array([l_h, l_s, l_v])
    u_b = np.array([u_h, u_s, u_v])

完整代码

import cv2
import numpy as np
def nothing(x):
    pass
cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)
while True:
    # 读取的为BGR颜色空间
    frame = cv2.imread('smarties.png')
    # 将图片颜色空间从BGR转换为HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")
    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")
    l_b = np.array([l_h, l_s, l_v])
    u_b = np.array([u_h, u_s, u_v])
    mask = cv2.inRange(hsv, l_b, u_b)
    res = cv2.bitwise_and(frame, frame, mask=mask)
    cv2.imshow("frame", frame)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    key = cv2.waitKey(1)
    if key == 27:
        break
cv2.destroyAllWindows()

效果

进阶:使用下面的代码可以使用摄像头实时监测

import cv2
import numpy as np
def nothing(x):
    pass
cap = cv2.VideoCapture(1)
cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)
while True:
    _,frame=cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")
    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")
    l_b = np.array([l_h, l_s, l_v])
    u_b = np.array([u_h, u_s, u_v])
    mask = cv2.inRange(hsv, l_b, u_b)
    res = cv2.bitwise_and(frame, frame, mask=mask)
    cv2.imshow("frame", frame)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    key = cv2.waitKey(1)
    if key == 27:
        break
cap.release()
cv2.destroyAllWindows()

简单应用:拿着物体运动,并给物体带上面具

import cv2
import numpy as np
from numba import jit
def nothing(x):
    pass
cap = cv2.VideoCapture(1)
cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)
img_mask=cv2.imread('mask.png')
apply=False
draw=True
@jit
def covert(res,img_mask):
    pos=[0,0]
    height=res.shape[0]
    width=res.shape[1]
    h=img_mask.shape[0]
    w=img_mask.shape[1]
    for i in range(height):
        for j in range(width):
            if res[i, j].all() > 0:
                pos = [i, j]
            res[i, j] = 255
    if pos[0]!=0 and pos[1]!=0:
        for i in range(h):
            for j in range(w):
                if pos[0] - int(h / 2) + i < height and pos[1] - int(w / 2) + j < width:
                    res[pos[0] - int(h / 2) + i, pos[1] - int(w / 2) + j] = img_mask[i, j]
while True:
    # 读取的为BGR颜色空间
    #frame = cv2.imread('smarties.png')
    _,frame=cap.read()
    # 将图片颜色空间从BGR转换为HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")
    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")
    l_b = np.array([l_h, l_s, l_v])
    u_b = np.array([u_h, u_s, u_v])
    mask = cv2.inRange(hsv, l_b, u_b)
    res = cv2.bitwise_and(frame, frame, mask=mask)
    if apply:
        covert(res,img_mask)
    cv2.imshow("frame", frame)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    key = cv2.waitKey(1)
    if key == 27:
        break
    if key== 32:
        apply=not apply
cap.release()
cv2.destroyAllWindows()

效果

最后

以上就是悦耳蜗牛为你收集整理的(Opencv)在HSV颜色空间下做目标检测以及目标跟踪的全部内容,希望文章能够帮你解决(Opencv)在HSV颜色空间下做目标检测以及目标跟踪所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部