原视频:https://www.bilibili.com/video/BV1L441147AX

我们有一张包含有不同种颜色球的图片,使用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()

效果

Last modification:September 29th, 2020 at 02:35 pm