图片的属性和数组的操作

import cv2 as cv
import numpy as np

def access_pixels(image):
    print(image.shape)
    width=image.shape[1]#将长宽 通道数 存入一个数组
    height=image.shape[0]
    channels=image.shape[2]
    print("width:%s/n,height:%s/n,channels:%s/n"%(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)
    """
    上面的函数定义是将图片的长宽 通道 显示出来 然后利用迭代 range()函数 将每一个
    参数用迭代函数记录下来 然后赋给pv 这样可以得到一个原图片 然后用 255-pv可以得到
    一个反图像
    """



def create_image():
    # img=np.zeros([400,400,3],np.uint8)#创建指定大小的数组,数组元素以 0 来填充:
    # img[ : ,: ,2]=np.ones([400,400,])*255#创建指定形状的数组,数组元素以 1 来填充:
    # cv.imshow("new image",img)
    img=np.ones([400,400,1],np.uint8)#创建一个单通道图像,只有灰度关系
    #img[ : ,:,0]=np.ones([400,400])*127#
    img=img*127#0是黑色 255是白色
    cv.imshow("new image",img)



src = cv.imread("D:/image flie/1.png")#读取图片 RGB图像
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)#给窗口命名以及全尺寸窗口
cv.imshow("input image",src)#显示图片
time1=cv.getTickCount()#读取CPU始终 算是access_pixels()经历的时间
access_pixels(src)
time2=cv.getTickCount()
time=((time2-time1)/cv.getTickFrequency())
print("time : %s ms"%(time*1000))#微秒*1000就是毫秒 想得到秒乘一万就行
create_image()
cv.waitKey(0)#无限等待
cv.destroyAllWindows()

处理图片实质就是处理一个矩阵,所以学习数组很重要,对数组,数列进行各种操作。

Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。

Python3 list() 函数是对象迭代器,可以把range()返回的可迭代对象转为一个列表,返回的变量类型为列表。

Python2 range() 函数返回的是列表。

>>>range(5)
range(0, 5)
>>> for i in range(5):
...     print(i)
... 
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(0))
[]
>>>

有两个参数或三个参数的情况(第二种构造方法)::

>>>list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(1, 0))
[]
>>>
>>>

numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:

import numpy as np 
x = np.empty([3,2], dtype = int) 
print (x)

输出结果为:

[[ 6917529027641081856  5764616291768666155]
 [ 6917529027641081859 -5764598754299804209]
 [          4497473538      844429428932120]]

numpy.zeros

创建指定大小的数组,数组元素以 0 来填充:
numpy.zeros(shape, dtype = float, order = ‘C’)

import numpy as np
 
# 默认为浮点数
x = np.zeros(5) 
print(x)
 
# 设置类型为整数
y = np.zeros((5,), dtype = np.int) 
print(y)
 
# 自定义类型
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  
print(z)

输出结果为:

[0. 0. 0. 0. 0.]
[0 0 0 0 0]
[[(0, 0) (0, 0)]
 [(0, 0) (0, 0)]]

numpy.ones

创建指定形状的数组,数组元素以 1 来填充:
numpy.ones(shape, dtype = None, order = ‘C’)

import numpy as np
 
# 默认为浮点数
x = np.ones(5) 
print(x)
 
# 自定义类型
x = np.ones([2,2], dtype = int)
print(x)

输出结果为:

[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]
`

``

[里面有phthon各种函数的解释 很实用](https://www.runoob.com/numpy/numpy-array-creation.html)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值