OpenCV Python: 遍历图像特定行像素值 - 获取色素信息
在 OpenCV 中,可以使用 'cv2.split()' 函数将图像分割为各个通道,然后使用 'numpy' 库的切片功能来获取某一行的像素值。
以下是一个示例代码,演示了如何遍历某一行的像素值:
import cv2
import numpy as np
# 读取图像
image = cv2.imread('image.jpg')
# 分割图像为各个通道
b, g, r = cv2.split(image)
# 获取图像的高度和宽度
height, width = image.shape[:2]
# 定义要遍历的行数
row_index = 100
# 遍历每个通道的像素值
for col_index in range(width):
# 获取每个通道的像素值
blue = b[row_index, col_index]
green = g[row_index, col_index]
red = r[row_index, col_index]
# 输出像素值
print('Pixel at (row={}, col={}): Blue={}, Green={}, Red={}'.format(row_index, col_index, blue, green, red))
在上述代码中,我们首先使用 'cv2.split()' 函数将图像分割为各个通道(蓝色通道、绿色通道和红色通道)。然后,我们使用 'numpy' 库的切片功能来获取指定行和列的像素值。在这个例子中,我们遍历了第 100 行的所有像素,并输出了每个通道的像素值。
请注意,行和列的索引都是从 0 开始的。
原文地址: https://www.cveoy.top/t/topic/fKdH 著作权归作者所有。请勿转载和采集!