I. Introduction to Functions 1, split-extract R, B, g components (return value order: B, G, r) function prototype: Split (M, Mv=none) m: Color matrix MV: Default parameter 2, merge-merge R, G, B (parameter order: B, G, r) function prototype: merge ( MV, Dst=none) M:b, G, r component mv: Default Parameter 3, cvtcolor-merge R, G, B (Parameter order: B, G, R) function prototype: Cvtcolor (SRC, code, dst=none, Dstcn=none) SRC: Image Matrix code: Conversion Parameters Other: Default parameter two, example walkthrough 1, extract Lena image of R, G, B components
The code is as follows:
#encoding: Utf-8#extraction of #彩图R, G and b#Import NumPy as Npimport cv2image = Cv2. Imread("H:\\img\\lena.jpg") Cv2. Imshow("Original", image) Cv2. Waitkey(0)extraction of #R, G and B components(b,g,r) = Cv2. Split(image)#提取R, G, b componentsCv2. Imshow("Red", R) Cv2. Imshow("Green", G) Cv2. Imshow("Blue", B) Cv2. Waitkey(0)
The results are as follows
1. Original image
2. Composition of R
3, G components
4, B components
2, merging Lena image of R, G, B components
The code is as follows:
#encoding: Utf-8#extraction and merger of #彩图R, G and b#Import NumPy as Npimport cv2image = Cv2. Imread("H:\\img\\lena.jpg") Cv2. Imshow("Original", image) Cv2. Waitkey(0)extraction of #R, G and B components(b,g,r) = Cv2. Split(image)#提取R, G, b componentsmerger of #R, G, bmerged = Cv2. Merge([B,g,r])#合并R, G, b componentsCv2. Imshow("merged", merged) Cv2. Waitkey(0)
The results are as follows
merge Lena images with R, G, B (in fact, as in the original image, here just to demonstrate the use of the function)
3. Convert color Lena image to gray Lena image
The code is as follows:
#encoding:utf-8import numpy as npimport cv2image = cv2.imread("H:\\img\\lena.jpg")cv2.imshow("Original",image)cv2.waitKey(0)#彩色转灰色gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)cv2.imshow("Gray",gray)cv2.waitKey(0)
The results are as follows
1. Original image
2. Grayscale Image
4. RGB to HSV
The code is as follows:
#encoding:utf-8import numpy as npimport cv2image = cv2.imread("H:\\img\\lena.jpg")cv2.imshow("Original",image)cv2.waitKey(0)#HSV空间hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)cv2.imshow("HSV",hsv)cv2.waitKey(0)
The results are as follows
1. Original image
2. HSV Space
5. RGB Turn Lab
The code is as follows:
#encoding:utf-8import numpy as npimport cv2image = cv2.imread("H:\\img\\lena.jpg")cv2.imshow("Original",image)cv2.waitKey(0)#lab空间lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)cv2.imshow("L*a*b*", lab)cv2.waitKey(0)
The results are as follows
1. Original image
2. Lab Space
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Opencv-python (7)--extraction and merging of color images R, G, B components and their related color space conversion