A. Color space conversion
1.cv2.cvtColor(input_img,flag)
Parameter 1 is to convert the image
2 is a conversion parameter types such as: cv2.COLOR_BGR2HSV (RGB-> HSV) cv2.COLOR_BGR2GRAY (RGB-> grayscale), Common
import cv2 import numpy as np img = cv2.imread('timg5.jpg') img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.namedWindow('img',cv2.WINDOW_NORMAL) cv2.namedWindow('Gray',cv2.WINDOW_NORMAL) cv2.namedWindow('hsv',cv2.WINDOW_NORMAL) cv2.imshow("img",img) cv2.imshow('Gray',img1) cv2.imshow('hsv',img2) cv2.waitKey(0)
Renderings:
Original grayscale HSV map
2.cv2.inRange (src, lowerb, upperb, dst = None) binarization
src: input picture, the image may be a single-channel gray, channel 3 may be a color image
lowerb: a pixel value range limit
upperb: pixel values of the upper range
Description: Single gray image, the pixel value is below and above upperb lowerb portion becomes 0, a value between 255 becomes upper_red lower_red ~; three-channel color images, then each channel, upperb lowerb are compared, and then modify the pixel values in the same way
import cv2 import numpy as np img = cv2.imread('timg5.jpg') img2 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.namedWindow('hsv',cv2.WINDOW_NORMAL) cv2.namedWindow('hsv1',cv2.WINDOW_NORMAL) cv2.namedWindow('hsv2',cv2.WINDOW_NORMAL) lower_blue = np.array([90, 50, 50]) upper_blue = np.array([130, 255, 255]) mask = cv2.inRange(img2, lower_blue, upper_blue)#二值化 res = cv2.bitwise_and(img2, img2, mask=mask) #按位与 cv2.imshow('hsv',img2) cv2.imshow('hsv1',mask) cv2.imshow('hsv2',res) cv2.waitKey(0)
3.cv2.bitwise_and(src1, src2, dst=None, mask=None)
When you call the absence of mask parameters src1 & src2 is returned, if the mask parameter is present, src1 & src2 & mask returns
src1: input picture. 1
src2: input picture 2, src1 and src2 can be the same or different, may be a gray scale image may be a color image
dst: If there is a parameter: src1 & src2 & SRC2 & mask or srcl
mask: 8bit gray scale image may be a single channel may be a matrix, typically binarized image