【问题标题】:Fast image normalisation in python [closed]python中的快速图像标准化[关闭]
【发布时间】:2019-07-07 01:39:54
【问题描述】:

我正在寻找一种更快的方法来规范化 Python 中的图像。我想将所有像素转换为 0 到 1 之间的值。

输入:JPEG 格式的 150x150 RGB 图像。

操作系统/硬件:8GB RAM 的 LINUX/P40 GPU

用例:实时分类任务的图像预处理。

每张图片的当前时间约为 5-10 毫秒。我正在寻找一种可以减少这个时间的方法。

我尝试了两种方法,使用 numpy 和 opencv。

使用 numpy(大约时间:8ms):

norm = (img - np.min(img)) / (np.max(img) - np.min(img))

使用opencv(大约时间:3ms):

norm = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

这两种方法对我的用例来说都很慢。谁能指导我使用更快的图像标准化方法?

【问题讨论】:

  • 您的用例到底是什么?你的图片有多大?你希望它有多快? (如果 numpy 太慢,要么 python 是错误的语言,要么你的期望不切实际)
  • 首先,您调用np.min 2 次,如果您那么在意性能,那是浪费时间。
  • + 请说明你的img是什么类型的
  • 请说明您的图像尺寸、当前需要多长时间、需要多长时间,并说明您的操作系统以及您是在 Raspberry Pi 还是超级计算机上运行。
  • 嗨,我已经更新了问题。

标签: python numpy computer-vision vision


【解决方案1】:

在我看来,您的时间安排很慢。您的安装可能有问题?

我试过这个测试程序:

#!/usr/bin/python3

import sys
import numpy as np
import cv2
from PIL import Image
from profilehooks import profile

@profile
def try_numpy(img):
    ar = np.array(img).astype(np.float32)
    for i in range(1000):
        mn = np.min(ar)
        mx = np.max(ar)
        norm = (ar - mn) * (1.0 / (mx - mn))

@profile
def try_cv2(img):
    for i in range(1000):
        norm = cv2.normalize(img, None, alpha=0, beta=1,
                             norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

img = Image.open(sys.argv[1])
try_numpy(img)

img = cv2.imread(sys.argv[1])
try_cv2(img)

在这台运行 Ubuntu 19.04 的 2015 i5 笔记本电脑上,我看到了:

$ ./try291.py ~/pics/150x150.png 
*** PROFILER RESULTS ***
try_cv2 (./try291.py:17)
function called 1 times

         1002 function calls in 0.119 seconds

   Ordered by: cumulative time, internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001    0.119    0.119 try291.py:17(try_cv2)
     1000    0.118    0.000    0.118    0.000 {normalize}

*** PROFILER RESULTS ***
try_numpy (./try291.py:9)
function called 1 times

         10067 function calls in 0.113 seconds

   Ordered by: cumulative time, internal time, call count
   List reduced from 52 to 40 due to restriction <40>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.064    0.064    0.113    0.113 try291.py:9(try_numpy)
     2000    0.004    0.000    0.045    0.000 fromnumeric.py:69(_wrapreduction)

所以它们每次调用都需要大约 0.1 毫秒,比您看到的数字快约 50 倍。

进一步加快速度:

  • 您对像素值的范围有任何先验知识吗?也许您可以跳过搜索最大值和最小值。
  • 根据您的采样密度,标准化整个输入图像的速度可能会更快,然后剪下 150x150 的补丁。

【讨论】:

  • 抱歉,您正在比较使用“除法”的 try_numpy 和不使用“除法”的 cv2(感谢上帝)。实际上,cv2 比最好的解决方案慢一点:norm = (img - mn) * (1.0 / (mx - mn)),比 cv2 快大约 3%(出于某种未知原因......)而且,你不'不知道在 try_numpy 中使用了什么数据类型。如果你强制float32,它会好得多。
  • 哦,谢谢!我已经更新了。
猜你喜欢
  • 2017-03-31
  • 2019-11-24
  • 2015-01-18
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多