Numpy 创建一个绿色通道比蓝色和红色通道亮的遮罩?

Numpy 创建一个绿色通道比蓝色和红色通道亮的遮罩?,numpy,opencv,opencv-python,Numpy,Opencv,Opencv Python,我试图从照片中提取绿色LED显示屏的显示,我发现预处理照片最简单的方法是屏蔽(变黑)绿色通道不是最亮通道的所有像素。我创建了一个算法来实现这一点,但速度非常慢: def mask_dominant(image, color): # For example, if color == "green", then we blacken out # all pixels where green isn't the brightest pixel image_copy = np.c

我试图从照片中提取绿色LED显示屏的显示,我发现预处理照片最简单的方法是屏蔽(变黑)绿色通道不是最亮通道的所有像素。我创建了一个算法来实现这一点,但速度非常慢:

def mask_dominant(image, color):
    # For example, if color == "green", then we blacken out
    # all pixels where green isn't the brightest pixel
    image_copy = np.copy(image)
    black_pixel = np.array([0, 0, 0], dtype=np.uint8)
    height, width, _ = image_copy.shape
    for row in range(height):
        for col in range(width):
            # OpenCV stores colors in BGR
            b, g, r = image_copy[row, col]
            zero = False
            if color == 'blue':
                if b < g or b < r:
                    zero = True
            elif color == 'green':
                if g < b or g < r:
                    zero = True
            elif color == 'red':
                if r < b or r < g:
                    zero = True
            else:
                raise AssertionError("Color must be one of blue, green, or red")
            if zero:
                image_copy[row, col] = black_pixel
    return image_copy
上面的算法在一张照片上运行需要40秒,这太大了。是否有一个内置算法可以做同样的事情,或者我可以使用numpy优化?

此解决方案有效:

def mask_dominant(image, color):
    # For example, if color == Green, then it blacks out
    # all pixels where green isn't the brightest pixel
    b,g,r = cv2.split(image)
    if color == 'green':
        target = g
        other1 = b
        other2 = r
    elif color == 'red':
        target = r
        other1 = g
        other2 = b
    elif color == 'blue':
        target = b
        other1 = g
        other2 = r
    else:
        raise AssertionError("invalid color: " + color)

    # Figure out which ones we need to zero & zero them
    should_zero = (target < other1) | (target < other2)
    g[should_zero] = 0
    r[should_zero] = 0
    b[should_zero] = 0

    # Merge channels back
    return cv2.merge((b,g,r))
def mask_显性(图像、颜色):
#例如,如果颜色==绿色,则它会变黑
#绿色不是最亮像素的所有像素
b、 g,r=cv2.分割(图像)
如果颜色=‘绿色’:
目标=g
其他1=b
其他2=r
elif颜色==“红色”:
目标=r
其他1=g
其他2=b
elif颜色==“蓝色”:
目标=b
其他1=g
其他2=r
其他:
引发断言错误(“无效颜色:+颜色”)
#找出我们需要归零的对象&归零
是否应该_zero=(目标<其他1)|(目标<其他2)
g[应为0]=0
r[应为0]=0
b[应该为零]=0
#归并频道
返回cv2.merge((b,g,r))

@PaulPanzer-你能举个例子吗?链接上有一个例子。事实上,这与你想要实现的目标非常相似。谢谢@PaulPanzer-我找到了答案并回答了我自己的问题
def mask_dominant(image, color):
    # For example, if color == Green, then it blacks out
    # all pixels where green isn't the brightest pixel
    b,g,r = cv2.split(image)
    if color == 'green':
        target = g
        other1 = b
        other2 = r
    elif color == 'red':
        target = r
        other1 = g
        other2 = b
    elif color == 'blue':
        target = b
        other1 = g
        other2 = r
    else:
        raise AssertionError("invalid color: " + color)

    # Figure out which ones we need to zero & zero them
    should_zero = (target < other1) | (target < other2)
    g[should_zero] = 0
    r[should_zero] = 0
    b[should_zero] = 0

    # Merge channels back
    return cv2.merge((b,g,r))