OpenCV 图像基础与文件处理全解析
1. OpenCV 图像像素操作
在 OpenCV 中,访问和修改图像像素是基础操作。对于彩色图像,OpenCV 使用 BGR 格式。要访问像素值,需提供像素的行和列坐标。例如,要获取像素 (x=40, y=6) 的值,可以使用以下代码:
# A pixel value can be accessed by row and column coordinates.
# In case of BGR image, it returns an array of (Blue, Green, Red) values.
# Get the value of the pixel (x=40, y=6):
(b, g, r) = img[6, 40]
也可以一次访问一个通道。例如,要获取像素 (x=40, y=6) 的蓝色值,可以使用以下代码:
# We can only access one channel at a time.
# In this case, we will use row, column and the index of the desired
# channel for indexing.
# Get only blue value of the pixel (x=40, y=6):
b = img[6, 40, 0]
像素值也可以用同样的方式修改。例如,要将像素 (x=40, y=6) 设置为红色,可以执行以下操作:
<
超级会员免费看
订阅专栏 解锁全文
1783

被折叠的 条评论
为什么被折叠?



