图像处理的基本操作(load, display, save)

图像处理的基本操作(load, display, save)

load

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

cv2.imread(filename[, flags])

display

图像的显示需要借助matplotlib.pyplot.imshow()函数。

# Display an image, i.e. data on a 2D regular raster.
import matplotlib.pyplot as plt
plt.imshow(image)

save

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo , and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.

cv2.imwrite(filename, img[, params])

示例

import cv2
import matplotlib.pyplt as plt

image = cv2.imread("image.jpg") # 加载图片
print("Height: %d pixels" % (image.shape[0])) # 图像高度
print("Width: %d pixels" % (image.shape[1])) # 图像宽度
print("Channels: %d" % (image.shape[2])) # 图像通道数

# Open CV读取图片是BGR模式
# matplotlib显示图片时是按照RGB模式,因此在显示之前需要做转换
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #将BGR模式数据转为RGB模式数据
plt.imshow(image) #显示图像 
plt.axis("off") #删除坐标轴
plt.show()

# 图片保存
cv2.imwrite("new_image.jpg", image)

猜你喜欢

转载自blog.csdn.net/w0801101117/article/details/84260584