如何分割血管python opencv

pieyvz9o  于 2022-12-19  发布在  Python
关注(0)|答案(1)|浏览(143)

我正在尝试使用Python和OpenCV分割视网膜图像中的血管。以下是原始图像:

理想情况下,我希望所有血管都像这样清晰可见(不同的图像):

以下是我到目前为止所做的尝试。我取了图像的绿色通道。

img = cv2.imread('images/HealthyEyeFundus.jpg')
b,g,r = cv2.split(img)

然后,我尝试按照this article创建匹配滤波器,输出图像如下:

然后我尝试了最大熵阈值法:

def max_entropy(data):
    # calculate CDF (cumulative density function)
    cdf = data.astype(np.float).cumsum()

    # find histogram's nonzero area
    valid_idx = np.nonzero(data)[0]
    first_bin = valid_idx[0]
    last_bin = valid_idx[-1]

    # initialize search for maximum
    max_ent, threshold = 0, 0

    for it in range(first_bin, last_bin + 1):
        # Background (dark)
        hist_range = data[:it + 1]
        hist_range = hist_range[hist_range != 0] / cdf[it]  # normalize within selected range & remove all 0 elements
        tot_ent = -np.sum(hist_range * np.log(hist_range))  # background entropy

        # Foreground/Object (bright)
        hist_range = data[it + 1:]
        # normalize within selected range & remove all 0 elements
        hist_range = hist_range[hist_range != 0] / (cdf[last_bin] - cdf[it])
        tot_ent -= np.sum(hist_range * np.log(hist_range))  # accumulate object entropy

        # find max
        if tot_ent > max_ent:
            max_ent, threshold = tot_ent, it

    return threshold

img = skimage.io.imread('image.jpg')
# obtain histogram
hist = np.histogram(img, bins=256, range=(0, 256))[0]
# get threshold
th = max_entropy.max_entropy(hist)
print th

ret,th1 = cv2.threshold(img,th,255,cv2.THRESH_BINARY)

这是我得到的结果,显然没有显示所有的血管:

我也尝试过获取匹配滤波器版本的图像,并获取其索贝尔值的幅度。

img0 = cv2.imread('image.jpg',0)
sobelx = cv2.Sobel(img0,cv2.CV_64F,1,0,ksize=5)  # x
sobely = cv2.Sobel(img0,cv2.CV_64F,0,1,ksize=5)  # y
magnitude = np.sqrt(sobelx**2+sobely**2)

这会使血管弹出更多:

然后我尝试了大津阈值处理:

img0 = cv2.imread('image.jpg',0)
# # Otsu's thresholding
ret2,th2 = cv2.threshold(img0,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img0,(9,9),5)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

one = Image.fromarray(th2).show()
one = Image.fromarray(th3).show()

大津不能给予足够的结果,结果中包含噪声:

如果能帮助我成功分割血管,我将不胜感激。

q3qa4bjr

q3qa4bjr1#

几年前,我曾从事视网膜血管检测工作,有很多不同的方法:

  • 如果你不需要一个最好的结果,但是需要快速的,你可以使用定向的开口,see herehere
  • 然后你有一个使用数学形态学version here的版本。

为了获得更好的结果,以下是一些想法:

相关问题