【问题标题】:Efficient way to threshold image array and render - Python / NumPy / OpenCV阈值图像数组和渲染的有效方法 - Python / NumPy / OpenCV
【发布时间】:2020-03-23 03:12:28
【问题描述】:

我有一台正在将图像数据发送到我的计算机的相机。从那里我的 python 脚本将 8 位颜色信息(黑白;范围从 0 - 黑色 - 到 255 - 白色)放入一个 numpy 数组中。数组是二维的,第一维最大为 384,第二维最大为 288 用 openCV 窗口显示这个效果很好,实时视频超过 24fps。

我现在的目标是处理图像,以便实时视频将任何低于 200 的颜色值显示为 0(全黑),将任何高于 200 的颜色值显示为 255(全白)。但是,我现在的代码只给了我大约 3fps。

我的代码正在执行以下操作:

  • 将来自相机的图像保存在一个 numpy 数组中
  • 打开第一个 for 循环以遍历 x 值
  • 在第一个 for 循环中使用 y 值遍历第二个 for 循环
  • 检查每个像素的每个颜色值,并检查它是否高于 200
  • 根据if 子句将颜色值设为0 或255
  • 显示新图像

这是代码中的决定性部分:

processedImage = frame
i = 0
for i in range(0, displayedWidth):
    ii = 0
    for ii in range(0, displayedHeight):
        if frame[ii, i] > 200:
            processedImage[ii, i] = 255
        else: 
            processedImage[ii, i] = 0
cv2.imshow("LiveVideo", processedImage)

我读到herefor 循环比while 循环快,但它并没有显着提高代码的速度,这就是为什么我认为processedImage 的重写需要太长时间。

有没有办法让整个过程更快?

感谢您的任何回答!

【问题讨论】:

    标签: python arrays performance loops numpy


    【解决方案1】:

    试试这个:

    frame[frame > 200] = 255
    frame[frame <= 200] = 0
    cv2.imshow("LiveVideo", frame)
    

    【讨论】:

    • 非常感谢!这使它恢复到超过 24fps 并且视频再次流畅运行。将尽快将其标记为正确。
    【解决方案2】:

    np.where()也可以使用:

    processed_frame = np.where(frame >= 200, 255, 0)
    

    例如

    >>> im = (np.random.rand(5, 5) * 255).round()
    >>> im
    array([[ 45.,  92.,  74., 207., 211.],
           [206., 184.,  31., 117., 119.],
           [ 88.,  82., 203.,  34.,   5.],
           [157.,  99., 154., 251.,  54.],
           [160., 177.,  14., 206., 234.]])
    >>> np.where(im >= 200, 255, 0)
    array([[  0,   0,   0, 255, 255],
           [255,   0,   0,   0,   0],
           [  0,   0, 255,   0,   0],
           [  0,   0,   0, 255,   0],
           [  0,   0,   0, 255, 255]])
    >>>
    

    【讨论】:

      【解决方案3】:

      使用 Seb 的解决方案是一个很大的改进,但似乎使用 numpy.where 更快:

      %%timeit
      frame1 = frame.copy()
      frame1[frame1 > 200] = 255
      frame1[frame1 <= 200] = 0
      
      974 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
      

      对比

      %%timeit
      frame2 = frame.copy()
      frame2 = np.where(frame2 > 200, 255, 0)
      
      385 µs ± 7.16 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
      
      >>> np.alltrue(frame1 == frame2)
      True
      

      【讨论】:

      • 当我尝试使用您的或@AKX 解决方案时,我在尝试使用cv2.imshow(...) 显示图像时收到错误(-215: Assertion failed)。我是否也可以保存颜色值高于 200 的点,还是需要第二个 np.where() 语句?
      • 是的,结果应该和Seb的做法一样。作为参考,我用这个数组对其进行了测试:frame = np.floor(256 * np.random.rand(384, 288)).astype(int)
      【解决方案4】:

      继续使用 NumPy,似乎最快的是 -

      (frame>200)*np.uint8(255)
      

      既然您已经在使用 OpenCV,那么更快的方法是使用 cv2.threshold -

      cv2.threshold(frame,200,255,cv2.THRESH_BINARY)[1]
      

      运行示例以验证结果并获取 1024X1024 图像上的时间 -

      In [2]: np.random.seed(0)
         ...: frame = np.random.randint(0,256,(1024,1024)).astype(np.uint8)
      
      In [3]: %timeit (frame>200)*np.uint8(255)
      253 µs ± 13.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
      
      In [4]: %timeit cv2.threshold(frame,200,255,cv2.THRESH_BINARY)[1]
      58.2 µs ± 437 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)    
      
      In [7]: out1 = (frame>200)*np.uint8(255)
      
      In [8]: out2 = cv2.threshold(frame,200,255,cv2.THRESH_BINARY)[1]
      
      In [9]: np.allclose(out1, out2)
      Out[9]: True
      

      在相同数据上使用其他解决方案的时间 -

      # @AKX's soln
      In [10]: %timeit np.where(frame >= 200, 255, 0)
      3.73 ms ± 22.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
      
      # @Seb's soln
      In [11]: %%timeit
          ...: frame[frame > 200] = 255
          ...: frame[frame <= 200] = 0
      10.2 ms ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
      

      带有cv2.threshold 的OpenCV 版本似乎是最快的版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-15
        • 2022-10-04
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        • 2015-09-11
        • 2011-12-21
        • 1970-01-01
        相关资源
        最近更新 更多