Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像标记不正确:如何仅返回一个连接的组件?_Python_Image Processing_Image Segmentation_Labeling - Fatal编程技术网

Python 图像标记不正确:如何仅返回一个连接的组件?

Python 图像标记不正确:如何仅返回一个连接的组件?,python,image-processing,image-segmentation,labeling,Python,Image Processing,Image Segmentation,Labeling,我想对图像进行预处理 这样只保留内部矩形段(即移除周围背景)。但是我没有得到正确的结果,结果显示为 对我来说 代码非常简单: def labelim(img): #labeling image gray = rgb2gray(img) #translate rgb to gray val = filters.threshold_local(gray,5) mask = gray > val clean_border = segmentation.

我想对图像进行预处理

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

对我来说

代码非常简单:

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)
def标签(img):
#标记图像
灰色=rgb2gray(img)#将rgb转换为灰色
val=过滤器。阈值_局部(灰色,5)
遮罩=灰色>val
清除边框=分段。清除边框(遮罩)
标签=标签(干净的边框)
signle_labeled=np。其中(labeled==0,labeled,1)#确保所有分配的标签返回为1。
返回标记为
def作物图像(图像,标签):
裁剪的_图像=[]
pad=20
对于regionprops中的区域(已标记):
如果region.area<2000:
持续
minr,minc,maxr,maxc=region.bbox
裁剪的_图像。追加(灰色[minr-pad:maxr+pad,minc-pad:maxc+pad])
对于c,枚举中的裁剪_图像(裁剪_图像):
cropim=裁剪的图像
返回cropim
标签=labelim(img)
裁剪图像=裁剪图像(图像,带标签)
测试代码适用于我的另一幅图像,但不适用于大多数图像。感谢您的帮助/建议。

问题已解决:

这里有一个小错误:

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

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

我已经把多余的部分去掉了。谢谢你让我知道!看起来您是从0,0开始的,所以minc/minr都是零(或者小于pad)。你最后的cropim循环没有什么意义你能详细解释一下吗?我将[minr,minc]打印为[119L,128L]。它对另一个类似的图像也起了作用。它看起来很像你刚刚从原始图像中提取了rect 0,0->200200,如果minr/c是错误的,这是有意义的