python-opencv--图像像素通道读取及修改

本文介绍如何使用Python和OpenCV库进行图像像素的读取和修改。通过示例展示了如何访问图像的像素,对图像进行像素取反操作,并创建多通道图像。最后,还演示了读取图像和计算操作时间的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

data/dtype/size/shape/len
‘’’

import cv2 as cv
import numpy as np

def access_pixes(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print(“width : %s, height : %s, channels : %s”%(width, height, channels))
for row in range(height):
for col in range(width):
for c in range(channels):
pv = image[row, col, c]
image[row, col, c] = 255-pv #像素取反
cv.imshow(“pixels_demo”, image)

def create_image():
#多通道修改
‘’’
img = np.zeros([512, 512, 3], np.uint8) #定义一个三维都是0的矩阵
img[:, :, 0] = np.ones([512, 512])*255 #修改第一个通道(blue通道)
cv.imshow(“new image”, img)

#单通道修改
img = np.ones([512, 512, 1], np.uint8)  #定义一个一维的单通道矩阵
img = img*127
cv.imshow("new image", img)
'''

m1 = np.ones([3, 3], np.uint8)
m1.fill(122.83)
print(m1)

m2 = m1.reshape([1, 9])
print(m2)

def inverse(image):
dst = cv.bitwise_not(image) #像素取反
cv.imshow(“i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值