心有法竹
由于您的注释掩盖了您要识别的交叉点,因此我制作了一个新的类似图像。我没有试图弯曲我的大脑来处理 8 位 RGB 颜色的 3 维,而是将其转换为单个 24 位整数,然后运行一个通用过滤器SciPy并计算每个 3x3 窗口中唯一颜色的数量并从中制作了一个新图像。因此结果中的每个像素的亮度值等于其邻域中的颜色数量。我通过将 Numpy 邻居数组转换为 Python 集合来计算颜色的数量——利用了集合中只能有唯一数字的事实。#!/usr/bin/env python3import numpy as npfrom PIL import Imagefrom scipy.ndimage import generic_filter# CountUniquedef 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 arrayPILim = Image.open('patches.png').convert('RGB')RGBim = np.array(PILim)# Make a single channel 24-bit image rather than 3 channels of 8-bit eachRGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)# Run generic filter counting unique colours in neighbourhoodresult = generic_filter(RGB24, CountUnique, (3, 3))# Save resultImage.fromarray(result.astype(np.uint8)).save('result.png')此处显示了生成的图像,并拉伸了对比度,以便您可以在您寻找的交叉点看到最亮的像素。结果图像中值的直方图显示,有 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 python3import numpy as npfrom PIL import Imagefrom skimage.morphology import dilation, disk# Open image and make into Numpy arrayPILim = 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 eachRGB24 = (RGBim[...,0].astype(np.uint32)<<16) | (RGBim[...,1].astype(np.uint32)<<8) | RGBim[...,2].astype(np.uint32)# Make list of unique coloursUniqueColours = np.unique(RGB24)# Create result imageresult = np.zeros((h,w),dtype=np.uint8)# Make mask for any particular colour - same size as original imagemask = np.zeros((h,w), dtype=np.uint8)# Make disk-shaped structuring element for morphologyselem = disk(1)# Iterate over unique coloursfor 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 resultImage.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、过滤器。