找到图像的RGB像素颜色计数的最快方法

7
我有一个使用案例,需要找到实时视频每帧连续的RGB像素颜色计数。在搜索后,我找到了一段代码可以完成相同的功能,但性能方面大约需要3秒才能给出输出。但在我的情况下,我需要尽快进行这个计算,可能是每秒25帧。有人可以帮助我通过重构以下代码来解决这个问题吗?
from PIL import Image
import timeit

starttime = timeit.default_timer()
with Image.open("netflix.png") as image:
    color_count = {}
    width, height = image.size
    print(width,height)
    rgb_image = image.convert('RGB')
    for x in range(width):
        for y in range(height):
            rgb = rgb_image.getpixel((x, y))
            if rgb in color_count:
                color_count[rgb] += 1
            else:
                color_count[rgb] = 1

    print('Pixel Count per Unique Color:')
    print('-' * 30)
    print(len(color_count.items()))
print("The time difference is :", timeit.default_timer() - starttime)

输出:

每种颜色的像素计数: 130869

时间差为:3.9660612

2个回答

16
你需要在Python中进行快速图像处理,可以使用Numpy或OpenCV。我制作了一个带有9种颜色的Paddington版本:

enter image description here

from PIL import Image
import numpy as np

# Open Paddington and make sure he is RGB - not palette
im = Image.open('paddington.png').convert('RGB')

# Make into Numpy array
na = np.array(im)

# Arrange all pixels into a tall column of 3 RGB values and find unique rows (colours)
colours, counts = np.unique(na.reshape(-1,3), axis=0, return_counts=1)

print(colours)
print(counts)

结果

[[ 14  48  84]
 [ 19  21  30]
 [ 33 108 163]
 [ 33 152 190]
 [ 72  58  58]
 [ 96 154 210]
 [180  89  64]
 [205 210 200]
 [208 151  99]]

[20389 40269 12820  1488 17185 25371 17050 16396  9032]

这意味着有20,389个RGB(14,48,84)的像素,以此类推。

在我的Mac上,对于一个400x400的图像,需要125毫秒,这将给你8 fps,因此你最好至少有4个CPU核心并使用它们来获得25+ fps。


更新

我认为你实际上可以比这快得多。如果你将每个像素与[1,256,65536]的点积相乘,则会为每个像素获得单个24位数字,而不是3个8位数字。然后查找唯一值会更快。具体做法如下:

# Open Paddington and make sure he is RGB - not palette
im = Image.open('paddington.png').convert('RGB')

# Make into Numpy array
na = np.array(im)

# Make a single 24-bit number for each pixel
f = np.dot(na.astype(np.uint32),[1,256,65536]) 

nColours = len(np.unique(f))     # prints 9

在我的Mac上,这只需要4ms而不是125ms :-)


关键词: Python, Numpy, PIL/Pillow, 图像处理, 计算唯一颜色, 计算颜色.


0
这个函数返回一个漂亮的DataFrame,包含你所需的所有信息,并且速度非常快 - 比起使用np.unique(...)的颜色和计数方法快10倍。
import numexpr  # Import the mighty mighty 'numexpr' library for efficient numerical expression evaluation - up to 10x faster than numpy
import numpy as np
import pandas as pd


def count_colors(img, sort=True):
    """
    Count the unique colors in an image and return a DataFrame with color counts.

    Parameters:
    img (numpy.ndarray): Input image as a NumPy array with shape (height, width, 3).
    sort (bool, optional): Whether to sort the resulting DataFrame by color count. Default is True.

    Returns:
    pandas.DataFrame: A DataFrame containing color information with columns 'a' ,'r', 'g', 'b', and 'c' (count).
    """
    r, g, b = (
        img[..., 2],
        img[..., 1],
        img[..., 0],
    )  # Extract the red, green, and blue color channels from the image.

    absnum = numexpr.evaluate(
        # Use 'numexpr' to efficiently calculate a unique numerical representation for each color.
        "(r << 16) + (g << 8) + b",
        global_dict={},
        local_dict={"r": r, "g": g, "b": b},
    )

    npunique = np.unique(
        absnum, return_counts=True
    )  # Find unique values and their counts in the 'absnum' array.

    return pd.merge(  # Merge DataFrames to combine color information and color counts.
        pd.DataFrame(
            {
                "r": r.ravel(),
                "g": g.ravel(),
                "b": b.ravel(),
                "a": absnum.ravel(),
            }
        )
        .drop_duplicates()  # Remove duplicate rows based on the unique numerical representation 'a'.
        .reset_index(drop=True)  # Reset the DataFrame index after dropping duplicates.
        .set_index("a"),  # Set the 'a' column as the index of the DataFrame.
        pd.DataFrame({"a": npunique[0], "c": npunique[1]}).set_index("a"),
        # Create another DataFrame with 'a' and 'c' columns.
        left_index=True,  # Merge DataFrames using the index of the left DataFrame ('a' column).
        right_index=True,  # Merge DataFrames using the index of the right DataFrame ('a' column).
        sort=sort,  # Sort the resulting DataFrame by color count if 'sort' is True.
    ).reset_index(
        drop=False
    )  # Reset the index of the merged DataFrame and return it.


# df
# Out[3]:
#           a    r    g    b      c
# 0    929876   14   48   84  20389
# 1   1250590   19   21   30  40269
# 2   2190499   33  108  163  12820
# 3   2201790   33  152  190   1488
# 4   4733498   72   58   58  17185
# 5   6331090   96  154  210  25371
# 6  11819328  180   89   64  17050
# 7  13488840  205  210  200  16396
# 8  13670243  208  151   99   9032
# %timeit colours, counts = np.unique(pic.reshape(-1,3), axis=0, return_counts=1)
# %timeit df = count_colors(pic)
# 127 ms ± 3.81 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 12.8 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接