首页 文章

在cthon中填充图像的“洞”,cv2无法正常工作

提问于
浏览
0

我试图在执行二进制阈值后填充图像中的红细胞“洞” . 当反转二进制阈值时,几乎所有红细胞都具有黑色中心 . 我想删除它们 .

示例图片:

Example image


这是我的代码:

import cv2 
from PIL import Image
import numpy as np
from scipy import ndimage
from skimage.feature import peak_local_max
from skimage.morphology import watershed

image = cv2.imread("blood_cells.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
darker = cv2.equalizeHist(gray)
ret,thresh = cv2.threshold(darker,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
newimg = cv2.bitwise_not(thresh)

im2, contours, hierarchy = cv2.findContours(newimg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    cv2.drawContours(newimg,[cnt],0,255,-1)

它奏效了 . 我用 findContours()drawContours() 填补了这些漏洞 .

但是当我尝试计算欧氏距离时,为了应用分水岭算法,我只得到52个独特的段,但是应该有更多 . 这是代码,如果它可能有用:

D = ndimage.distance_transform_edt(newimg)
localMax = peak_local_max(D, indices=False, min_distance=20, labels=thresh)
markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=thresh)
print("[INFO] {} unique segments found".format(len(np.unique(labels)) - 1))

我试图对每个细胞进行分割,但结果非常不合适 . 只有具有“孔”的细胞内部才被分割 .

第一张图显示了我的结果,第二张图显示了它应该如何:

First image shows my result, second shows how it should roughly look like
.

然后我手动填充漏洞,只是为了看看我的分段代码是否有效 - 并且它有效 . 错误应该介于我绘制轮廓的部分和我计算欧氏距离的部分之间 . 任何人都可以向我解释什么可能是错的?我很无能为力 .

1 回答

  • 2

    您的问题在于以下几行:

    labels = watershed(-D, markers, mask=thresh)
    

    你作为掩码传递了一个倒置的,未校正的阈值结果:

    Bad mask

    给你这个糟糕的细分:

    bad result

    你应该通过修正后的面具:

    labels = watershed(-D, markers, mask=newimg)
    

    Good mask

    给你你可能期望的结果:

    good result

相关问题