前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python填充任意颜色,不同算法时间差异分析说明

Python填充任意颜色,不同算法时间差异分析说明

作者头像
砸漏
发布2020-11-02 09:56:18
9460
发布2020-11-02 09:56:18
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

我就废话不多说了,大家还是直接看代码吧!

代码语言:javascript
复制
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)

代码语言:javascript
复制
以规则八边型为例:

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()

以上这篇Python填充任意颜色,不同算法时间差异分析说明就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档