1 回答
TA貢獻1866條經(jīng)驗 獲得超5個贊
由于您的注釋掩蓋了您要識別的交叉點,因此我制作了一個新的類似圖像。

我沒有試圖彎曲我的大腦來處理 8 位 RGB 顏色的 3 維,而是將其轉(zhuǎn)換為單個 24 位整數(shù),然后運行一個通用過濾器SciPy并計算每個 3x3 窗口中唯一顏色的數(shù)量并從中制作了一個新圖像。因此結(jié)果中的每個像素的亮度值等于其鄰域中的顏色數(shù)量。我通過將 Numpy 鄰居數(shù)組轉(zhuǎn)換為 Python 集合來計算顏色的數(shù)量——利用了集合中只能有唯一數(shù)字的事實。
#!/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')

此處顯示了生成的圖像,并拉伸了對比度,以便您可以在您尋找的交叉點看到最亮的像素。
結(jié)果圖像中值的直方圖顯示,有 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 建議的方法,并給出相同的結(jié)果,代碼如下所示:
#!/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 中創(chuàng)建了禁用抗鋸齒的圖像,如下所示:
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
關(guān)鍵詞:Python、圖像、圖像處理、相交、相交、PIL/Pillow、鄰接、鄰里、鄰里、鄰居、鄰居、通用、SciPy、3x3、過濾器。
添加回答
舉報
