python + opencv 图像处理(三)—— 简单对像素进行处理

from cv2 import cv2 as cv
import numpy as np 

def access_pixels(image):  #属性读取
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    chanmels = image.shape[2]
    print('width:%s,height:%s,chanmels:%s'%(width,height,chanmels))
    #每个像素点进行处理         # 应该是像素反转
    for row in range(height):
        for col in range(width):
            for c in range(chanmels):
                pv = image[row,col,c]
                image[row,col,c] = 255 - pv
    cv.imshow('pixels_demo',image)

# 利用内置的模块反转像素
def inverse(image):
    dst = cv.bitwise_not(image)
    cv.imshow('inverse demo',dst)

# 创建图
def creat_image():
    '''
    # 多通道
    img = np.zeros([400,400,3],np.uint8)
    img[:,:,0] = np.ones([400,400])*255    # blue green red  #得到的是一张蓝色的图
    cv.imshow('new image',img)  
    '''
    # 单通道

    img = np.ones([400,400,1],np.uint8)     # 全部初始化为1
    img = img * 0               # *0 为黑色  *127为灰色  *255为白色
    cv.imshow('new image',img)  


filepath = "C:\\pictures\\0.jpg"
img = cv.imread(filepath)       # blue green red
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",img)
# t1 和 t2是用来测试时间的
t1 = cv.getTickCount()
# creat_image()
# access_pixels(img)
inverse(img)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print('time:%s ms'%(time*1000))

cv.waitKey(0)
cv.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值