图像分割opencv

时间:2018-06-09 16:18:04

标签: python opencv3.0 image-segmentation

我正在尝试从MIAS数据集中分割乳房X线照片图像。它似乎是非常着名的数据集,但在一些图像中很麻烦。我试图通过提取一些艺术品和胸肌来提取感兴趣的区域。即使我找到了这样做的方法,它似乎也不是一般的实现,因为它取决于灰度阈值。

原始图片

original image

最终图片

final image

我到目前为止尝试的是首先使用滤镜和直方图均衡。通过使用轮廓消除了这些伪影。然后使用灰度级本影来提取胸肌,但是在这种情况下发生,通过使用该阈值也检测到内部区域。因此,即使通过与原始图像合并检测到内部区域,我想出的也是重建。

我想要做的是创建一个更一般的想法,因为灰度级可以改变acrosss不同的图像。任何想法或建议都会受到欢迎。

# To remove minimun componensts
def remove_min_components(img):
        nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8)
        sizes = stats[1:, -1]; nb_components = nb_components - 1

        img2 = np.zeros((output.shape))

        for i in range(0, nb_components):
                if sizes[i] == max(sizes):
                        img2[output == i + 1] = 255
        plt.imshow(img2, cmap = 'gray')
        plt.show()
        return img2


img = cv2.imread(path, 0)
#histogram equalization
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img)
plt.imshow(cl1, cmap = 'gray')
plt.show()
# Otsu Binarization
ret, thr = cv2.threshold(cl1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
er = cv2.morphologyEx(thr, cv2.MORPH_OPEN, np.ones((5,5))) 
# preprocessing
kernel = np.ones((3,3),np.uint8)
op = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blurred = cv2.blur(op, (4,4))

test = remove_min_components(blurred)
test1 = (test * blurred) # to obtain the original image without artefacts
test1.astype('uint8')
plt.imshow(test1, cmap = 'gray')
check = test1.copy() # copy to keep the original test1
check.astype('uint8')
# Threshold in grayscale to eliminate pectoral muscle
check[check > 45000.] = 0.
#Binarization the previous threshold
ret, thr = cv2.threshold(check, 0, 255, cv2.THRESH_BINARY)
thr = cv2.convertScaleAbs(thr) # change scale to use function 
#to keep the biggest contour
def remove_min_components_(img):
    image, contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    mask = np.zeros(img.shape, np.uint8)
    largest_areas = sorted(contours, key=lambda x: cv2.arcLength(x,True), reverse = True)
    largest = largest_areas[0]
    cv2.drawContours(mask, [largest], 0, 255, -1)
    plt.imshow(mask, cmap = 'gray')
    plt.show()
    return mask
final = remove_min_components_(thr) # note this is the previous function
# Imagen final 
test2 = cv2.bitwise_and(test1,test1, mask=final)
plt.imshow(test2, cmap = 'gray')
plt.show()

0 个答案:

没有答案