Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
The code to use:
import cv2 as cv
import matplotlib.pyplot as plt
# Read in the image
img = cv.imread(src,0)
# display images
cv.imshow("img", img)
cv.waitKey(0)
Copy the code
cv.imread()
Use imread(‘ Image address ‘, Flag) to display color images directly to grayscale images.
- Flags = -1: Imread reads the image as decoded
- Flags = 0: imread reads the image in a single-channel manner, i.e., the gray image
- Flags = 1: imread reads images in three-channel mode, i.e., color images
cv.cvtColor
useCV. CvtColor (variable name, CV.COLOR_BGR2GRAY)
Gray-scale images were obtained by separating RBG
One interesting thing to add:Opencv-python uses BGR three channels for image processing, while Matlibplot uses RGB.
b, g, r = cv.split(img)
The three channels of the image were separated to obtain three BGR scoresimg_rgb = cv.merge([r, g, b])
: Merge them into RBG
This shows up as shown in the image above, the color is not normal.
But if you display an image with Matplotlib, RGB is the normal color, and CV reads BGR as an abnormal color.