python
import cv2
img = cv2.imread('view.jpg')
cv2.imshow('view.jpg', img)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR转RGB
cv2.imshow('RGB Color Space', img_rgb)
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) # RGB转BGR
cv2.imshow('BGR Color Space', img_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
image = cv2.cvtColor(src, code)
code:色彩空间转换具体参数,
COLOR_BGR2BGRA:图像从BGR色彩转换为BGRA色彩
COLOR_RGB2RGBA:
COLOR_BGRA2BGR: 图像从BGRA色彩转换为BGR色彩
COLOR_BGR2RGB:图像从BGR色彩转换为RGB色彩
COLOR_RGB2BGR:图像从RGB色彩转换为BGR色彩
COLOR_BGR2GRAY:图像从BGR色彩转换到GRAY色彩
COLOR_RGB2GRAY:图像从RGB色彩转换到GRAY色彩
COLOR_GRAY2BGR:
COLOR_GRAY2RGB:
COLOR_BGR2HSV:图像从BGR色彩转换为HSV色彩
COLOR_RGB2HSV:
COLOR_HSV2BGR:
COLOR_HSV2RGB:
python
import cv2
img = cv2.imread('mountain.jpg')
cv2.imshow("BGR ColorSpace", img)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow('HSV Color Space', img_hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
python
import cv2
image = cv2.imread('colorbar.jpg')
cv2.imshow('bgr', image)
blue, green, red = cv2.split(image)
cv2.imshow('blue', blue)
cv2.imshow('green', green)
cv2.imshow('red', red)
print(f"B通道影像属性 shape={blue.shape}")
print("打印B通道内容")
print(blue)
cv2.waitKey(0)
cv2.destroyAllWindows()
blue, green, red = cv2.split(bgr_image), 拆分BGR图像对象的色彩通道。
python
import cv2
image = cv2.imread('street.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hue, staturation, value = cv2.split(hsv_image)
hsv_image = cv2.merge([hue, staturation, value])
cv2.imshow('The Image', image)
cv2.imshow('The Merge Image', hsv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
hsv_image = cv2.image([hue, saturation, value])合并H、S、V通道的图像。
python
import cv2
image = cv2.imread('street.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hue, saturation, value = cv2.split(hsv_image)
hue[:,:] = 200 # 修改hue内容
hsv_image =cv2.merge([hue, saturation, value])
new_image = cv2.cvtColor(hsv_image,cv2.COLOR_HSV2BGR)
cv2.imshow('The Image', image)
cv2.imshow("The New Image", new_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
色调Hue调整,也可以使用 hue.fill(200) 执行修改,可以得到相同结果。
python
import cv2
image = cv2.imread('street.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hue,saturation,value = cv2.split(hsv_image)
saturation.fill(255)
hsv_image = cv2.merge([hue, saturation, value])
new_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
cv2.imshow('The Image',image)
cv2.imshow('The New Image', new_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
饱和度Ssturation调整;
python
import cv2
image = cv2.imread('street.jpg')
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
hue, saturation, value = cv2.split(hsv_image)
value.fill(255)
hsv_image = cv2.merge([hue,saturation,value])
new_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
cv2.imshow('image', image)
cv2.imshow('new image', new_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
明度value调整。
python
import cv2
image = cv2.imread('street.jpg')
cv2.imshow('image', image)
bgra_image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
b,g,r,a = cv2.split(bgra_image)
print('列出转换成含A通道图像对象后的alpha值')
print(a)
a[:,:] = 32
a32_image = cv2.merge([b,g,r,a])
cv2.imshow("the a32 image", a32_image)
a.fill(128)
a128_image = cv2.merge([b,g,r,a])
cv2.imshow('the a128 image', a128_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('a32.png', a32_image)
cv2.imwrite('a128.png',a128_image)
opencv在BGR的色彩通道,除了B、G、R通道外,另外增加了A通道(又称alpha通道),代表透明度,A的值时0~255,0表示完全透明,255完全不透明。如果后缀为.png ,就是一个典型的又有A通道的图像。
执行上述代码,原图、a32_image和a128_image彼此没有差异,但查看存储的文件,可以发现差异。