python - 如何检测 3 种不同颜色的交集

标签 python opencv

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

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

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

enter image description here

最佳答案

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

enter image description here

我没有费尽心思去处理 3 维的 8 位 RGB 颜色,而是将其转换为单个 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')

enter image description here

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

结果图像中的值的直方图显示,有 21 个像素在其 3x3 邻域中具有 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、过滤器。

关于python - 如何检测 3 种不同颜色的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57845611/

相关文章:

python - PYTHON-Google Analytics-上传产品数据-THROUGH API

python - 出不来这个洞: can't use pre-learnt model's output

python - 修正线性单元的反向传播

python - 'gi.repository.Gtk' 对象没有属性 'gdk'

python - PyQt 应用程序成功关闭,但进程没有被终止?

python - OpenCV imwrite()/imread() 这两个函数是否以任何方式修改图像?

android - Android OpenCV 图像处理在特定手机上的超慢性能,但不是全部

C++: 错误: opencv_core: 即使需要的 .so 文件在/usr/lib/中,也没有这样的文件或目录

matlab - 点云、聚类、 Blob 检测

python - 遍历在 cheerypy python 中作为 **kwargs 传递的列表