OpenCV 获取并修改图中的像素点

使用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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值