labelme共边算法

labelme共边算法

效果图:可以看到 线与线之间重合

在这里插入图片描述

如果需要想自己写算法,可以参考shapely中文文档

这里附上我自己写的算法

from shapely.geometry import Polygon, mapping
from qtpy import QtCore
import copy
import cv2
import numpy as np


def rec_to_polygon(shape) :
    """
    矩形转shapely多边形对象
    """
    points = [[i.x(), i.y()] for i in shape.points]
    _x1 = points[0][0]
    _y1 = points[0][1]
    _x2 = points[1][0]
    _y2 = points[1][1]
    x1 = min(_x1, _x2)
    x2 = max(_x1, _x2)
    y1 = min(_y1, _y2)
    y2 = max(_y1, _y2)
    print([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
    _p = Polygon([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
    return _p


def merge_border(target_shape, shapes) :
    """
    共边
    target_shape shape对象 需要共边的shape
    shapes 本张图片所有标注信息
    :return:
    """
    try :
        new_shapes = []  # 一个多边形共边时  会产生多个多边形,只保存面积最大的两个
        target = Polygon([[i.x(), i.y()] for i in target_shape.points])  # 只共边出一个多边形时 修改target  否则返回new_shapes
        inters = []
        for shape in shapes :  # shapes 包含本张图片的所有标注信息
            if shape.shape_type in ["polygon", "rectangle"] and shape != target_shape :  # 过滤掉非矩形 非多边形
                if shape.shape_type == "rectangle" :  # 如果是矩形 需要转成多边形处理
                    _p = rec_to_polygon(shape)
                    if _p.intersects(target) or target.within(_p) :
                        inters.append(_p)
                    continue
                points = [[i.x(), i.y()] for i in shape.points]
                if len(points) <= 2 :
                    continue
                _p = Polygon([[i.x(), i.y()] for i in shape.points])
                if _p.intersects(target) or target.within(_p) :
                    inters.append(_p)

        for _inter in inters :
            if _inter.is_valid :
                target = target - _inter  # 核心部分 共边算法

        print(mapping(target))
        if target.type == "GeometryCollection" :  # 有线条 有多边形
            geometries = mapping(target).get('geometries')
            # 找到第一个多出来的多边形 坐标赋值给target_shape
            _add_polygons = []
            for index, geometry in enumerate(geometries) :
                if geometry["type"] == "Polygon" :
                    _p = Polygon(geometry["coordinates"][0])
                    if _p.area > 10 and len(
                            geometry["coordinates"][0][:-1]) >= 3 :  # 共边结果新增的多边形需要过滤掉小于3的面积  这里只是随便设

                        test_p = Polygon([[x, y] for x, y in geometry["coordinates"][0][:-1]])
                        if not test_p.is_valid :
                            continue
                        _add_polygons.append(test_p)
            _add_polygons.sort(key=lambda x : x.area, reverse=True)
            if _add_polygons :
                coordinates = mapping(_add_polygons[0]).get('coordinates')
                if coordinates and len(coordinates[0][:-1]) >= 3 :
                    target_shape.points = []
                    new_points = []
                    for x, y in coordinates[0][:-1] :
                        if [x, y] in new_points :
                            continue
                        else :
                            new_points.append([x, y])
                    for x, y in new_points :
                        target_shape.addPoint(QtCore.QPointF(x, y))


        elif target.type == "Polygon" :
            coordinates = mapping(target).get('coordinates')
            if coordinates :
                if len(coordinates[0][:-1]) >= 3 :
                    target_shape.points = []  # 重置
                    new_points = []
                    for x, y in coordinates[0][:-1] :
                        if [x, y] in new_points :
                            continue
                        else :
                            new_points.append([x, y])
                    for x, y in new_points :
                        target_shape.addPoint(QtCore.QPointF(x, y))
        elif target.type == "MultiPolygon" :
            MultiPolygons = mapping(target)['coordinates']
            MultiPolygons.sort(key=lambda p : Polygon(p[0]).area, reverse=True)
            polygon_coordinates = MultiPolygons[0][0]
            polygon_coordinates1 = MultiPolygons[1][0]
            print(polygon_coordinates)
            target_shape.points = []
            new_points = []
            for x, y in polygon_coordinates :
                if [x, y] in new_points :
                    continue
                else :
                    new_points.append([x, y])
            for x, y in new_points :
                target_shape.addPoint(QtCore.QPointF(x, y))

            target_shape1 = copy.deepcopy(target_shape)
            target_shape1.points = []

            new_points = []
            for x, y in polygon_coordinates1 :
                if [x, y] in new_points :
                    continue
                else :
                    new_points.append([x, y])
            for x, y in new_points :
                target_shape1.addPoint(QtCore.QPointF(x, y))
            new_shapes.append(target_shape1)
            return new_shapes
    except Exception as e :
        print(str(e))
        return None

若有不对的地方,请大佬指点

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Labelme是一个用于图像标注的工具,而YOLO是一种目标检测算法。在使用Labelme进行图像标注时,可以结合YOLO算法来实现目标的检测和标注。具体使用步骤如下: 1. 首先,需要打开Anaconda Prompt,并通过以下命令激活Labelme环境: ``` activate labelme ``` 2. 接下来,需要确保存放标签数据的文件夹与存放图片数据的文件夹对应。可以创建一个名为label的文件夹,将标签数据和图片数据分别存放在该文件夹的子文件夹中。例如,在处理标签数据时,可以提取文件夹路径为`label\label_data\train_label\xxx.jpg`和`label\label_data\val_label\xxx.jpg`,并将.jpg转换为相应的.txt格式。 3. 确保标签和图片的名称对应。可以编写一个脚本来实现名称的对应关系。可以参考下面的图片来理解文件夹路径和标签名称的对应关系。 4. 修改classes.names配置文件,这个文件用于存放目标类别的名称。根据具体需要,可以修改该文件来适应自己的目标类别。 总之,使用Labelme进行图像标注时,可以结合YOLO算法来实现目标的检测和标注。通过以上步骤,可以完成Labelme和YOLO的整合使用。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [深度学习数据标注_Lableme及标注文件的使用(以YOLO v3为例)](https://blog.csdn.net/m0_54634272/article/details/128739829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值