python - 像素点变换的性能
问题描述
所以我使用 numpy 和 OpenCV 来处理一些视频。我遇到的问题是使用查找表对像素值进行点转换:
def sig(x,factor):
if x == 255:
return 255
elif x!=0:
return int(255*(1- 1/(1+(255/x -1)**(-factor))))
else:
return 0
def Constrast(img):
global LUT
for x in range(len(img)):
for y in range(len(img[0])):
img[x,y] = LUT[img[x,y]]
return img
def Enhance(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Some other functions here
img = Constrast(img) # This one thing takes 0.3 seconds
#Do more stuff here
factor = .8
LUT = []
for i in range(256):
LUT.append(sig(i,factor))
Enhance(img) # Enhancing a greyscale image
我使用全局变量的原因是因为我的对比函数嵌套在其他函数中。我已经测试了使用LUT
作为这些函数的参数,结果它变慢了。我使用了 numpy 数组、字典、数组模块中的数组和列表,后者是最快的。到目前为止,我在每张图像上管理 0.3 秒。有没有一种明显的方法可以更快地循环像素?
提前致谢。
解决方案
推荐阅读
- python - Python:如何处理文件夹中的多种不同类型的文件?
- php - 我无法连接 MySQL Web 服务器
- grails - 从 Grails 2.2.5 升级到 4.0.11 后出现令人费解的验证错误
- blazor - 在 blazor wasm 中,如何在不更改当前 url 的情况下使用 a 标签下载文件
- node.js - 您需要在 Node/Express 中向 Imgur 发送哪些数据?
- java - java - 如何找到存储在字符串数组中的整数的总和以及java中的字符串?我需要输出中的 0 是整数的总和
- python - 需要帮助在 python 中运行 .execute 方法(SQLite3 库)
- python - 如何通过避免在python中嵌套来简化下面的代码?
- javascript - ts-loader 不工作(您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件)
- visual-studio - Salesforce 使用 Salesforce 数据单元的 VS 代码错误