dbscan 聚类并可视化简单案例

复制代码
import math
import numpy as np
from sklearn.cluster import DBSCAN
import cv2
colors_8 = [
    (255, 255, 255), # White
    (255, 0, 0),     # Red
    (0, 255, 0),     # Lime
    (0, 0, 255),     # Blue
    (255, 255, 0),   # Yellow
    (0, 255, 255),   # Cyan
    (255, 0, 255)    # Magenta
]

# 计算自定义的距离度量
def line_distance(line1, line2):
    x1, theta1 = line1
    x2, theta2 = line2
    x_dis = x2 - x1
    if abs(x_dis) < 2:
        x_dis = 0
    return math.sqrt(x_dis ** 2 + (theta1 - theta2) ** 2)

# DBSCAN 聚类
def dbscan_cluster(points, eps=4, min_samples=2):
    dbscan = DBSCAN(eps=eps, min_samples=min_samples, metric=line_distance)
    dbscan.fit(points)
    return dbscan.labels_

# 主函数
if __name__ == "__main__":

    # 给定的线段数据 (x1, y1, x2, y2)
    # lines = [(252, 65, 253, 6),  # 点1: (265, 105), (268, 0)
    #     (246, 62, 247, 7),  # 点2: (246, 62), (247, 7)
    #     (246, 60, 246, 5),  # 点3: (246, 60), (246, 5)
    # ]

     ##线下脚本
    lines = [
        (331 ,34, 333, 0),
        (328 ,32, 333, 126),
        (380 ,126, 380, 90),
        (329 ,61,331 ,119),
        (337 ,34 ,341, 80),
        (328 ,43 ,328, 74),
    ]


    # # app
    # lines = [
    #     (379 ,127 ,381, 0),
    #     (332 ,32 ,333, 0),
    #     ( 337, 35 ,341 ,77),
    #     (330 ,117, 331 ,44),
    #     (330, 68 ,331 ,15),
    # ]


    # # # 刀隔算法
    lines = [
        (328,  34 , 332 , 125),
        (334,  0 , 342,  81),
        (381 , 62,  381 , 24),
        (331 , 89 , 331 , 56),
        (337 , 9 , 342,  80),
        (379 , 5 , 379 , 61),
        (337,  35 , 341 , 82),
        (330 , 70,  330,  28),
        (329 , 80,  330,  113),
    ]
    #

    #刀尖角度未识别

    target_points= [ (84 ,  62),
  (85,   63),
  (85 ,  97),
( 64 , 125),
  (65,  125),
  (62,  126),
 (63,  12),
  (60,  127),
 (61,  127),]

    if 0:
        dbscan_labels = dbscan_cluster(target_points)
        # 打印 DBSCAN 聚类标签
        print("DBSCAN 聚类标签:", dbscan_labels)

        # 获取所有唯一的聚类标签
        unique_labels = np.unique(dbscan_labels)

        # 创建一个空白图像用于显示
        image = np.ones((600, 600, 3), dtype=np.uint8) * 255  # 白色背景

        # 绘制聚类点
        # 绘制每个点并根据聚类标签上色
        for i, label in enumerate(dbscan_labels):
            color = colors_8[label]  # 确保颜色循环
            point = target_points[i]
            cv2.circle(image, point, 5, color, -1)  # 绘制圆点,半径5,填充颜色

    if 1 :
        # 极坐标转换
        lines_polar = []
        for line in lines:
            x1, y1, x2, y2 = line
            dx = x2 - x1
            dy = y2 - y1
            if dx == 0:
                x_value_when_y_0 = x1
                if dy != 0:
                    angle_radians = math.pi / 2  # 对应90度,转换为弧度制为pi/2
                else:
                    angle_radians = 0
            else:
                slope = (y2 - y1) / (x2 - x1)
                if slope == 0:
                    x_value_when_y_0 = x1
                else:
                    x_value_when_y_0 = x1 - y1 / slope
                    m = dy / dx
                    angle_radians = math.atan(m)
            lines_polar.append((x_value_when_y_0, angle_radians))

        lines_polar = np.array(lines_polar)

        points = np.array(lines_polar)
        print("聚类后的点", points)
        dbscan_labels = dbscan_cluster(points)
        # 打印 DBSCAN 聚类标签
        print("DBSCAN 聚类标签:", dbscan_labels)

        image = np.zeros((500, 500, 3), dtype=np.uint8)

        # 获取所有唯一的聚类标签
        unique_labels = np.unique(dbscan_labels)
        # 遍历每个点并绘制
        for i, label in enumerate(dbscan_labels):
            color = colors_8[label]
            line =lines[i]
            x1, y1, x2, y2 = line
            cv2.line(image, (x1, y1), (x2, y2), color, 2)


    # 显示图像
    cv2.imshow('Clustered Points', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
相关推荐
碳酸的唐3 小时前
A* 工程实践全指南:从启发式设计到可视化与性能优化
python·神经网络
倔强青铜三6 小时前
苦练Python第64天:从零掌握多线程,threading模块全面指南
人工智能·python·面试
Q26433650237 小时前
【有源码】基于Hadoop生态的大数据共享单车数据分析与可视化平台-基于Python与大数据的共享单车多维度数据分析可视化系统
大数据·hadoop·python·机器学习·数据分析·spark·毕业设计
计算机毕业设计木哥8 小时前
计算机毕设选题推荐:基于Hadoop和Python的游戏销售大数据可视化分析系统
大数据·开发语言·hadoop·python·信息可视化·spark·课程设计
小蕾Java8 小时前
PyCharm 2025:使用图文教程!
ide·python·pycharm
至此流年莫相忘8 小时前
配置Python环境之Conda
python·conda
cooldream20098 小时前
深入解析 Conda、Anaconda 与 Miniconda:Python 环境管理的完整指南
开发语言·python·conda
B站计算机毕业设计之家8 小时前
多模态项目:Python人脸表情系统 CNN算法 神经网络+Adaboost定位+PyQt5界面 源码+文档 深度学习实战✅
python·深度学习·神经网络·opencv·yolo·计算机视觉·情绪识别
大模型真好玩9 小时前
LangGraph实战项目:从零手搓DeepResearch(一)——DeepResearch应用体系详细介绍
人工智能·python·mcp