首页 > 解决方案 > 如何去除附着在另一个大轮廓上的小轮廓

问题描述

我正在做细胞分割,所以我正在尝试编写一个函数来删除主要轮廓周围的所有次要轮廓,以便做一个遮罩。发生这种情况是因为我加载了带有一些颜色标记的图像: 图片1

问题是当我做阈值时,它假定颜色标记之间的“框”是主轮廓的一部分。

图片2 图3

正如您在我的代码中看到的那样,我不会直接将彩色图像传递给灰色,因为红色会变成黑色,但也有其他颜色,至少 8 种,并且在每张图像中总是不同的。我有成千上万张这样的图像,其中只显示一个单元格,但在大多数情况下,总是附加外部轮廓。我的目标是找到一个函数,它为每个图像输入提供单个单元格的二进制图像,就像这样。所以我从这段代码开始:

import cv2 as cv
cell1 = cv.imread(image_cell, 0)
imgray = cv.cvtColor(cell1,cv.COLOR_BGR2HSV)
imgray = cv.cvtColor(imgray,cv.COLOR_BGR2GRAY)
ret,thresh_binary = cv.threshold(imgray,107,255,cv.THRESH_BINARY)
cnts= cv.findContours(image =cv.convertScaleAbs(thresh_binary) , mode = 
cv.RETR_TREE,method = cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
   cv.drawContours(thresh_binary,[c], 0, (255,255,255), -1)    
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3,3))
opening = cv.morphologyEx(thresh_binary, cv.MORPH_OPEN, kernel, 
iterations=2) # erosion followed by dilation

总结一下,我如何从图像 1 中获得红色轮廓?

标签: pythonimageopencvcomputer-visionscikit-image

解决方案


所以另一种方法,没有颜色范围。

我认为您的代码中有几件事不正确。首先,您在 上绘制轮廓thresh_binary,但是已经有其他单元格的外线 - 您试图摆脱的线。我认为这就是为什么你使用opening(?) 而在这种情况下你不应该使用。

要解决问题,首先要了解有关 findContours 工作原理的一些信息。findContours 开始在黑色背景上查找白色形状,然后在该白色轮廓内查找黑色形状,依此类推。这意味着将单元格中的白色轮廓thresh_binary检测为轮廓。里面是其他轮廓,包括你想要的轮廓。带有示例的文档

您应该首先只查找内部没有轮廓的轮廓。findContours 还返回轮廓的层次结构。它指示轮廓是否有“childeren”。如果它没有(值:-1),那么您查看轮廓的大小并忽略那些太小的轮廓。您也可以只寻找最大的,因为这可能是您想要的。最后,您在黑色蒙版上绘制轮廓。

结果:
在此处输入图像描述

代码:

    import cv2 as cv
    import numpy as np
    # load image as grayscale
    cell1 = cv.imread("PjMQR.png",0)
    # threshold image
    ret,thresh_binary = cv.threshold(cell1,107,255,cv.THRESH_BINARY)
    # findcontours
    contours, hierarchy = cv.findContours(image =thresh_binary , mode = cv.RETR_TREE,method = cv.CHAIN_APPROX_SIMPLE)

    # create an empty mask
    mask = np.zeros(cell1.shape[:2],dtype=np.uint8)

    # loop through the contours
    for i,cnt in enumerate(contours):
            # if the contour has no other contours inside of it
            if hierarchy[0][i][2] == -1 :
                    # if the size of the contour is greater than a threshold
                    if  cv2.contourArea(cnt) > 10000:
                            cv.drawContours(mask,[cnt], 0, (255), -1)    
    # display result
    cv2.imshow("Mask", mask)
    cv2.imshow("Img", cell1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

注意:我使用了您上传的图像,您的图像可能像素少得多,因此轮廓区域更小
注2:enumerate循环遍历轮廓,并为每个循环返回轮廓和索引


推荐阅读