import cv2 as cv
import numpy as np
def extract_object_demo():
capture = cv.VideoCapture("D:/opencv/work.mp4")
while(True):
ret, frame = capture.read()
if ret == False:
break
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lower_hsv = np.array([11, 43, 46])
upper_hsv = np.array([25, 255, 255])
mask = cv.inRange(frame, lowerb=lower_hsv, upperb=upper_hsv)
cv.imshow("video", frame)
cv.imshow("mask", mask)
c = cv.waitKey(40)
if c == 27:
break
def color_space_demo(image):
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
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)
print("--------- Hello Python ---------")
src = cv.imread("E:/Users/tu.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
b, g, r = cv.split(src)
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)
src[:, :, 2] = 0
cv.imshow("changed image", src)
src = cv.merge([b, g, r])
cv.imshow("merged image", src)
cv.waitKey(0)
cv.destroyAllWindows()
调用转换函数实现图像色彩空间转换:
RGB、HSV、YUV与Ycrcb之间的互相转换利用cv.cvtColor()以及cv.COLOR_***2***即可
图像颜色信息(H)、饱和度(S)、亮度(V)
H:0-180 S:0-255 V:0-255
opencv读取图片的默认像素排列是BGR
色彩空间转换,利用inrange函数过滤视频中的颜色,实现跟踪某一颜色:
在使用inrange时,需要对hsv的lowerb以及upperb进行设置,应根据下图进行相应设置
在设置时可利用numpy得到数组
b, g, r = cv.split(src)
cv.split可对图片进行通道分离
src = cv.merge([b, g, r])
cv.merge可对通道进行合并
src[:, :, 2] = 0
利用上述命令可直接对通道进行赋值