我需要帮助将一个三维数组(RGB/BGR图像)使用一个二维索引数组进行索引。所有值都是0、1或2,代表不同的颜色通道。结果应该是一个二维颜色值数组。如果有人能告诉我在Python中实现这个的语法,那就太好了!
为了了解我正在尝试做什么(也请阅读下面的TLDR):
我实际上正在尝试将以下代码从普通的for循环语法转换为更有效的Python / Numpy语法,因为它非常慢。
为了了解我正在尝试做什么(也请阅读下面的TLDR):
我实际上正在尝试将以下代码从普通的for循环语法转换为更有效的Python / Numpy语法,因为它非常慢。
colorIndices = np.zeros((height,width)); # an array which has the index of the outstanding color
colorIndices -= 1; # all -1's
for x in range(0,width):
for y in range(0,height):
pix = img[y,x]; # get the pixel, a 1D array of length 3
colorID = np.argmax(pix); #get which index has max value (candidate for outstanding color)
if(pix[colorID]>np.average(pix)+np.std(pix)): # if that value is more than one std dev away from the overall pixel's value, the pixel has an outstanding color
colorIndices[y,x] = colorID;
我希望能够使用类似以下方式访问每个像素中的杰出颜色通道:
img[:,:,:]=0;
img[colorIndices] = 255;
简而言之,我想要设置一个像素为纯蓝、绿或红色,如果它是该颜色的一种阴影。我定义一个像素是否为红色的方法是,如果该像素的R值比 {R,G,B} 的整体分布平均值高一个标准差以上。
到目前为止,我有一些错误的代码:
colorIDs = np.argmax(img, axis=2);
averages = np.average(img, axis=2);
stds = np.std(img, axis=2);
cutoffs = averages + stds;
print(img[colorIDs]);
img
的示例吗? - najeem