【问题标题】:Create mask or boundary from each other in Python3在 Python3 中相互创建掩码或边界
【发布时间】:2020-08-06 12:49:52
【问题描述】:

我想从带有边界的图像中获取蒙版,或者从蒙版的图像中获取边界。

这里有两张图片:

我经历了以下,我不明白那里的逻辑: link1link2link3link4

谢谢, 伊利亚斯

【问题讨论】:

  • MASK->CONTOUR:使用带有 RETR_EXTERNAL 的 findContours。 CONTOUR->MASK:使用带有 RETR_TREE 的 findContours,仅使用内部轮廓(使用 contourArea see here),然后使用 drawContours。如果您有特定的编程问题和一些代码,请尝试并回来
  • @Miki 我不确定您提供的链接中的 c++ 代码。您可以尝试使用 python3 并提供代码吗?我尝试了分水岭等,但我做不到。我想做的事:我想为任何形状(细胞、细胞核等)手工绘制边界。然后,从这些边界创建一个蒙版。或者不画边界,我先手动创建蒙版,然后画边界。
  • 我发现了这个:linkim = cv.imread('mask.png')im = cv.cvtColor(im,cv.COLOR_BGR2GRAY)idx = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[1][0]out = np.zeros_like(im)out[idx[:,0],idx[:,1]] = 255
  • 但仍然无法创建边界(第二张图像),但如果使用渐变函数,它会给出一些边界但不正确!
  • 现在这个 [link] (docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/…) 帮了大忙:

标签: python-3.x opencv mask scikit-image cv2


【解决方案1】:

从此link 可以使用以下内容;

从掩码中获取边界:

im = cv2.imread('mask.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
boundary[boundary > 0] = 255
plt.imsave("mask_boundary.png", boundary, cmap = "gray")

【讨论】:

    【解决方案2】:

    我仍然不明白逻辑,但是,可以从掩码创建边界,从边界创建掩码。

    import os
    import numpy as np
    import cv2
    from matplotlib import pyplot as plt
    
    out = "mask_boundary.png"
    inp = "mask.png"
    im = cv2.imread(inp)
    imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,127,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    tmp = np.zeros_like(im)
    boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
    plt.imsave(out, boundary, cmap = "gray")
    
    
    out = "boundary_mask.png"
    inp = "mask_boundary.png"
    im = cv2.imread(inp)
    imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,0,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    tmp = np.zeros_like(im)
    boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
    for i in range(1,len(contours)):
        boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
    
    plt.imsave(out, boundary, cmap = "gray")
    
    
    
    out = "boun_mask.png"
    inp = "boundary.png"
    im = cv2.imread(inp)
    imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,0,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    tmp = np.zeros_like(im)
    boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
    for i in range(1,len(contours)):
        boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
    
    plt.imsave(out, boundary, cmap = "gray")
    

    注意:使用上面的蒙版图像,我首先创建了边界,然后从这个边界创建了蒙版。但是,我无法将上面的所有边界都转换为遮罩,尤其是图像边缘/边界处的边界。看来边界处也一定有分界线! 我们将不胜感激!

    将上面的掩码转换为边界:

    现在将新边界转换为掩码:

    现在将上面的边界转换为掩码:

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多