读取、写入、转换
# =============================================================================
# imread(img_path,flag) 读取图片,返回图片对象
# img_path: 图片的路径,即使路径错误也不会报错,但打印返回的图片对象为None
# flag:cv.IMREAD_COLOR,读取彩色图片,图片透明性会被忽略,为默认参数,也可以传入1
# cv.IMREAD_GRAYSCALE,按灰度模式读取图像,也可以传入0
# cv.IMREAD_UNCHANGED,读取图像,包括其alpha通道,也可以传入-1
#
# imshow(window_name,img):显示图片,窗口自适应图片大小
# window_name: 指定窗口的名字
# img:显示的图片对象
# 可以指定多个窗口名称,显示多个图片
#
# waitKey(millseconds) & 0xFF 键盘绑定事件,阻塞监听键盘按键,返回一个数字(不同按键对应的数字不同)
# millseconds: 传入时间毫秒数,在该时间内等待键盘事件;传入0时,会一直等待键盘事件
# 0xFF : 64-bit 的机器需要加上
#
# namedWindow(window_name, flag)
# window_name: 指定窗口的名字
# flag: 默认 cv.WINDOW_AUTOSIZE,根据图片原始尺寸固定窗口尺寸。cv.WINDOW_NORMAL
# 设置为窗口可调节
#
#
# destroyAllWindows() 关闭所有窗口
#
# destroyWindows(window_name)
# window_name: 需要关闭的窗口名字
# =============================================================================
import cv2 as cv
img1 = cv.imread("image1.jpg")
img2 = cv.imread("image1.jpg", 0) # 灰度
img3 = cv.imread("image1.jpg", -1)
def image_show(window_name, img, window_option=False):
if window_option:
cv.namedWindow(window_name, cv.WINDOW_NORMAL) # 设定窗口尺寸可调节
cv.imshow(window_name, img)
while True:
key = cv.waitKey(0) & 0xFF # 监控键盘输入返回值
if key in (255, 27): # 关闭为-1 但经过 “& 0xFF” 后为 255, 27为 Esc
cv.destroyAllWindows()
break
elif key == ord("s"):
cv.imwrite(window_name + ".png", img)
cv.destroyAllWindows()
break
# show image
image_show("img1", img1, True)
image_show("img2", img2, True)
image_show("img3", img3, True)
# =============================================================================
# imwrite(os_path)
# os_path: 路径
# threshold(src, thresh, maxval, type) 固定阈值二值化,非黑即白
# src : 输入图,只能输入单通道图像,通常来说为灰度图
# thresh : 阈值
# maxval : 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
# type : 二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY;
# cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;
# cv2.THRESH_TOZERO_INV
# =============================================================================
# BGR -> GRAY
img_gray = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
ret, img_threshold = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY)
image_show("thre", img_threshold) # 展示
cv.imwrite("thre.jpg", img_threshold) # 保存
# =============================================================================
# Matplotlib is a plotting library for Python which gives you wide variety of
# plotting methods. You will see them in coming articles. Here, you will learn
# how to display image with Matplotlib. You can zoom images, save them, etc, using Matplotlib.
#
# 使用 Matplotlib 可操作 opencv 读取的 image
# matplotlib.pyplot.imshow(img, cmap, interpolation)
# img: img 对象
# cmap: 显示类型
# interpolation: 插值算法,用于选择插值算法,填补图像由于拉伸放大造成的不平滑的
# 像素块,保持一定的图片质量。此处选择的是双三次插值算法,是一种
# 精度很高但速度偏慢的算法
# =============================================================================
from matplotlib import pyplot as plt
plt.imshow(img1, cmap = 'gray', interpolation = 'bicubic') # 灰度显示,双三次插值法(画面类似平滑处理)
plt.xticks([]), plt.yticks([]) # 隐藏 x 轴、y 轴的坐标轴刻度
plt.show()
# cv 读取的 image 是 BGR 类型的, Matplotlib.pyplot 是 RGB 类型的,因此若需要正确打印颜色,需转类型
img_rgb = cv.cvtColor(img1, cv.COLOR_BGR2RGB) # BGR 类型 -> RGB 类型
plt.imshow(img_rgb, cmap = 'gray', interpolation = 'bicubic') # 灰度显示,双三次插值法(画面类似平滑处理)
plt.xticks([]), plt.yticks([]) # 隐藏 x 轴、y 轴的坐标轴刻度
plt.show()
参考资料
opencv 官方文档 Getting Started with Images
网友评论