利用分水岭算法 检测图片中的冰雹 但是冰雹大小不一 而且有粘连部分 侵蚀太多导致小块冰雹直接没了 侵蚀太少粘连部分又分不开,这应该咋改啊

import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取图像
src = cv2.imread('D:/pycharm/ice/photo/4.jpg')
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
cv2.waitKey(0)
H = 50
# 使用形态学操作实现H-minima变换
kernel = np.ones((6, 5), np.uint8)
minima = cv2.erode(gray, kernel, iterations=1)
h_minima_transformed = cv2.subtract(gray, minima)
h_minima_transformed[h_minima_transformed < H] = 0
cv2.imshow('H-minima transformed', h_minima_transformed)
cv2.waitKey(0)
cv2.destroyAllWindows()
img1 = gray+h_minima_transformed
cv2.imshow('img', img1)
cv2.waitKey(0)
ret1, th1 = cv2.threshold(img1, 200, 255, 0)
cv2.imshow('th1', th1)
cv2.waitKey(0)
k1 = np.ones((6, 3), np.uint8)
opening = cv2.morphologyEx(th1, cv2.MORPH_OPEN, k1)
cv2.imshow('opening1', opening)
cv2.waitKey(0)
k2 = np.ones((3, 3), np.uint8)
sure_bg = cv2.dilate(opening, k2, iterations=2)
sure_fg = cv2.erode(opening, k1, iterations=1)
cv2.imshow("sure_bg", sure_bg)
cv2.imshow("sure_fg", sure_fg)
cv2.waitKey(0)
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
# Normalize the distance image for range = {0.0, 1.0}
cv2.normalize(dist_transform, dist_transform, 0, 1.0, cv2.NORM_MINMAX)
cv2.imshow('dist_transform', dist_transform)
cv2.waitKey(0)
unknown = cv2.subtract(sure_bg, sure_fg) # unknown area
cv2.imshow('unknown', unknown)
cv2.waitKey(0)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# Now, mark the region of unknown with zero
markers[unknown == 255] = 0
markers_show = np.uint8(markers)
cv2.imshow('markers', markers_show * 100)
# 分水岭算法分割
markers = cv2.watershed(src, markers=markers)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(markers)
markers_8u = np.uint8(markers)
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
(255, 0, 255), (0, 255, 255), (255, 128, 0), (255, 0, 128),
(128, 255, 0), (128, 0, 255), (255, 128, 128), (128, 255, 255)]
for i in range(2, int(max_val + 1)):
ret, thres1 = cv2.threshold(markers_8u, i - 1, 255, cv2.THRESH_BINARY)
ret2, thres2 = cv2.threshold(markers_8u, i, 255, cv2.THRESH_BINARY)
mask = thres1 - thres2
cv2.imshow('mask', mask)
cv2.waitKey(0)
# color = (rd.randint(0,255), rd.randint(0,255), rd.randint(0,255))
# image[markers == i] = [rd.randint(0,255), rd.randint(0,255), rd.randint(0,255)]
# image[markers == i] = [colors[i-2]]
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = cv2.drawContours(src, contours, -1, colors[(i - 2) % 12], -1)#图片上的划分的颜色
#cv2.imshow('contours', contours)
# cv2.waitKey(0)
# cv2.drawContours(src,contours,-1,colors[(i-2)%12],-1)
if len(contours) > 0:
# 计算第一个轮廓的矩
M = cv2.moments(contours[0])
# 在这里继续处理 M 或者其他操作
else:
print("No contours found.")
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00']) # 轮廓重心
cv2.drawMarker(src, (cx, cy), (150, 150, 150), 1, 10, 2)
cv2.drawMarker(src, (cx, cy), (150, 150, 150), 1, 10, 2)
cv2.putText(src, "count=%d" % (int(max_val - 1)), (200, 30), 0, 1, (0, 255, 0), 2)#图片上的文字
cv2.imshow('regions', src)
cv2.waitKey(0)
result = cv2.addWeighted(src, 0.6, src, 0.5, 0) # 图像权重叠加
cv2.imshow('result', result)
cv2.waitKey(0)