【问题标题】:Image not labeling correctly: how to return only one connected component?图像未正确标记:如何仅返回一个连接组件?
【发布时间】:2019-02-11 23:37:38
【问题描述】:

我想对图片进行预处理

只保留内部矩形段(即移除周围的背景)。但我没有得到正确的结果,显示为

对我来说。

代码很简单:

def labelim(img):
    #labeling image
    gray = rgb2gray(img) #translate rgb to gray
    val = filters.threshold_local(gray,5)
    mask = gray > val
    clean_border = segmentation.clear_border(mask)
    labeled = label(clean_border)
    signle_labeled = np.where(labeled == 0,labeled, 1)#ensure all assigned label return as 1.
    return single_labeled
def crop_img(img, labeled):    
    cropped_images = []
    pad = 20
    for region in regionprops(labeled):
        if region.area < 2000:
            continue
        minr,minc,maxr,maxc = region.bbox
        cropped_images.append(gray[minr-pad:maxr+pad, minc-pad:maxc+pad])
    for c, cropped_image in enumerate(cropped_images):
        cropim = cropped_image
    return cropim

labeled = labelim(img)
cropped_image = crop_img(img, labeled)

测试代码适用于我的另一张图片,但不适用于大多数图片。感谢您的任何帮助/建议。

【问题讨论】:

  • 我已经删除了多余的部分。谢谢你告诉我!
  • 看来你是从 0,0 开始的,所以 minc/minr 都为零(或小于 pad)。
  • ps。最后你的cropim循环没有意义
  • 您能详细说明一下吗?我将 [minr,minc] 打印为 [119L, 128L]。它确实适用于另一个类似的图像。
  • 看起来很像您刚刚从原始图像中提取了 rect 0,0->200,200,如果 minr/c 错误,这将是有意义的

标签: python image-processing image-segmentation labeling


【解决方案1】:

问题解决了:

这里有一个小错误:

cropped_images.append(gray[minr-pad:maxr+pad, minc-pad:maxc+pad])

应该是:

cropped_images.append(img[minr-pad:maxr+pad, minc-pad:maxc+pad])

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2015-11-12
    • 2013-03-28
    • 2019-05-26
    • 2021-06-18
    • 2018-05-13
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多