【OpenCv 4 Python 3.7】Numpy数组操作

39 篇文章 9 订阅
30 篇文章 5 订阅

图片以矩阵的形式存在电脑里,需要用到数组操作来完成对图像的处理

Numpy数组操作

Numpy包介绍-www.numpy.org
 遍历数组中的每个像素点
 修改数组中像素点的值
 data\dtype\size\shape\len
在这里插入图片描述

常用的有两个API: zeros和ones

1 np.ones(size) 可以创建任意维度的数组,各个元素值均为1 
2 np.zeros(size,dtype) 同上,但各个元素值为0.默认元素类型为浮点数

使用示例:


 1    img = np.zeros([256, 256, 3], np.uint8)
 2     #创建长宽为256的图片,三通道(BGR),像素大小为8位无符号整数
 3     img[: , : , 0] = np.ones([256,256])*255
 4     #设置图片的颜色B通道为255,也就是蓝色
 5     cv.imshow("new image", img)
 6 
 7     #单通道的灰度图像
 8     img1 = np.ones([400, 400, 1], np.uint8)
 9     img1 = img1 * 147
10     cv.imshow("new image", img1)

图片填充 image.fill(pixel)

转换维度 image.reshape(size)

使用示例:

1 m1 = np.ones([30, 30], np.uint8) #创建单通道的灰度图形
2 m1.fill(12)
3 print(m1)
4 cv.imshow("m1", m1)
5 #reshape注意size大小匹配 30*30=10*90
6 m2 = m1.reshape([10,90])
7 print(m2)
8 cv.imshow("m2", m2)

对pixel的简单操作:方法一:遍历; 方法二:API

 1 def access_pixels(image):
 2     print(image.shape)
 3     height = image.shape[0]
 4     width = image.shape[1]
 5     channels = image.shape[2]
 6     print("width : %s, height : %s, channels : %s" %(width, height, channels))
 7     for i in range(height):
 8         for j in range(width):
 9             for k in range(channels):
10                 pv = 255 - image[i, j, k]
11                 image[i, j, k] = pv
12     cv.imshow("pixels_demo", image)
13 def inverse(image):
14     #和上一个函数的for循环功能类似
15     dst = cv.bitwise_not(image)
16     cv.imshow("inverse demo", dst)

分别运行两个函数发现使用API比循环遍历快不少。(python运行比较慢,API是C++写的)

计算函数运行时间:

1 t1 = cv.getTickCount()
2 access_pixels(src) #需要测试时间的函数
3 t2 = cv.getTickCount()
4 time = (t2-t1)/cv.getTickFrequency()
5 print("time : %s ms" %(time*1000))

代码实现(部分):

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[: , : , 0] = np.ones([400, 400])*255
    img[:, :, 2] = np.ones([400, 400]) * 255 #乘以255是rgb,乘以127是灰度图像
    cv.imshow("new image", img)

    img = np.ones([400, 400, 1], np.uint8)
    img = img * 0
    cv.imshow("new image", img)
    cv.imwrite("D:/myImage.png", img)
    """

    m1 = np.ones([3, 3], np.float) #3*3 的数组
    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("E:/ji_qi_xue_xi/opencv_kejian/opencv_python_image/aaa.jpg") # blue, green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
create_image()
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency();
print("time : %s ms"%(time*1000))
cv.waitKey(0)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值