python填充颜色规则_Python填充任意颜色,不同算法时间差异分析说明

我就废话不多说了,大家还是直接看代码吧!mrC免费资源网

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)

结果mrC免费资源网

方法一(切片赋值)时间: 6.554100275039673mrC免费资源网

方法二(Opencv颜色填充)时间: 3.6737191677093506mrC免费资源网

方法三(矩阵乘法)时间: 74.28376317024231mrC免费资源网

方法四(循环遍历赋值)时间: 3245.07548809051504mrC免费资源网

补充知识:规则多边形颜色填充(Python)mrC免费资源网

以规则八边型为例:

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

mrC免费资源网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值