小白学python(opencv(numpy部分数组操作))

小白学python(opencv(numpy部分数组操作))

介绍

NumPy是Python中科学计算的基础包。它是一个Python库,提供多维数组对象,各种派生对象(如掩码数组和矩阵),以及用于数组快速操作的各种API,有包括数学、逻辑、形状操作、排序、选择、输入输出、离散傅立叶变换、基本线性代数,基本统计运算和随机模拟等等。
(百度百科啦)

使用

import numpy as np

如果我们希望两个列表对应项相加,则我们需要这样做,使用Python列表这样的代码是冗余的,而使用numpy则大大减少了代码的冗余。
eg:

#使用Python列表
a = [1,2,3,4]
b = [4,5,6,7,8]
out = []
for i,j in zip(a,b):
    out.append(i+j)
print(out)
#output
[ 5,  7,  9, 11]

#使用numpy
import numpy as np
a = np.array([1,2,3,4])
b = np.array([4,5,6,7])
print(a+b)
#output
array([ 5,  7,  9, 11])

这里内容太多,我就用啥学啥了,有机会再更numpy

numpy.ones()函数:

ones(shape, dtype=None, order='C')

shape是一个int或一个int元组,用于定义数组的大小。 如果我们仅指定一个int变量,则将返回一维数组。 对于一个整数元组,将返回给定形状的数组。
dtype是一个可选参数,默认值为float。 它用于指定数组的数据类型,例如int。
order该顺序定义是在内存中以行优先(C风格)还是列优先(Fortran风格)顺序存储多维数组。
eg:

import numpy as np
 
array_1d = np.ones(3)
print(array_1d)
#output为[1. 1. 1.]

array_2d = np.ones((2, 3))
print(array_2d)
#output为
[[1. 1. 1.]
 [1. 1. 1.]]
 
array_2d_int = np.ones((2, 3), dtype=int)
print(array_2d_int)
#output为
[[1 1 1]
 [1 1 1]]
import numpy as np
 
array_mix_type = np.ones((2, 2), dtype=[('x', 'int'), ('y', 'float')])
print(array_mix_type)
#output为
[[(1, 1.) (1, 1.)]
 [(1, 1.) (1, 1.)]]

zeros():

在给数组赋初始值的时候,Python中,我们使用zero()函数来实现。
ones函数可以创建任意维度和元素个数的数组,其元素值均为1;

求反色图片

这里先写一个小函数求图片的反色图片:

def access_pixels(image):
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2] #blue , green , red
    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)

创建图片

用numpy创建图片

ef create_iamge():
    img = np.zeros([400,400,4],np.uint8) #创建黑色图片
    #img[ :, :, 0] = np.ones([400,400])*255  #三通道为0,1,2
    #可以用此句将图片变为蓝色
    cv.imshow("new",img)

unit8为dtype(格式)
在这里插入图片描述
目前函数主体为:

import cv2  as cv
import  numpy as np
def main():
    src = cv.imread("C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg")
    #cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
    # cv.imshow("input image",src)
    #access_pixels(src)  #h获取反色图片
    #video_demo()
    #create_iamge()
    cv.waitKey(0)
    cv.destroyAllWindows()
    # get_image_info(src)
    #cv.imwrite("D:/result.png",src)


def get_image_info(image):#获取图像信息
    print(type(image))
    print(image.shape)
    print(image.size)
    print(image.dtype)
    pixel_data = np.array(image)
    print(pixel_data)


def video_demo():#调用摄像头
    capture = cv.VideoCapture(0)
    while(True):
        ret,frame = capture.read()
        frame=cv.flip(frame,1)
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27):    #esc为27
            break

def access_pixels(image):#获得反色图片
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2] #blue , green , red
    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_iamge():#创建图片
    img = np.zeros([400,400,3],np.uint8) #创建图片
    img[ :, :, 0] = np.ones([400,400])*255  #三通道为012
    cv.imshow("new",img)
if __name__ =="__main__":
    main()

能力有限,我还得慢慢学,先记到这里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mcl19909949541

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

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

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

打赏作者

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

抵扣说明:

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

余额充值