使用for循环遍历图中的所有像素点
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
image = np.zeros((256, 256, 3), np.uint8) # 创建一个 长256 宽 256的图
plt.imshow(image[:, :, ::-1])
h, w, c = image.shape
for row in range(h): # 对图中所有的像素点进行遍历
for col in range(w):
b, g, r = image[row, col]
print(b, g, r)
plt.show()
通过行和列的坐标值获取该像素点的像素值,对于BGR图像,它返回一个蓝,绿,红值的数组。对于灰度图像,仅返回相应的强度值。
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((256, 256, 3), np.uint8) # 创建一个 长256 宽 256的图
plt.imshow(img[:, :, ::-1])
# 获取某个点的值
print(img[100, 100])
# 仅获取蓝色通道的强度值
print(img[100, 100, 0])
# 修改某个位置的像素值
img[100, 200] = (0, 0, 255) # 在y轴100,x轴200的位置 将像素点变为红色
img[100, 100] = [255, 255, 255] # 在y轴100,x轴100的位置 将像素点变为白色
plt.imshow(img[:, :, ::-1])
plt.show()
print(img[100, 200]) # 获取某个点的值的内容
print(img[100, 200].dtype) # 8位无符号整型
print(img.shape) # 获取到图像的行数,列数
print(img.dtype) # 查看图像类型
print(img.size) # 查看像素数
获取图像的属性
图像属性包括行数,列数和通道数,图形数据类型,像素数等。
属性 | APT |
---|---|
形状 | img.shape |
图像大小 | img.dtype |
数据类型 | img.size |