欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年05月16日 15:55:26   作者:vselfdom  
這篇文章主要介紹了Python填充任意顏色,不同算法時間差異分析說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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 ("方法四(循環(huán)遍歷賦值)時間:",end-start)
cv2.imwrite("test4.png",canvas)

結(jié)果

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

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

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

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

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

以規(guī)則八邊型為例:

import matplotlib.pyplot as plt
import numpy as np

# 設(shè)置八邊形頂點(diǎn)坐標(biāo)
x = [0, 0, 5, 10, 15, 15, 10, 5]
y = [5, 10, 15, 15, 10, 5, 0, 0]

# 通過調(diào)用 fill() 函數(shù) 完成繪制八邊形
# 參數(shù) x 和 y 是用來繪制封閉區(qū)域頂點(diǎn)的有序坐標(biāo)集
# 參數(shù) color 用來指定封閉區(qū)域的填充顏色
plt.fill(x, y, color="green")

# 為了可視化效果更好,使用函數(shù) xlim() 和 ylim() 完成多邊型在整個坐標(biāo)軸中的相對位置調(diào)整(可自行刪除對比效果)
plt.xlim(-1, 17)
plt.ylim(-1, 17)

# 使用 xticks() 和 yticks() 調(diào)整刻度線的顯示位置
# np.arange(起始坐標(biāo),結(jié)束坐標(biāo),坐標(biāo)間隔)
plt.xticks(np.arange(0, 16, 5))
plt.yticks(np.arange(0, 16, 5))

# 調(diào)用 show() 函數(shù)展示圖形的繪制效果
plt.show()

以上這篇Python填充任意顏色,不同算法時間差異分析說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論