OpenCV+Python——Numpy数组操作

今天是第二课啦——Numpy数组操作

下面是一些关键代码

import cv2 as cv
import numpy as np


def access_pixels(image): # 遍历图像像素
  print(image.shape)
  height = image.shape[0]
  width = image.shape[1]
  channels = image.shape[2] 
  print("Height : %s, Width : %s, Channels : %s" % (height, width, channels))
  for row in range(height): # 开始遍历
    for col in range(width):
      for c in range(channels):
        pv = image[row, col, c] # 也可以写成 pv = image[row][col][c]
        image[row][col][c] = 255 - pv # 这里我的理解是三个通道的每个数值x都进行了运算255-x
  cv.imshow("New Image", image)


def inverse(image): # 完成与上个函数相同的功能
  dst = cv.bitwise_not(image) # 该函数是直接调用opencv库中的API,速度较自己遍历快许多许多,因为API调用的是C代码
  cv.imshow("New image", dst)
  

def create_image(): # 通过Numpy数组创建图像
  img = np.zeros([400, 400, 3], np.uint8) # 创建400*400*3的数值为0的三维数组,相当于一个RGB图像
  img[:, :, 0] = np.ones([400, 400]) * 255 # blue, green, red, 255其实表示程度,0-255程度由深至浅,因为np.ones()相当于创建的矩阵的数值为1,":"默认表示所有,添上数值后可以表示赋值范围
  cv.imshow("New Image1", img)
  i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值