NumPy是使用Python進行科學計算的基礎包。它包含其他內容:
- 一個強大的N維數組對象
- 複雜的(廣播)功能
- 用於集成C / C ++和Fortran代碼的工具
- 有用的線性代數,傅里葉變換和隨機數功能
除了明顯的科學用途外,NumPy還可以用作通用數據的高效多維容器。可以定義任意數據類型。這使NumPy能夠無縫快速地與各種數據庫集成。
Numpy的文檔在www.numpy.org可以詳細閱讀
本節所敲的代碼如下
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): # 一個取反的API,比上面函數的速度快很多,因爲使用了c語言的代碼
dst = cv.bitwise_not(image)
cv.imshow("inverse demo", dst)
def create_image():
""" img = np.zeros([400, 400, 3], np.uint8) # 創建一個三通道的圖(RGB圖)
img[:, :, 0] = np.ones([400, 400])*255 # 把藍色通道的值全賦值255
cv.imshow("new image", img)
# img = np.zeros([400, 400, 1],np.uint8) # 創建一個單通道的圖(灰度圖)
# img[:, :, 0] = np.ones([400, 400])*127
img = np.ones([400, 400, 1],np.uint8)
img = img * 127 #與上面兩句等價
cv.imshow("new image", img)
"""
m1 = np.ones([3, 3], np.int32)
m1.fill(122222.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)
src = cv.imread("D:/demo.png") # blue,green,red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount() # 時鐘
inverse(src)
#access_pixels(src)
#create_image()
t2 = cv.getTickCount() # 時鐘
time = (t2-t1)/cv.getTickFrequency()
print("time :%s ms" % (time*1000))
cv.waitKey(0)
cv.destroyAllWindows()