图像处理项目_自定义边缘检测函数

图像处理项目

项目简介

这个项目主要关注图像处理技术,包括但不限于边缘检测、图像二值化,分辨率,灰度量化在线处理等。项目采用Python语言和OpenCV库,为图像处理提供了高效和灵活的解决方案。

功能列表

图像二值化:使用全局阈值将图像转换为二值图像。

边缘检测:提供多种度量标准,包括L1、L2和基于连通性的方法。

代码分析

图像二值化 - binarize_image

这个函数负责将输入图像转换为灰度图像(如果还不是),然后使用阈值127进行二值化。

python 复制代码
def binarize_image(image):
    if len(image.shape) == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    threshold = 127
    _, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
    return binary_image

4连通和8连通 - four_connected 和 eight_connected

这两个函数分别返回一个点的4连通和8连通邻居。

python 复制代码
def four_connected(x, y):
    return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]

def eight_connected(x, y):
    return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1),
            (x - 1, y - 1), (x + 1, y + 1), (x - 1, y + 1), (x + 1, y - 1)]

边缘检测 - detect_edges

这个函数实现了基于不同度量标准的边缘检测。

l1: 使用Canny边缘检测

l2: 使用Laplacian边缘检测

基于连通性的度量(4连通和8连通)

python 复制代码
def detect_edges(image, metric):
    binary_image = binarize_image(image)
    edges = np.zeros_like(binary_image, dtype=np.uint8)

    height, width = binary_image.shape

    inner_func = eight_connected if metric == '内部点8连通,轮廓点4连通' else four_connected
    edge_func = four_connected if metric == '内部点8连通,轮廓点4连通' else eight_connected

    for x in range(1, height - 1):
        for y in range(1, width - 1):
            pixel = binary_image[x, y]

            inner_neighbors = inner_func(x, y)
            is_inner = all(
                0 <= i < height and 0 <= j < width and binary_image[i, j] == pixel for i, j in inner_neighbors)

            if is_inner:
                continue

            edge_neighbors = edge_func(x, y)
            is_edge = any(0 <= i < height and 0 <= j < width and binary_image[i, j] != pixel for i, j in edge_neighbors)

            if is_edge:
                edges[x, y] = 255  # Set the edge pixel to white

    return edges

如何使用

Flask== 2.3.3

opencv-python== 4.6.0

numpy== 1.23.4

确保安装了Python和OpenCV。

导入项目代码。

使用detect_edges函数进行边缘检测。

联系方式

GitHub

相关推荐
智驱力人工智能11 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
XX風12 小时前
8.1 PFH&&FPFH
图像处理·算法
光泽雨13 小时前
检测阈值 匹配阈值分析 金字塔
图像处理·人工智能·计算机视觉·机器视觉·smart3
sali-tec13 小时前
C# 基于OpenCv的视觉工作流-章22-Harris角点
图像处理·人工智能·opencv·算法·计算机视觉
学电子她就能回来吗14 小时前
深度学习速成:损失函数与反向传播
人工智能·深度学习·学习·计算机视觉·github
光羽隹衡15 小时前
计算机视觉——Opencv(图像拼接)
人工智能·opencv·计算机视觉
爱打代码的小林16 小时前
基于 MediaPipe 实现实时面部关键点检测
python·opencv·计算机视觉
aaaffaewrerewrwer17 小时前
线上免费 HEIC 转 PNG 工具推荐:5 个超好用的转换网站
图像处理
深蓝电商API17 小时前
图片验证码识别:pytesseract+opencv入门
人工智能·opencv·计算机视觉·pytesseract
Sagittarius_A*19 小时前
特征检测:SIFT 与 SURF(尺度不变 / 加速稳健特征)【计算机视觉】
图像处理·人工智能·python·opencv·计算机视觉·surf·sift