【发布时间】:2019-01-08 12:31:34
【问题描述】:
我需要帮助使用 2 维索引数组来索引 3 维数组(RGB/BGR 图像)。对于不同的颜色通道,所有值都是 0、1 或 2。结果应该是一个二维的颜色值数组。如果有人能告诉我在 python 中的语法,那就太好了!
关于我正在尝试做的事情的背景(另请阅读下面的 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;
TLDR:我想将像素设置为纯蓝色、绿色或红色(如果它是那种颜色的阴影)。我定义像素是否为红色阴影的方式是像素的 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的样本 -
你的代码为什么/如何被破坏以及img的形状是什么?
-
@MadPhysicist 我的代码不完整,我不知道如何完成它以满足我的目标。我应该说不完整。
-
@najeem 图片应该是任何尺寸的通用图片,但为了您的方便,我会上传我正在使用的图片。