1、图像的读取,显示与保存
import cv2
i = cv2.imread("E:\image\p1.jpg") # 读取图片
cv2.imshow("123", i) # 显示图片
cv2.waitKey(0) # 窗口停留
cv2.destroyAllWindows()
cv2.imwrite("E:\image\timg.jpg", i) # 保存图片
2、图像的处理
import cv2
# 处理灰度图像的像素
# i = cv2.imread("E:\image\lena256.bmp", cv2.IMREAD_UNCHANGED)
#
# print(i[100, 100]) # 读取像素 73
#
# new_i = i[100, 100] = 255 # 修改像素
# print(new_i) # 255
# 处理RGB图像的像素
i = cv2.imread("E:\image\lenacolor.png", cv2.IMREAD_UNCHANGED)
# print(i[100, 100]) # [ 78 68 178]
# i[100, 100, 0] = 255 # 修改第一个通道
#
# print(i[100, 100]) # [255 68 178]
cv2.imshow("demo",i)
# i[100:150,100:150]=[255,255,255] # 第100-150行,100-150列 都是白色
i[100:150,100:150]=[255,0,0] # 第100-150行,100-150列 都是蓝色
cv2.imshow("demo1",i)
cv2.waitKey(0)
cv2.destroyAllWindows()
3、使用Numpy访问像素
import cv2
import numpy as np
i = cv2.imread("E:\image\lena256.bmp", cv2.IMREAD_UNCHANGED)
print(i.item(100, 100)) # 读取100行100列的像素
i.itemset((100, 100), 255) # 修改
print(i.item(100, 100))
4,获取图像属性
import cv2
i = cv2.imread("E:\image\lena256.bmp", cv2.IMREAD_UNCHANGED)
b = cv2.imread("E:\image\lena512.bmp", cv2.IMREAD_UNCHANGED)
print(i.shape) # 几行几列 (256, 256)
print(b.shape) # (512, 512)
print(i.size) # 大小65536
print(b.size) # 262144
print(i.dtype) # 类型uint8
print(b.dtype) # uint8