python - 图像未正确标记:如何仅返回一个连接组件?
问题描述
我想预处理图像
以便只保留内部矩形段(即删除周围的背景)。但我没有得到正确的结果,显示为
为了我。
代码很简单:
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)
测试代码适用于我的另一张图像,但不适用于大多数图像。感谢您的任何帮助/建议。
解决方案
问题解决了:
这里有一个小错误:
cropped_images.append(gray[minr-pad:maxr+pad, minc-pad:maxc+pad])
应该:
cropped_images.append(img[minr-pad:maxr+pad, minc-pad:maxc+pad])
推荐阅读
- html - 有没有办法在不使用 getUI 方法的情况下在 Google 工作表中获取 Html 弹出窗口
- python - Django 3 通过管理面板删除字段项
- python - 在 Windows 10 上的 Visual Studio Code 中配置 Python
- r - 在 R 中计算分组 data.frame 期间,是否有一种优雅的方式来显示进度条?
- json - 如何在 Delphi REST Datasnap 中声明 POST 请求?
- javascript - 对象作为尝试返回 swal 的 React 子项(找到:[object Promise])无效
- groovy - Groovy - 在字符串中替换 ${}
- angular - 使用引导侧边栏模板无法正确显示角度覆盖
- python-3.x - 将浮点数写入 csv 文件,但浮点数变为字符串
- crm - Microsoft Dynamics CRM 365(内部部署)打开 Web 门户 - 错误