实战赢家:为何传统边缘分割方法比深度学习更有效?附源码+教学+数据

news2024/9/25 3:27:16

前言

传统的边缘分割方法,如Canny边缘检测和Sobel算子,已经在计算机视觉领域中使用了数十年。这些方法依赖于图像梯度和边缘强度来识别边缘,通过一系列精心设计的滤波器和阈值化步骤来实现高效的边缘检测。虽然这些方法较为简单,但它们的计算开销低,效果稳定,并且能够在资源有限的环境中实现快速处理。随着技术的发展,这些传统算法不断优化,并与现代技术结合,展现出在特定应用场景中优于深度学习模型的独特优势。

传统的边缘分割方法在计算机视觉的早期阶段就开始发挥关键作用。这些方法以其简洁而有效的处理流程,在许多应用中奠定了基础。最具代表性的传统边缘检测技术包括Canny边缘检测器、Sobel算子和Prewitt算子等。

Canny边缘检测器由John Canny在1986年提出,被广泛认为是经典的边缘检测方法。它通过多阶段的处理流程来提取边缘:首先应用高斯滤波器来平滑图像,减少噪声的影响;接着计算图像的梯度幅值和方向,以检测边缘;然后通过非极大值抑制技术来精确定位边缘,并利用双阈值处理来进一步确认和连接边缘。这种方法由于其精准度高、结果稳定,至今仍在很多实际应用中使用。

Sobel算子Prewitt算子则是基于图像的梯度来检测边缘的经典技术。Sobel算子使用一个卷积核来计算图像在水平和垂直方向上的梯度,从而检测边缘。这些方法的优点在于计算简单、实时性好,并且能够有效地检测到图像中的主要边缘特征。

虽然深度学习方法近年来在许多视觉任务中取得了显著的成功,但传统的边缘分割方法在某些应用场景中依然展示了其独特的优势。这些传统技术不仅计算开销低,适合资源有限的环境,还在处理特定类型的图像时展现出高效性。例如,在噪声较少、对实时性要求高的应用中,传统方法的简单性和高效性使其成为优选方案。此外,传统方法的可解释性强,使得在调试和优化过程中更具优势。

随着技术的发展,传统边缘分割方法也不断得到改进。算法优化和新技术的引入,使得这些方法在现代应用中仍能发挥重要作用。在某些情况下,它们甚至可以与深度学习模型结合,利用其优越的特性来补充深度学习技术的不足,提供更全面的解决方案。因此,传统边缘分割方法在边缘检测领域依然具有不容忽视的价值和竞争力。

深度学习的Hed等边缘分割算法,我之前做过很久,后续可以给大家提供。

我们以裂缝分割来演示

裂缝分割与斜率检测源码

话不多说,先附带源码和原数据

import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# 读取目标图像和模板图像
target_img = cv2.imread(r"C:\Users\sunhongzhe\Pictures\images\mmexport1723604959151.png")
template_img = cv2.imread(r"C:\Users\sunhongzhe\Pictures\images\Dingtalk_20240814140529.jpg")

# 转换为灰度图像
target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template_img, cv2.COLOR_BGR2GRAY)

# 获取模板图像的宽度和高度
w, h = template_gray.shape[::-1]

# 使用模板匹配
res = cv2.matchTemplate(target_gray, template_gray, cv2.TM_CCOEFF_NORMED)

# 设置阈值
threshold = 0.8
loc = np.where(res >= threshold)

x1,y1 = 0,0
# 在目标图像上绘制匹配结果
for pt in zip(*loc[::-1]):
    cv2.rectangle(target_img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)
    x1, y1 = pt[1], pt[0]

target_roi = target_gray[y1-25:y1+h+75,x1-50:x1+w+50]
# img_with_shapes = target_roi.copy()
img_with_shapes = np.zeros_like(target_roi) + 255

blurred = cv2.GaussianBlur(target_roi, (9, 9), 0)
edges = cv2.Canny(blurred, 50, 150)

contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# 提取 x 和 y
filtered_contours = []
threshold_slope = 0.5  # 自定义斜率阈值
for index, cnt in enumerate(contours):
    C= cv2.arcLength(cnt,False)
    if C <= 50:continue
    segment_length = cnt.shape[0] // 20
    for cnt_index in range(0,18):
        if cnt[cnt_index*segment_length][0][0] ==cnt[(cnt_index+1)*segment_length][0][0]: continue
        slop = abs((((cnt[cnt_index*segment_length][0][1]) -cnt[(cnt_index+1)*segment_length][0][1]) / ((cnt[cnt_index*segment_length][0][0]) -cnt[(cnt_index+1)*segment_length][0][0]))) 
        if slop > threshold_slope:
            for j in range(cnt_index*segment_length, (cnt_index+1)*segment_length):
                filtered_contours.append([cnt[j][0][0], cnt[j][0][1]])

filtered_contours = np.array(filtered_contours, dtype=np.int32).reshape(-1, 1, 2)
# filtered_points = []
# for index, cnt in enumerate(filtered_contours):
#     for point in cnt:
#         filtered_points.append([[filtered_contours[0][0], filtered_contours[0][1]]])
# filtered_points = np.array(filtered_points)
filtered_contours = filtered_contours[np.argsort(filtered_contours[:,0, 0])]
x = filtered_contours[:,0, 0]
y = filtered_contours[:,0, 1]

# 拟合多项式曲线(这里使用二次多项式)
coefficients = np.polyfit(x, y, 3)
polynomial = np.poly1d(coefficients)

# 生成拟合曲线的 x 值
# x_fit = np.linspace(np.min(x), np.max(x), 5000)
y_fit = polynomial(x).clip(y.min())
# y_fit = polynomial(x).clip(y.min())




def remove_anomalies(points, threshold=100, threshold_2=10):  
    # 第一个参数是剔除掉距离拟合曲线上远一些的一群点;
    # 第二个参数是剔除掉距离前几个
    sorted_points = points[np.argsort(points[:,0, 0])]
    x = sorted_points[:,0, 0]
    y = sorted_points[:,0, 1]

    # 拟合多项式曲线(这里使用二次多项式)
    coefficients = np.polyfit(x, y, 3)
    polynomial = np.poly1d(coefficients)

    # 生成拟合曲线的 x 值
    filtered_points = []
    y_fit = polynomial(x).clip(y.min())
    last_x = -1
    for index, x_val in enumerate(sorted_points):
        # 计算当前 x 值在拟合曲线上的 y 值
        y_fit_val = int(y_fit[index])
        
        # 获取当前 x 值的所有 y 值
        y_vals = sorted_points[sorted_points[:,0, 0] == x_val[0][0]]
        
        if len(y_vals) > 0:
            # 找到距离拟合 y 值最近的实际 y 值
            distances = np.abs(y_vals[:,0,0] - y_fit_val)
            nearest_y = y_vals[np.argmin(distances)][0][1]
            if (abs(nearest_y - y_fit_val) > threshold): 
                continue
            
            if abs(nearest_y - np.mean(y[index-3:index+3])) >= threshold_2: 
                continue
            if last_x != x_val[0][0]:  

                filtered_points.append([[int(sorted_points[index][0][0]), nearest_y]])
                last_x = x_val[0][0]
    filtered_points = np.array(filtered_points)

    result_points = []            
    for index, x_val in enumerate(filtered_points): # 斜率同向计算,点x和x-1的斜率应与x-1和x-2同向 保持单调
        x = filtered_points[:,0, 0]
        y = filtered_points[:,0, 1]
        if index > 2:
            slope_curr = (y[index] - y[index-1]) / (x[index] - x[index-1])
            slope_prev = (y[index-1] - y[index-2]) / (x[index-1] - x[index-2])
            if np.sign(slope_curr) == np.sign(slope_prev):
                result_points.append([[int(filtered_points[index][0][0]), filtered_points[index][0][1]]])
    
    result_points = np.array(result_points)
    return result_points


filtered_points = remove_anomalies(filtered_contours)
result_img = np.ones_like(img_with_shapes) * 255
cv2.drawContours(result_img, filtered_points, -1, 0, 2)


data = {'左边缘': [], '右边缘': []}

# 计算并显示斜率
def compute_and_display_slopes(img, points, interval=5):
    result_img_ = np.copy(img)
    for i in range(0, len(points) - interval, interval):  
        p1 = points[i][0]
        p2 = points[i + 2][0]  # 间隔2点取斜率
        # 计算斜率
        if p2[0] != p1[0]:
            slope = -(p2[1] - p1[1]) / (p2[0] - p1[0])
        else:
            slope = float('inf')  # 垂直线的斜率
        # 在图像上标记斜率
        midpoint = (p2[0], p2[1])
        midpoint = (int(midpoint[0]), int(midpoint[1]))  # 确保midpoint是整数
        cv2.putText(result_img_, f"{slope:.2f}", midpoint, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1, cv2.LINE_AA)
        cv2.putText(result_img, f"{slope:.2f}", midpoint, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1, cv2.LINE_AA)
    flag = True
    for i in range(0, len(points)-2):
        p1 = points[i][0]
        p2 = points[i + 2][0]
        if p2[0] != p1[0]:
            slope_ = -(p2[1] - p1[1]) / (p2[0] - p1[0])
        else:
            slope_ = float('inf')  # 垂直线的斜率
        # 将斜率按规则添加到对应列
        if slope_ < 0:
            data['左边缘'].append(slope_)
            # data['右边缘'].append(None)  # 填充 None 表示空值
        else:
            # data['左边缘'].append(None)  # 填充 None 表示空值
            data['右边缘'].append(slope_)
        # if flag and points[i ][0][1] - points[i+1][0][1] > 1: 
        #     flag = False
        #     continue
        cv2.line(result_img, tuple(points[i][0]), tuple(points[i + 1][0]), 0, 2)
        cv2.line(result_img_, tuple(points[i][0]), tuple(points[i + 1][0]), 0, 2)
    return result_img_


# 计算并绘制斜率
result_img_ = compute_and_display_slopes(target_roi, filtered_points)
# 创建 DataFrame
data['右边缘'] = data['右边缘'][::-1]
# 找出每列的最大长度
max_length = max(len(data['左边缘']), len(data['右边缘']))

# 填充较短的列
data['左边缘'].extend([None] * (max_length - len(data['左边缘'])))
data['右边缘'].extend([None] * (max_length - len(data['右边缘'])))
df = pd.DataFrame(data)

# 保存到 Excel 文件
df.to_excel('slopes.xlsx', index=False)


plt.figure(figsize=(18, 8))

plt.subplot(1, 3, 1)
plt.title('Original Points')
plt.imshow(target_roi, cmap='gray')


# plt.subplot(1, 3, 2)


# # 绘制散点图和拟合曲线
# plt.scatter(x, 800-y, color='blue', label='Data points')
# plt.plot(x, 800-y_fit, color='red', label='Fitted curve')
# plt.xlabel('X')
# plt.ylabel('Y')
# plt.title('Scatter Points and Fitted Curve')
# plt.legend()

plt.subplot(1, 3,3)
plt.title('Convex Hull')
plt.imshow(result_img, cmap='gray')

plt.subplot(1, 3, 2)
plt.title('Convex Hull')
plt.imshow(result_img_, cmap='gray')
plt.savefig("Gradient.png")
plt.show()

数据

下面将两张图放进去,改改前面几行的路径即可,第一张对应第一个路径

使用模板匹配找出预定区域,这个方法比较简单,后续咱们用不需要模板匹配的实现,因为用匹配的比较鲁棒,在几百张图里准确率高不少。

然后我们用多项式拟合与梯度阈值过滤

def remove_anomalies(points, threshold=100, threshold_2=10):  
    # 第一个参数是剔除掉距离拟合曲线上远一些的一群点;
    # 第二个参数是剔除掉距离前几个
    sorted_points = points[np.argsort(points[:,0, 0])]
    x = sorted_points[:,0, 0]
    y = sorted_points[:,0, 1]

    # 拟合多项式曲线(这里使用二次多项式)
    coefficients = np.polyfit(x, y, 3)
    polynomial = np.poly1d(coefficients)

    # 生成拟合曲线的 x 值
    filtered_points = []
    y_fit = polynomial(x).clip(y.min())
    last_x = -1
    for index, x_val in enumerate(sorted_points):
        # 计算当前 x 值在拟合曲线上的 y 值
        y_fit_val = int(y_fit[index])
        
        # 获取当前 x 值的所有 y 值
        y_vals = sorted_points[sorted_points[:,0, 0] == x_val[0][0]]
        
        if len(y_vals) > 0:
            # 找到距离拟合 y 值最近的实际 y 值
            distances = np.abs(y_vals[:,0,0] - y_fit_val)
            nearest_y = y_vals[np.argmin(distances)][0][1]
            if (abs(nearest_y - y_fit_val) > threshold): 
                continue
            
            if abs(nearest_y - np.mean(y[index-3:index+3])) >= threshold_2: 
                continue
            if last_x != x_val[0][0]:  

                filtered_points.append([[int(sorted_points[index][0][0]), nearest_y]])
                last_x = x_val[0][0]
    filtered_points = np.array(filtered_points)

    result_points = []            
    for index, x_val in enumerate(filtered_points): # 斜率同向计算,点x和x-1的斜率应与x-1和x-2同向 保持单调
        x = filtered_points[:,0, 0]
        y = filtered_points[:,0, 1]
        if index > 2:
            slope_curr = (y[index] - y[index-1]) / (x[index] - x[index-1])
            slope_prev = (y[index-1] - y[index-2]) / (x[index-1] - x[index-2])
            if np.sign(slope_curr) == np.sign(slope_prev):
                result_points.append([[int(filtered_points[index][0][0]), filtered_points[index][0][1]]])
    
    result_points = np.array(result_points)
    return result_points

最后我们计算并显示图像

# 计算并显示斜率
def compute_and_display_slopes(img, points, interval=5):
    result_img_ = np.copy(img)
    for i in range(0, len(points) - interval, interval):  
        p1 = points[i][0]
        p2 = points[i + 2][0]  # 间隔2点取斜率
        # 计算斜率
        if p2[0] != p1[0]:
            slope = -(p2[1] - p1[1]) / (p2[0] - p1[0])
        else:
            slope = float('inf')  # 垂直线的斜率
        # 在图像上标记斜率
        midpoint = (p2[0], p2[1])
        midpoint = (int(midpoint[0]), int(midpoint[1]))  # 确保midpoint是整数
        cv2.putText(result_img_, f"{slope:.2f}", midpoint, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1, cv2.LINE_AA)
        cv2.putText(result_img, f"{slope:.2f}", midpoint, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1, cv2.LINE_AA)
    flag = True
    for i in range(0, len(points)-2):
        p1 = points[i][0]
        p2 = points[i + 2][0]
        if p2[0] != p1[0]:
            slope_ = -(p2[1] - p1[1]) / (p2[0] - p1[0])
        else:
            slope_ = float('inf')  # 垂直线的斜率
        # 将斜率按规则添加到对应列
        if slope_ < 0:
            data['左边缘'].append(slope_)
            # data['右边缘'].append(None)  # 填充 None 表示空值
        else:
            # data['左边缘'].append(None)  # 填充 None 表示空值
            data['右边缘'].append(slope_)
        # if flag and points[i ][0][1] - points[i+1][0][1] > 1: 
        #     flag = False
        #     continue
        cv2.line(result_img, tuple(points[i][0]), tuple(points[i + 1][0]), 0, 2)
        cv2.line(result_img_, tuple(points[i][0]), tuple(points[i + 1][0]), 0, 2)
    return result_img_

就得到了下图,可以正确地分割出裂缝,又可以得到每个区域的斜率,美哉。

总结

这是裂缝实验的实践,后续给大家带来AI的Hed网络。传统方法不需要训练,简单实现即可,ai需要大规模样本,还是差点意思。小样本里传统方法好使,还不用训练资源,还不用标注数据。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2105001.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux malloc内存分配实现原理

目录 一、用户进程虚拟内存空间布局 二、malloc工作原理 2.1 malloc实现流程 2.1.1 brk方式申请内存 2.1.2 mmap方式分配内存 2.2 核心代码 2.3 malloc分配物理内存的时机 2.4 malloc分配的实际内存大小 三、虚拟内存与物理内存 3.1 如何建立映射 3.2 分配物理内存 …

传统CV算法——基于 SIFT 特征点检测与匹配实现全景图像拼接

全景图像拼接实现 定义 Stitcher 的类&#xff0c;用于实现两张图片的拼接。使用的技术是基于 SIFT 特征点检测与匹配&#xff0c;以及利用视角变换矩阵来对齐和拼接图像。 import numpy as np import cv2class Stitcher:#拼接函数def stitch(self, images, ratio0.75, repro…

Kubernetes 简介及部署方法

目录 1 Kubernetes 简介及原理 1.1 应用部署方式演变 1.2 容器编排应用 1.3 kubernetes 简介 1.4 K8S的设计架构 1.5 K8S 各组件之间的调用关系 1.6 K8S 的 常用名词感念 1.7 k8S的分层架构 2 K8S 集群环境搭建 2.1 k8s 中容器的管理方式 2.2 k8s中使用的几种管理容器的介绍 3 …

欧洲应用市场的特点

欧洲应用市场是一个充满活力和多样性的景观&#xff0c;其特点是复杂性和巨大的潜力。仅在27个欧盟&#xff08;EU&#xff09;国家就有5亿多人&#xff0c;该地区为希望扩大影响力的应用程序开发人员和企业提供了重要机会。然而&#xff0c;进入这个市场需要了解其独特的特征&…

幻觉问题综述

https://arxiv.org/pdf/2202.03629 分类 内在幻觉&#xff1a;生成的输出与源内容相矛盾 外部幻觉&#xff1a;生成的输出无法从源内容中验证 数据引发的幻觉&#xff08;来源不同引发分歧&#xff09; 训练和推理中的幻觉&#xff08;编码器不能很好的表征&#xff0c;解码…

【云原生-Docker】docker、docker-compose离线安装【包括dokcer、docker-compose资源下载】

资源资源下载在线下载百度网盘下载csdn下载 解压上传文件 配置系统配置配置 docker-compose安装验证 资源 资源下载 在线下载 下载地址&#xff1a;https://download.docker.com/linux/static/stable/x86_64/根据不同系统版本下载不同的docker版本在线下载&#xff1a;wget …

网络编程 0904作业

作业 1、多进程多线程并发服务器&#xff0c;再实现一遍&#xff08;重点模型&#xff09; 多进程并发服务器 多进程服务器 PIDserver.c 代码 #include <myhead.h> #define SERPORT 7777 #define SERIP "192.168.19.128" #define BACKLOG 10void hande(int…

【数据结构与算法 | 搜索二叉树篇 力扣篇】力扣530, 501

1. 力扣530&#xff1a;二叉搜索树的最小绝对差 1.1 题目&#xff1a; 给你一个二叉搜索树的根节点 root &#xff0c;返回 树中任意两不同节点值之间的最小差值 。 差值是一个正数&#xff0c;其数值等于两值之差的绝对值。 示例 1&#xff1a; 输入&#xff1a;root [4,…

【人工智能】Transformers之Pipeline(十五):总结(summarization)

​​​​​​​ 目录 一、引言 二、总结&#xff08;summarization&#xff09; 2.1 概述 2.2 BERT与GPT的结合—BART 2.3 应用场景​​​​​​​ 2.4 pipeline参数 2.4.1 pipeline对象实例化参数 2.4.2 pipeline对象使用参数 ​​​​​​​ 2.4.3 pipeline返回参…

【MATLAB源码-第260期】基于simulink的OFDM+QPSK系统仿真,采用RS编码经过瑞利信道包含信道估计输出各节点波形图以及星座图。

操作环境&#xff1a; MATLAB 2022a 1、算法描述 OFDM-QPSK系统是一种广泛应用于现代无线通信中的数字信号处理系统&#xff0c;结合了正交频分复用&#xff08;Orthogonal Frequency Division Multiplexing, OFDM&#xff09;和四相移相键控&#xff08;Quadrature Phase S…

Java字节码文件、组成、详解、分析;jclasslib插件、阿里arthas工具;Java注解

文章目录 一、字节码文件1.1 以正确的方式打开文件1.2 字节码文件的组成1.2.1 基础信息1.2.2 常量池1.2.3 方法 1.3 字节码常用工具1.4 总结 二、Java注解2.1 什么是Java注解2.2 注释和注解Annotation的区别&#xff08;掌握&#xff09;2.3 如何使用注解&#xff08;掌握&…

C语言典型例题61

《C程序设计教程&#xff08;第四版&#xff09;——谭浩强》 题目&#xff1a; 习题4.2 一个单位下设三个班组&#xff0c;每个班组人员不固定&#xff0c;需要统计每个班组的平均工资。分别输入3个班组所有职工的工资&#xff0c;当输入-1时&#xff0c;表示输入结束。输出…

常见排序方法详解(图示+方法)

一、插入排序 1.1基本思想 把待排序的记录 按其关键码值的大小逐个插入到一个已经排好序的有序序列中 &#xff0c;直到所有的记录插入完为止&#xff0c;得到 一个新的有序序列。 1.2直接插入排序 当插入第 i(i>1) 个元素时&#xff0c;前面的 array[0],array[1],…,array…

大文件上传vue插件vue-simple-uploader

https://www.cnblogs.com/xiahj/p/vue-simple-uploader.html

springboot-es(elasticsearch)搜索项目

目标界面 html页面 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><title>途牛旅游</title><link rel"stylesheet" href"https://a.amap.com/jsapi_demos/static/demo-center/css/d…

windows安装php7.4

windows安装php7.4 1.通过官网下载所需的php版本 首先从PHP官网&#xff08;https://www.php.net/downloads.php&#xff09;或者Windows下的PHP官网&#xff08;http://windows.php.net/download/&#xff09;下载Windows版本的PHP安装包。下载后解压到一个路径下。 2.配…

2024/9/4 Canlink配置介绍与常见故障排查

双击一个站进去配置&#xff0c;如果双击PLC则是PLC往外面发数据&#xff0c;双击伺服&#xff0c;则是伺服往外发数据。 例如我想读伺服的功能吗&#xff1f; 点击伺服的配置 将0b00的地址数据发给PLC&#xff08;D100&#xff09; ,寄存器长度是一个 然后下载程序即可

使用docker安装jenkins,然后使用jenkins本地发版和远程发版

使用docker安装jenkins&#xff0c;然后使用jenkins本地发版和远程发版 1、安装docker 1.安装必要的一些系统工具 sudo yum install docker-ce 2.添加软件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 3.更新…

电子行业最全【芯片标签二维码】知识剖析

电子行业最全【芯片标签二维码】知识剖析 本文为辰逸绅士小编原创&#xff0c;未经许可请勿私下复制转载 长 文 预 警 目录 ★01--------前言 ★02--------关于电子元器件协会ECIA ★03--------关于矩阵二维码 3.1--------矩阵二维码 构成 3.2--------矩阵二维码 种类 3.…

【数学分析笔记】第3章第1节 函数极限(3)

3. 函数极限与连续函数 3.1 函数极限 3.1.1 函数极限的性质 【局部有界性】若 lim ⁡ x → x 0 f ( x ) A \lim\limits_{x\to x_{0}}f(x)A x→x0​lim​f(x)A&#xff0c;则 ∃ δ > 0 , ∀ x ( 0 < ∣ x − x 0 ∣ < δ ) : m ≤ f ( x ) ≤ M \exists \delta>…