当您使用opencv-python (cv2)并从VideoCapture设备读取时,它会返回一个表示图像的numpy数组,在我的例子中,维数是(480,640,3)。我读过关于矢量化的文章,但我还不能真正理解它。
这是我想要映射的函数
def RGBtoRGChromaticity(pixel):
r, g, b = pixel
total = r + g + b
return r/total, g/total, b/total下面是我对它进行向量化的尝试,但它不起作用:(
def RGBtoRGChromaticity(pixel):
r, g, b = pixel
total = ufunc.add(r, g, b)
return ufunc.true_divide(r, total), ufunc.true_divide(g, total), ufunc.true_divide(b, total)我正在尝试拍摄一张图像并找到绿色像素。我在一个名为RG Chromaticity的颜色空间上找到了这篇文章,据我所知,它可以很容易地找到每个像素中的主色。这篇文章中的数学似乎遵循了这个想法。我这里的主要问题是如何在numpy数组上映射一个函数,但是如果任何人对色彩空间和更好的方法有任何建议,请不要犹豫地分享!
发布于 2021-03-19 16:08:34
矢量化的要点是一次将其应用于整个数组,而不是每个像素。
比方说你有
img = np.random.randint(255, size=(480, 640, 3), dtype=np.uint8)你的函数就变成了
def RGBtoRGC(img):
return img / img.sum(axis=-1, keepdims=True)如果您希望输出为uint8,我建议四舍五入:
def RGBtoRGC(img):
return np.rint(img / img.sum(axis=-1, keepdims=True), dtype=np.uint8)发布于 2021-03-19 14:29:09
如何在map_function参数中拆分通道?像这样
import numpy as np
def RGBtoRGChromaticity(r, g, b):
total = r + g + b
return r / total, g / total, b / total
vfunc = np.vectorize(RGBtoRGChromaticity)
image = np.random.uniform(size=(480, 640, 3))
res = vfunc(image[:, :, 0], image[:, :, 1], image[:, :, 2])https://stackoverflow.com/questions/66703182
复制相似问题