歡迎您光臨本站 註冊首頁

Python填充任意顏色,不同算法時間差異分析說明

←手機掃碼閱讀     niceskyabc @ 2020-06-01 , reply:0

我就廢話不多說了,大家還是直接看代碼吧!

 import time import numpy as np import cv2 #方法一 start = time.time() for i in range(1000): canvas = np.zeros((1080,1920,3), np.uint8) canvas[:,:,0] = 113 canvas[:,:,1] = 207 canvas[:,:,2] = 250 end = time.time() print ("方法一(切片賦值)時間:",end-start) cv2.imwrite("test1.png",canvas) #方法二 start = time.time() for i in range(1000): canvas = np.zeros((1080,1920,3), np.uint8) cv2.rectangle(canvas, (0, 0), (1920, 1080), (113,207,250), thickness=-1) end = time.time() print ("方法二(Opencv顏色填充)時間:",end-start) cv2.imwrite("test2.png",canvas) #方法三 start = time.time() for i in range(1000): canvas = np.ones([1080,1920,3])*[113,207,250] end = time.time() print ("方法三(矩陣乘法)時間:",end-start) cv2.imwrite("test3.png",canvas) # #方法四 start = time.time() for i in range(1000): canvas = np.zeros((1080,1920,3), np.uint8) for i in range(1080): for j in range(1920): canvas[i][j] = [113,207,250] end = time.time() print ("方法四(循環遍歷賦值)時間:",end-start) cv2.imwrite("test4.png",canvas)


結果

方法一(切片賦值)時間: 6.554100275039673

方法二(Opencv顏色填充)時間: 3.6737191677093506

方法三(矩陣乘法)時間: 74.28376317024231

方法四(循環遍歷賦值)時間: 3245.07548809051504

補充知識:規則多邊形顏色填充(Python)

 以規則八邊型為例: import matplotlib.pyplot as plt import numpy as np # 設置八邊形頂點座標 x = [0, 0, 5, 10, 15, 15, 10, 5] y = [5, 10, 15, 15, 10, 5, 0, 0] # 通過調用 fill() 函數 完成繪製八邊形 # 參數 x 和 y 是用來繪製封閉區域頂點的有序座標集 # 參數 color 用來指定封閉區域的填充顏色 plt.fill(x, y, color="green") # 為了可視化效果更好,使用函數 xlim() 和 ylim() 完成多邊型在整個座標軸中的相對位置調整(可自行刪除對比效果) plt.xlim(-1, 17) plt.ylim(-1, 17) # 使用 xticks() 和 yticks() 調整刻度線的顯示位置 # np.arange(起始座標,結束座標,座標間隔) plt.xticks(np.arange(0, 16, 5)) plt.yticks(np.arange(0, 16, 5)) # 調用 show() 函數展示圖形的繪製效果 plt.show()



[niceskyabc ] Python填充任意顏色,不同算法時間差異分析說明已經有233次圍觀

http://coctec.com/docs/python/shhow-post-236534.html