【OpenCV基础】02. Numpy数组操作

02. Numpy数组操作

1. Numpy数组操作

  • Numpy包介绍 - www.numpy.org
  • 遍历数组中的每个像素点
  • 修改数组中像素点的值
  • data\dtype\size\shape\len
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("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 inverse(image):
    dst = cv.bitwise_not(image)
    cv.imshow("inverse demo", dst)


def create_image():
    '''
    img = np.zeros([400, 400, 3], np.uint8)
    img[:, :, 1] = np.ones([400, 400])*255
    cv.imshow("new image", img)

    img = np.zeros([400, 400, 1], np.uint8)
    img[:, :, 0] = np.ones([400, 400])*127
    cv.imshow("new image", img)
    '''

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

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

    m3 = np.array([[2, 3, 4],[4, 5, 6], [7, 8, 9]], np.int32)
    m3.fill(9)
    print(m3)


print("--------------Hello Python----------------")
src = cv.imread("F:/tool/PyCharm/pic/test1.jpeg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
create_image()
inverse(src)
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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值