如何使用fill_holes函数填充分割区域?

问题描述 投票:0回答:1

我想跟踪分段区域的灰度随时间的变化。为什么 ndi.binary_fill_holes 函数没有产生很大的差异?

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sigma = 3
img_smooth = ndi.gaussian_filter(gray, sigma)
thresh = threshold_otsu(img_smooth)
new = img_smooth > thresh
fill = ndi.binary_fill_holes(new)

fig, ax = plt.subplots(1, 2, figsize=(10,7))
ax[0].imshow(mem, interpolation='none', cmap='gray')
ax[1].imshow(fill, interpolation='none', cmap='gray')
ax[0].set_title('Thresh')
ax[1].set_title('Fill')

original

gray

smooth

filling

opencv image-processing scipy scikit-image
1个回答
0
投票

我对 scipy 不太熟悉。我尝试使用 opencv 形态学运算来完成该任务。下面是代码,注释中有解释。希望这有帮助。

import cv2
import numpy as np

#Read input image and convert to gray
image = cv2.imread('red_ball.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Apply filter and threshold image.
gray = cv2.GaussianBlur(gray, (11, 11), 0)
ret, thresholded_image = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#Apply morphological open and close operation to remove noise and close gaps
#Can read more in https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html
kernel = np.ones((7,7),np.uint8)
thresholded_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_CLOSE, kernel,iterations=5)
final_mask = cv2.morphologyEx(thresholded_image, cv2.MORPH_OPEN, kernel,iterations=5)
#Final mask of the segmented balls
cv2.imwrite("final_mask.jpg",final_mask)

#Segmented image using the final mask
segmented_image = cv2.bitwise_and(image, image, mask=final_mask)
cv2.imwrite("segmented_image.jpg",segmented_image)

输出掩码:

分割图像

© www.soinside.com 2019 - 2024. All rights reserved.