Getting Started with Images

摘自https://docs.opencv.org/4.2.0/dc/d2e/tutorial_py_image_display.html

Read an image

Use the function cv.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

import numpy as np
import cv2 as cv
# Load a color image in grayscale
img = cv.imread('messi5.jpg', cv.IMREAD_GRAYSCALE)

warning: Even if the image path is wrong, it won't throw any error, but print img will give you None

Display an image

Use the function cv.imshow() to display an image in a window. The window automatically fits to the image size.

First argument is a window name which is a string. Second argument is our image. You can create as many windows as you wish, but with different window names.

cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

cv.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes.

Besides binding keyboard events this function also processes many other GUI events, so you MUST use it to actually display the image.

cv.destroyAllWindows() simply destroys all the windows we created. If you want to destroy any specific window, use the function cv.destroyWindow() where you pass the exact window name as the argument.

There is a special case where you can create an empty window and load an image to it later. It is done with the function cv.namedWindow(). By default, the flag is cv.WINDOW_AUTOSIZE. But if you specify the flag to be cv.WINDOW_NORMAL, you can resize window. It will be helpful when an image is too large in dimension and when adding track bars to windows.

cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

Write an image

Use the function cv.imwrite() to save an image.

First argument is the file name, second argument is the image you want to save.

cv.imwrite('messigray.png',img)

This will save the image in PNG format in the working directory.

import numpy as np
import cv2 as cv
img = cv.imread('messi5.jpg',0)
cv.imshow('image',img)
k = cv.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv.imwrite('messigray.png',img)
    cv.destroyAllWindows()

warning: If you are using a 64-bit machine, you will have to modify k = cv.waitKey(0) line as follows : k = cv.waitKey(0) & 0xFF

Using Matplotlib

plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()

Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. 

img[..., ::-1] # same as the mentioned img[:, :, ::-1] but slightly shorter

#or
cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

猜你喜欢

转载自blog.csdn.net/Airfrozen/article/details/104422489