【问题标题】:Convert all values in numpy array if that value found in a list of numpy array without MemoryError如果在没有 MemoryError 的 numpy 数组列表中找到该值,则转换 numpy 数组中的所有值
【发布时间】:2020-05-18 23:51:19
【问题描述】:

我有一个彩色图像 (img),如果在我的 RGB 值列表 (rgbL) 中找到 RGB 值而不执行循环,我想更改该图像的所有 RGB 值。如何以 Numpyish 的方式做到这一点。

img = cv2.imread("image location") 
rgbL = [[0, 0, 113],[107, 9, 0], [ 98, 131, 119], 
        [108, 124, 113], [108, 125, 114], ..... , [108, 125, 116], 
        [108, 127, 112]]

newVal = # LINE OF CODES THAT DOES THE JOB

有一个工作代码@How to check if all items in list of RGB colors is in an image without a loop?

但是当我的 rbgL 太长时,我得到了这个错误:MemoryError: Unable to allocate array with shape (3047, 1048576, 3) and data type int32

【问题讨论】:

  • StackOverflow 不是免费的代码编写服务。请展示您已经尝试过的内容以及它与您想要的内容有何不同。
  • 这篇文章没有解决您的问题吗? stackoverflow.com/questions/61829495/…
  • 我更新了我的帖子。这样就可以了,但是当我的 rgbL 太长时出现 MemoryError。
  • 这不是意料之中的吗?没有循环 -> 高内存使用率。这是一种妥协。
  • 如果你在比较图片,最好使用np.uint8。所以(3047, 1048576, 3)大约是9.6GB,16GB系统可以处理。否则,您需要将您的 rgbL 剪切成更小的列表并执行循环。

标签: python numpy


【解决方案1】:

在链接帖子中的答案之前尝试此操作:

rgbL = np.array(rgbL, dtype=np.uint8)
img = img.astype('uint8')

这将使形状 (3047, 1048576, 3) 的数组变为 (3047*1048576*3)*8~8GB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 2017-03-08
    • 2020-03-18
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2017-12-23
    相关资源
    最近更新 更多