以下代码使用 scikit-image 的峰值检测器,应用于图像与纯红色和纯蓝色值之间计算的距离图:
from skimage import io, color, img_as_float
from skimage.feature import corner_peaks, plot_matches
import matplotlib.pyplot as plt
import numpy as np
image = img_as_float(io.imread('colordots.jpg'))
black_mask = color.rgb2gray(image) < 0.1
distance_red = color.rgb2gray(1 - np.abs(image - (1, 0, 0)))
distance_blue = color.rgb2gray(1 - np.abs(image - (0, 0, 1)))
distance_red[black_mask] = 0
distance_blue[black_mask] = 0
coords_red = corner_peaks(distance_red, threshold_rel=0.9, min_distance=50)
coords_blue = corner_peaks(distance_blue, threshold_rel=0.9, min_distance=50)
f, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, figsize=(15, 10))
ax0.imshow(image)
ax0.set_title('Input image')
ax1.imshow(image)
ax1.set_title('Marker locations')
ax1.plot(coords_red[:, 1], coords_red[:, 0], 'ro')
ax1.plot(coords_blue[:, 1], coords_blue[:, 0], 'bo')
ax1.axis('image')
ax2.imshow(distance_red, interpolation='nearest', cmap='gray')
ax2.set_title('Distance to pure red')
ax3.imshow(distance_blue, interpolation='nearest', cmap='gray')
ax3.set_title('Distance to pure blue')
plt.show()