查找图像的rgb像素颜色计数的最快方法 [英] fastest way to find the rgb pixel color count of image
问题描述
我有一个用例,在搜索后,我必须找到实时视频每一帧的连续rgb像素颜色计数,我发现了一段代码可以执行相同的操作,但从性能角度来看,大约需要3秒钟才能给我输出,但就我而言,我必须尽可能快地执行此计算,可能是在1秒内达到25帧.有人可以通过重构以下代码来帮助我弄清楚该怎么做
I have a use case where i have to find the consecutive rgb pixel color count of each frame of live video after searching i found a piece of code which does the same thing but performance wise it take around ~ 3 sec to give me output but in my case i have to do this calculation as fast as possible may be 25 frames in 1 seconds. Can someone help me to figure out how to do this by refactoring the below code
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
Pixel Count per Unique Color: 130869
时差是3.9660612
The time difference is : 3.9660612
推荐答案
您需要使用Numpy或OpenCV在Python中进行快速图像处理.我制作了9色版本的Paddington:
You need to use Numpy, or OpenCV, for fast image processing in Python. I made a 9-colour version of Paddington:
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),依此类推.
That means there are 20,389 pixels of RGB(14,48,84), and so on.
在我的Mac上,一张400x400的图片需要125毫秒的时间,这将为您提供8 fps,因此,您最好至少拥有4个CPU核心,并使用所有这些核心获得25+ fps.
That takes 125ms on my Mac for a 400x400 image, which will give you 8 fps, so you better have at least 4 CPU cores and use all of them to get 25+ fps.
更新
我认为您实际上可以比这快得多.如果将每个像素的点积与[1,256,65536]相乘,则每个像素将获得一个24位数字,而不是3个8位数字.这样一来,找到唯一值的速度要快得多.看起来像这样:
I think you can actually go significantly faster than this. If you take the dot-product of each of the pixels with [1,256,65536], you will get a single 24-bit number for each pixel, rather than 3 8-bit numbers. It is then a lot faster to find the unique values. That looks like this:
# 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上,这需要4毫秒而不是125毫秒:-)
That takes 4ms rather than 125ms on my Mac :-)
关键字:Python,Numpy,PIL/枕头,图像处理,计算独特的颜色,计算颜色.
Keywords: Python, Numpy, PIL/Pillow, image processing, count unique colours, count colors.
这篇关于查找图像的rgb像素颜色计数的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!