多目标图像分割
本人参考以下博客进行多目标进行分割,结果很好。
https://blog.csdn.net/weixin_42380114/article/details/103294631
运行环境:python,opencv
1、彩色图像阈值分割
以下是彩色图像阈值分割的代码,根据个人图片的情况可以自行修改。
def delet(image):
for i in range(3000):
for j in range(4096):
for k in range(3):
if int(image[i][j][1])-int(image[i][j][0]) >=50:
image[i][j] = 0
if int(image[i][j][0]) <= 20:
image[i][j] = 0
return image
2、二值化
二值化变为黑白图像,将目标区域变为白色,背景区域变为黑色。
ret, thresh1 = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY)
cv2.namedWindow('th1', 0)
cv2.resizeWindow('th1', 682, 500)
cv2.imshow('th1', thresh1)
cv2.waitKey(0)
3、双边滤波去噪+填充
blur = cv2.bilateralFilter(thresh1, 9, 80, 80)
fill = blur.copy()
h, w = blur.shape[: 2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(fill, mask, (90, 90), 255);
fill_INV = cv2.bitwise_not(fill)
fill_out = blur | fill_INV
cv2.namedWindow('fill_out_img', 0)
cv2.resizeWindow('fill_out_img', 682, 500)
cv2.imshow('fill_out_img', fill_out)
#cv2.imwrite('K:/opencv/tianchong.png', fill_out)
cv2.waitKey(0)