1. Read the image
Use the function cv2.imread(filepath, flags) to read an image
filepath: the full path of the image to be read in
flags: read the flags of the image
cv2.IMREAD_COLOR: Default parameters, read a color picture, ignore the alpha channel
cv2.IMREAD_GRAYSCALE: Read in grayscale images
cv2.IMREAD_UNCHANGED: As the name suggests, read in the complete image, including alpha channel
import cv2
img = cv2.imread('1.jpg',cv2.IMREAD_GRAYSCALE)
2. Display image
Use the function cv2.imshow(wname,img) to display the image. The first parameter is the name of the window that displays the image. The second parameter is the image to be displayed (the image read by imread). The window size is automatically adjusted to the image size.
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#dv2.destroyWindow(wname)
As the name suggests, cv2.waitKey waits for keyboard input, the unit is milliseconds, that is, it waits for the specified number of milliseconds to see if there is keyboard input. If any key is pressed during the waiting time, the ASCII code of the key is returned, and the program continues to run. If no key is pressed, -1 is returned after timeout. A parameter of 0 means waiting indefinitely. If waitKey is not called, the window will flash away and the displayed image will not be visible.
cv2.destroyAllWindow() destroys all windows
cv2.destroyWindow(wname) destroys the specified window
3. Save the image
Use the function cv2.imwrite(file, img, num) to save an image. The first parameter is the filename to be saved, and the second parameter is the image to be saved. The optional third parameter is for a specific format: for JPEG, it represents the quality of the image, expressed as an integer from 0 to 100, the default is 95; for png, the third parameter represents the compression level, the default is 3
cv2.IMWRITE_JPEG_QUALITY type is long and must be converted to int
cv2.IMWRITE_PNG_COMPRESSION, from 0 to 9. The higher the compression level, the smaller the image.
cv2.imwrite('1.png',img, [int( cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.imwrite('1.png',img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
4. Picture operation
1. Use the function cv2.flip(img, flipcode) to flip the image, and flipcode controls the flip effect.
flipcode = 0: flip along the x-axis
flipcode > 0: flip along the y-axis
flipcode < 0: x and y axes are flipped at the same time
imgflip = cv2.flip(img,1)
2. Copy the image
imgcopy = img.copy()
3. Color space conversion
#彩色图像转为灰度图像
img2 = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#灰度图像转为彩色图像
img3 = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
# cv2.COLOR_X2Y,其中X,Y = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
Example
import cv2
# img=cv2.imread('1.jpg',cv2.IMREAD_COLOR)
img=cv2.imread('1.png',cv2.IMREAD_COLOR) # 打开文件
font = cv2.FONT_HERSHEY_DUPLEX # 设置字体
# 图片对象、文本、像素、字体、字体大小、颜色、字体粗细
imgzi = cv2.putText(img, "zhengwen", (1100, 1164), font, 5.5, (0, 0, 0), 2,)
# cv2.imshow('lena',img)
cv2.imwrite('5.png',img) # 写磁盘
cv2.destroyAllWindows() # 毁掉所有窗口
cv2.destroyWindow(wname) # 销毁指定窗口
5. Opencv shoots video and saves it
import cv2
cam = cv2.VideoCapture(0)
wid = int(cam.get(3))
hei = int(cam.get(4))
size = (wid,hei)
fps = 30
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter()
out.open(r"/home/ucar/ucar_ws/src/video/11.mp4",fourcc, fps, size)
while True:
ret, frame = cam.read()
if not ret:
break
frame = cv2.flip(frame,1)
out.write(frame)
cv2.imshow("frame",frame)
key = cv2.waitKey(100)
if cv2.waitKey(25) & 0xFF == ord('1'): # 当按键 1 按下
break
cam.release()
out.release()
cv2.destroyAllWindows()