1.改变颜色空间
主要使用cvtColor(input_image, flag)来实现,有两个参数: ①输入图片 ②转换类型,主要有以下几种:
类型 | 函数 |
---|
RGB和BGR(opencv默认的彩色图像的颜色空间是BGR)颜色空间的转换 | cv.COLOR_BGR2RGB cv.COLOR_RGB2BGR cv.COLOR_RGBA2BGRA cv.COLOR_BGRA2RGBA | 向RGB和BGR图像中增添alpha通道 | cv.COLOR_RGB2RGBA cv.COLOR_BGR2BGRA | 从RGB和BGR图像中去除alpha通道 | cv.COLOR_RGBA2RGB cv.COLOR_BGRA2BGR | 从RBG和BGR颜色空间转换到灰度空间 | cv.COLOR_RGB2GRA cv.COLOR_BGR2GRAY cv.COLOR_RGBA2GRAY cv.COLOR_BGRA2GRAY | 从灰度空间转换到RGB和BGR颜色空间 | cv.COLOR_GRAY2RGB cv.COLOR_GRAY2BGR cv.COLOR_GRAY2RGBA cv.COLOR_GRAY2BGRA | RGB和BGR颜色空间与BGR565颜色空间之间的转换 | cv.COLOR_RGB2BGR565 cv.COLOR_BGR2BGR565 cv.COLOR_BGR5652RGB cv.COLOR_BGR5652BGR cv.COLOR_RGBA2BGR565 cv.COLOR_BGRA2BGR565 cv.COLOR_BGR5652RGBA cv.COLOR_BGR5652BGRA | 灰度空间域BGR565之间的转换 | cv.COLOR_GRAY2BGR555 cv.COLOR_BGR5552GRAY | RGB和BGR颜色空间与CIE XYZ之间的转换 | cv.COLOR_RGB2XYZ cv.COLOR_BGR2XYZ cv.COLOR_XYZ2RGB cv.COLOR_XYZ2BGR | RGB和BGR颜色空间与uma色度(YCrCb空间)之间的转换 | cv.COLOR_RGB2YCrCb cv.COLOR_BGR2YCrCb cv.COLOR_YCrCb2RGB cv.COLOR_YCrCb2BGR | RGB和BGR颜色空间与HSV颜色空间之间的相互转换 | cv.COLOR_RGB2HSV cv.COLOR_BGR2HSV cv.COLOR_HSV2RGB cv.COLOR_HSV2BGR | RGB和BGR颜色空间与HLS颜色空间之间的相互转换 | cv.COLOR_RGB2HLS cv.COLOR_BGR2HLS cv.COLOR_HLS2RGB cv.COLOR_HLS2BGR | RGB和BGR颜色空间与CIE Lab颜色空间之间的相互转换 | cv.COLOR_RGB2Lab cv.COLOR_BGR2Lab cv.COLOR_Lab2RGB cv.COLOR_Lab2BGR | RGB和BGR颜色空间与CIE Luv颜色空间之间的相互转换 | cv.COLOR_RGB2Luv cv.COLOR_BGR2Luv cv.COLOR_Luv2RGB cv.COLOR_Luv2BGR | Bayer格式(raw data)向RGB或BGR颜色空间的转换 | cv.COLOR_BayerBG2RGB cv.COLOR_BayerGB2RGB cv.COLOR_BayerRG2RGB cv.COLOR_BayerGR2RGB cv.COLOR_BayerBG2BGR cv.COLOR_BayerGB2BGR cv.COLOR_BayerRG2BGR cv.COLOR_BayerGR2BGR |
代码示例:
import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac.jpg')
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
cv.imshow("prac", img)
cv.waitKey(0)
cv.destroyAllWindows()
这个函数常与inRange(hsv, lower_red, upper_red)函数搭配使用,此函数用来设阈值,来去除背景部分,有三个参数:
①hsv指的是原图 ②lower_red指的是图像中低于这个lower_red的值,图像值变为0 ③upper_red指的是图像中高于这个upper_red的值,图像值变为0
代码示例:
lower_red = np.array([20, 20, 20])
upper_red = np.array([200, 200, 200])
mask = cv.inRange(img, lower_red, upper_red)
2.对象追踪
通过取视频的每一帧,转换从BGR到HSV颜色空间,然后对HSV图像设置想要的颜色范围阈值,最后就可以单独提取想要的对象。
提取蓝色代码示例:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
while(1):
_, frame = cap.read()
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
mask = cv.inRange(hsv, lower_blue, upper_blue)
res = cv.bitwise_and(frame,frame, mask= mask)
cv.imshow('frame',frame)
cv.imshow('mask',mask)
cv.imshow('res',res)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cap.release()
cv.destroyAllWindows()
提取红、绿、蓝三色代码示例:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
while (1):
_, frame = cap.read()
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
lower_red = np.array([0, 43, 46])
upper_red = np.array([10, 255, 255])
lower_green = np.array([35, 43, 46])
upper_green = np.array([77, 255, 255])
mask_blue = cv.inRange(hsv, lower_blue, upper_blue)
mask_red = cv.inRange(hsv, lower_red, upper_red)
mask_green = cv.inRange(hsv, lower_green, upper_green)
mask = cv.add(mask_blue, mask_red)
mask = cv.add(mask, mask_green)
res = cv.bitwise_and(frame, frame, mask=mask)
cv.imshow('frame', frame)
cv.imshow('mask', mask)
cv.imshow('res', res)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cap.release()
cv.destroyAllWindows()
3.追踪HSV值
使用相同的函数cv.cvtColor()。只需传递你想要的BGR值,而不是传递图像。代码示例:
import numpy as np
import cv2 as cv
green = np.uint8([[[0,255,0 ]]])
hsv_green = cv.cvtColor(green,cv.COLOR_BGR2HSV)
print( hsv_green )
|