我有一张实际上是两个连接的单元格的图像,我要应用距离变换对其进行拆分。问题在于图片有两个以上的峰,结果,我得到的最终图像不是正确的峰。
我实际上正在做的是应用Otsu的二进制阈值以获得黑白图像,并将黑白图像作为输入以应用距离变换。问题是我有两个以上的局部最大值,因此我想使用2个标记以便将两个单元格(较大的圆圈)分开。我在使用标记时找不到足够的信息,所以有人知道我该怎么做吗?感谢先验!
这是我使用的代码
import numpy as np
import cv2
from skimage.morphology import watershed
from skimage.feature import peak_local_max
import matplotlib.pyplot as plt
from scipy import ndimage
img = cv2.imread('C:\\Users\\Hex\\Desktop\\praktiki\\images1.jpg')
#split the bgr images to single planes
b,g,r = cv2.split(img)
rgb_img = cv2.merge([r,g,b])
#turn color image to gray image in order to threshold
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#threshold obtained through otsu s algorithm, maxval=255(white)
#convert everything that is brighter than threshold to black everything else is converted to white
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance
# to the background
distance = ndimage.distance_transform_edt(thresh)
local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((3, 3)),labels=thresh)
markers = ndimage.label(local_maxi)[0]
labels = watershed(-distance, markers, mask=thresh)
plt.figure(figsize=(9, 3.5))
plt.subplot(141)
plt.imshow(rgb_img, cmap='gray', interpolation='nearest')
plt.axis('off')
plt.subplot(142)
plt.imshow(-distance, interpolation='nearest')
plt.axis('off')
plt.subplot(143)
plt.imshow(labels, cmap='Spectral_r', interpolation='nearest')
plt.axis('off')
plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,
right=1)
plt.show()
这就是我的结果