【问题标题】:Remove bounding box outline删除边界框轮廓
【发布时间】:2020-06-17 09:08:21
【问题描述】:

我有这张图片

当我申请时

from skimage import filters
result_sobel = filters.sobel(image)

图片是

如何移除边框轮廓使其与背景融为一体?

理想情况下,输出将是黑色背景,中间是红色,没有轮廓边框。

【问题讨论】:

    标签: python image opencv image-processing scikit-image


    【解决方案1】:

    您可以在 skimage.filters.sobel 中使用掩码:

    import skimage
    
    img = skimage.io.imread('N35nj.png', as_gray=True)   
    mask = img > skimage.filters.threshold_otsu(img)
    edges = skimage.filters.sobel(img, mask=mask)
    

    让我们绘制结果:

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,5))
    ax[0].imshow(img, cmap='gray')
    ax[0].set_title('Original image')
    
    ax[1].imshow(edges, cmap='magma')
    ax[1].set_title('Sobel edges')
    
    for a in ax.ravel():
        a.axis('off')
    
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      【解决方案2】:

      这是 Python/OpenCV 中的一种方法。只需从原始灰度图像中获取轮廓。然后在红色轮廓图像上以 3 像素厚(Sobel 边缘厚度)绘制黑色。我注意到您的两个图像大小不同,并且轮廓相对于灰色框移动。这是为什么呢?

      灰色原件:

      索贝尔红边:

      import cv2
      import numpy as np
      
      # read original image as grayscale 
      img = cv2.imread('gray_rectangle.png', cv2.IMREAD_GRAYSCALE)
      hi, wi = img.shape[:2]
      
      # read edge image
      edges = cv2.imread('red_edges.png')
      
      # edges image is larger than original and shifted, so crop it to same size
      edges2 = edges[3:hi+3, 3:wi+3]
      
      # threshold img
      thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]
      
      # get contours and draw them as black on edges image
      result = edges2.copy()
      contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
      contours = contours[0] if len(contours) == 2 else contours[1]
      cv2.drawContours(result, contours, -1, (0,0,0), 3)
      
      # write result to disk
      cv2.imwrite("red_edges_removed.png", result)
      
      # display it
      cv2.imshow("ORIG", img)
      cv2.imshow("EDGES", edges)
      cv2.imshow("THRESH", thresh)
      cv2.imshow("RESULT", result)
      cv2.waitKey(0)
      


      结果:

      【讨论】:

      • 这种方法很聪明,令人印象深刻。非常感谢。回答您关于图像变化的问题,因为我截取了图像而不是直接上传。我为我的无能深表歉意。
      • 明白。感谢您解释偏移量和大小差异。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多