首页 > 解决方案 > 如何检测3种不同颜色的交集

问题描述

我有一个由许多不同纯色的多边形创建的图像。坐标本身没有给出,但如果需要可以检测。

我正在寻找一种方法来检测所有点,这些点是 3 种或更多不同颜色的交集。颜色是事先不知道的,可能彼此相似(例如,一种可能是 (255, 255, 250),另一种是 (255, 255, 245)。具体的颜色并不重要,只是事实是不同的)。

例如,在下图中,一个小星星标记了我正在寻找的所有点。

在此处输入图像描述

标签: pythonopencv

解决方案


由于您的注释掩盖了您要识别的交叉点,因此我制作了一个新的类似图像。

在此处输入图像描述

我没有试图弯曲我的大脑来处理 8 位 RGB 颜色的 3 维,而是将其转换为单个 24 位整数,然后运行一个通用过滤器SciPy并计算每个 3x3 窗口中唯一颜色的数量并从中制作了一个新图像。因此结果中的每个像素的亮度值等于其邻域中的颜色数量。我通过将 Numpy 邻居数组转换为 Python 集合来计算颜色的数量——利用了集合中只能有唯一数字的事实。

#!/usr/bin/env python3

import numpy as np
from PIL import Image
from scipy.ndimage import generic_filter

# CountUnique
def CountUnique(P):
    """
    We receive P[0]..P[8] with the pixels in the 3x3 surrounding window, return count of unique values
    """
    return len(set(P))

# Open image and make into Numpy array
PILim = Image.open('patches.png').convert('RGB')
RGBim = np.array(PILim)

# Make a single channel 24-bit image rather than 3 channels of 8-bit each
RGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)

# Run generic filter counting unique colours in neighbourhood
result = generic_filter(RGB24, CountUnique, (3, 3))

# Save result
Image.fromarray(result.astype(np.uint8)).save('result.png')

在此处输入图像描述

此处显示了生成的图像,并拉伸了对比度,以便您可以在您寻找的交叉点看到最亮的像素。

结果图像中值的直方图显示,在其 3x3 邻域中有 21 个像素具有 3 种唯一颜色,在其邻域中有 4,348 个像素具有 2 种唯一颜色。例如,您可以通过运行找到这些np.where(result==3)

  Histogram:
    155631: (  1,  1,  1) #010101 gray(1)
      4348: (  2,  2,  2) #020202 gray(2)
        21: (  3,  3,  3) #030303 gray(3)

为了更有趣,我尝试编写@Micka 建议的方法,并给出相同的结果,代码如下所示:

#!/usr/bin/env python3

import numpy as np
from PIL import Image
from skimage.morphology import dilation, disk

# Open image and make into Numpy array
PILim = Image.open('patches.png').convert('RGB')
RGBim = np.array(PILim)
h, w = RGBim.shape[0], RGBim.shape[1]

# Make a single channel 24-bit image rather than 3 channels of 8-bit each
RGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)

# Make list of unique colours
UniqueColours = np.unique(RGB24)

# Create result image
result = np.zeros((h,w),dtype=np.uint8)

# Make mask for any particular colour - same size as original image
mask = np.zeros((h,w), dtype=np.uint8)

# Make disk-shaped structuring element for morphology
selem = disk(1)

# Iterate over unique colours
for i,u in enumerate(UniqueColours):
   # Turn on all pixels matching this unique colour, turn off all others
   mask = np.where(RGB24==u,1,0)
   # Dilate (fatten) the mask by 1 pixel
   mask = dilation(mask,selem)
   # Add all activated pixels to result image
   result = result + mask

# Save result
Image.fromarray(result.astype(np.uint8)).save('result.png')

作为参考,我在命令行的 ImageMagick 中创建了禁用抗锯齿的图像,如下所示:

convert -size 400x400 xc:red -background red +antialias              \
  -fill blue   -draw "polygon 42,168 350,72 416,133 416,247 281,336" \
  -fill yellow -draw "polygon 271,11 396,127 346,154 77,86"          \
  -fill lime   -draw "polygon 366,260 366,400 120,400" patches.png

关键词:Python、图像、图像处理、相交、相交、PIL/Pillow、邻接、邻里、邻里、邻居、邻居、通用、SciPy、3x3、过滤器。


推荐阅读