Opencv learning (a) - display and save images to load

Image loading and display

import cv2 as cv
import numpy as np
'''
cv2.imread()
imread函数有两个参数,第一个参数是图片路径,第二个参数表示读取图片的形式,有三种:

cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数,可以直接写1。
cv2.IMREAD_GRAYSCALE:以灰度模式加载图片,可以直接写0。
cv2.IMREAD_UNCHANGED:包括alpha,可以直接写-1。
而imread函数的第二个参数不写就是默认读原始图片

imshow()函数:
有两个参数,第一个参数为显示的窗口名,第二个为显示的图像
'''
im=cv.imread('C:/Users/ASUS/Desktop/rgb2gray/fruit.png')#读取原始图片
im2=cv.imread('C:/Users/ASUS/Desktop/rgb2gray/fruit.png',cv.IMREAD_COLOR)#读取RGB彩色图片
cv.imshow('color image',im)
cv.imshow('color image2',im2)
cv.waitKey(0)

Operating results
Here Insert Picture Description
can be seen that the two forms are the same

Another: cv2.waitKey () function usage:

waitKey () - within a given time (in ms) wait for a key trigger;
if the user key is not pressed, the connection wait (loop)
common: Set waitKey (0), the program will wait indicates unlimited users of key events
in general imgshow of time, if the waitKey (0), on behalf of press any key to continue

Save image

cv.imwrite('lena2.jpg',im)
cv.imwrite('lina_jpeg_quality_90.jpg',im,[int(cv.IMWRITE_JPEG_QUALITY),90])
cv.imwrite('line_png_compress_2.png',im,[int(cv.IMWRITE_PNG_COMPRESSION),2])

The third parameter function specifies cv2.imwrite keeping quality or compression ratio of the image. For jpeg, which represents the quality of the image, 0-100, the greater the value the better the image quality, the default value is 95. For the value of the compression ratio png, set, 0-9, the greater the value the higher the compression ratio, the smaller the image.

Released six original articles · won praise 11 · views 1039

Guess you like

Origin blog.csdn.net/qq_43650722/article/details/104025121