OpenCV征程-----Numpy数组

Nunmpy数组包含:

  • 强大的N维数组对象

  • 复杂的(广播)功能

  • 集成C / C ++和Fortran代码的工具

  • 有用的线性代数,傅立叶变换和随机数功能

遍历与修改数组中的所有像素点

 1 #对所有像素进行循环
 2 def access_pixels(image):
 3     print(image.shape)
 4     height = image.shape[0]     #高度
 5     width = image.shape[1]      #宽度
 6     channels = image.shape[2]   #通道数
 7     print("width : %s, height : %s, channels : %s"%(width, height, channels))
 8     for row in range(height):               #循环获取每一个像素点
 9         for col in range(width):
10             for c in range(channels):
11                 pv = image[row, col, c]         #维度
12                 image[row, col, c] = 255 - pv
13     cv.imshow("pixels_demo",image)

创建新图像

创建新图像:

1 np.zeros([400, 400, 3], np.uint8)        #形状、类型

代码:

 1 #通过Numpy对一个数组指定维数赋值
 2 def create_image():
 3     '''
 4     #(创建一个新三通道的图像)
 5     #多通道常见为RGB图像
 6     # img = np.zeros([400, 400, 3], np.uint8)
 7     # img[: ,: ,0] = np.ones([400, 400])*255      #0表示第一通道Blue;1表示第二通道Green;3表示第三通道Read
 8 
 9     #单通道常见为灰度图像
10     # img = np.zeros([400, 400, 1], np.uint8)       #创建新的图像
11     # img[:, :, 0] = np.ones([400, 400]) * 127
12     img = np.ones([400, 400, 1], np.uint8)
13     img =img*127
14     cv.imshow("new image", img)     #窗口显示
15     cv.imwrite("C:/Users/shinelon/Desktop/DL/001.png")   #将图像保存
16     '''
17 
18     #初始化二维,打印像素点
19     m1 = np.ones([3, 3], np.float32)
20     m1.fill(222.388)
21     print(m1)
22 
23     m2 = m1.reshape([1,9])      #改变其在空间的形状
24     print(m2)

 其他知识点

获取当前CPU时钟:

1 t1 = cv.getTickCount()                  #获取当前CPU时间

 完整代码

 1 import cv2 as cv
 2 import numpy as np
 3 
 4 
 5 #对所有像素进行循环;解释执行速度较慢
 6 def access_pixels(image):
 7     print(image.shape)
 8     height = image.shape[0]     #高度
 9     width = image.shape[1]      #宽度
10     channels = image.shape[2]   #通道数
11     print("width : %s, height : %s, channels : %s"%(width, height, channels))
12     for row in range(height):               #循环获取每一个像素点
13         for col in range(width):
14             for c in range(channels):
15                 pv = image[row, col, c]         #维度
16                 image[row, col, c] = 255 - pv
17     cv.imshow("pixels_demo",image)
18 
19 
20 def inverse(image):
21     dest = cv.bitwise_not(image)    #像素取反,依靠C的代码
22     cv.imshow("inverse",dest)
23 
24 
25 #通过Numpy对一个数组指定维数赋值
26 def create_image():
27     '''
28     #(创建一个新三通道的图像)
29     #多通道常见为RGB图像
30     # img = np.zeros([400, 400, 3], np.uint8)
31     # img[: ,: ,0] = np.ones([400, 400])*255      #0表示第一通道Blue;1表示第二通道Green;3表示第三通道Read
32 
33     #单通道常见为灰度图像
34     # img = np.zeros([400, 400, 1], np.uint8)       #创建新的图像
35     # img[:, :, 0] = np.ones([400, 400]) * 127
36     img = np.ones([400, 400, 1], np.uint8)
37     img =img*127
38     cv.imshow("new image", img)     #窗口显示
39     cv.imwrite("C:/Users/shinelon/Desktop/DL/001.png")   #将图像保存
40     '''
41 
42     #初始化二维,打印像素点
43     m1 = np.ones([3, 3], np.float32)
44     m1.fill(222.388)
45     print(m1)
46 
47     m2 = m1.reshape([1,9])      #改变其在空间的形状
48     print(m2)
49 
50     m3 = np.array([[2, 3, 4], [4, 5, 6], [7, 8, 9]], np.int32)      #卷积神经需要
51     #m3.fill(9)
52     print(m3)
53 
54 
55 print("------Python OpenCV Tutorial-----")
56 src = cv.imread("C:/Users/shinelon/Desktop/DL/12.png")      #括号类为图片的绝对路径
57 cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
58 cv.imshow("input image",src)            #将图片在Windows窗口显示
59 t1 = cv.getTickCount()                  #获取当前CPU时间
60 create_image()
61 #access_pixels(src)      #时间比较长
62 inverse(src)            #优化,时间较短
63 t2 = cv.getTickCount()
64 time = (t2 - t1)/cv.getTickFrequency()      #运行的时间ms
65 print("time : %s ms"%(time*1000))
66 cv.waitKey(0)
67 
68 
69 cv.destroyAllWindows()

猜你喜欢

转载自www.cnblogs.com/gghy/p/11761949.html