python - 尝试创建一个功能来标记图像
问题描述
我正在尝试对图像进行阈值处理,然后标记对象。
这是我到目前为止所拥有的:
from skimage.measure import label
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb
def segment (image, default= None):
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))
cleared = clear_border(bw)
label_image = label(cleared)
image_label_overlay = label2rgb(label_image, image=image)
return (label (image))
解决方案
如果您只想要对象的数量,您可以使用:
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.morphology import closing, square
from scipy.ndimage import label
def segment(image, threshold=None):
if threshold is None:
threshold = threshold_otsu(image)
bw = closing(image > threshold, square(3))
cleared = clear_border(bw)
return label(cleared)[1]
你不应该创建image_label_overlay
或者image_label
如果你不打算使用它们。如果您使用 Python 感知编辑器,例如带有 Python 扩展的 Visual Studio Code(它会在您第一次运行它时询问您要安装哪些语言)或 PyCharm,代码编辑器会在某些变量出现时警告您不曾用过。
推荐阅读
- django - Django Rest Framework - 无法在 DateTimeField HTML 表单上指定秒数
- python - python中的实例属性调试,如果条件不按预期工作,实例的这个属性是空的?
- java - 异常 javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(未知来源)
- c# - 如何使用 Microsoft bot 框架通过 facebook messenger (C#) 向用户发送音频
- flask - 烧瓶如何使数据库查询引用保持最新
- java - 带有类元素的java队列
- rotation - 如何让乌龟绕着一个点转
- mongodb - @query 注释在 spring boot 中不适用于 hibernate-ogm 和 mongodb
- android - 数据未更改时调用 Android ViewModel onChanged
- android - retrofit-2 - 在将图像和音频上传到 aws 服务器时获取 403 响应代码