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

图像处理项目

项目简介

这个项目主要关注图像处理技术,包括但不限于边缘检测、图像二值化,分辨率,灰度量化在线处理等。项目采用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

相关推荐
兵慌码乱5 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
小小杨树7 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
H__Rick9 天前
自动对焦学习-3
人工智能·学习·计算机视觉
计算机科研狗@OUC9 天前
(cvpr26) AIMDepth: Asymmetric Image-Event Mamba for Monocular Depth Estimation
人工智能·深度学习·计算机视觉
qq_366566509 天前
2026最新:5款AI视频口型同步工具实测横评,视频翻译后嘴型对不上的终极解决方案
人工智能·计算机视觉·新媒体运营
梦想三三9 天前
OpenCV银行卡数字识别项目(图像预处理与字符分割)
人工智能·opencv·计算机视觉
kaikaile19959 天前
图像稀疏化分解 + 压缩感知(CS)重建 MATLAB
开发语言·计算机视觉·matlab
武子康9 天前
调查研究-180 roboflow/supervision:计算机视觉工程里的“胶水层“,为什么值得关注?
人工智能·opencv·计算机视觉·chatgpt·llm·向量化
YOLO数据集集合9 天前
无人机风电设备智能巡检 风机叶片缺陷目标检测数据集实战 | 表面腐蚀漏油识别 工业视觉质检 深度学习模型训练落地10337期
人工智能·深度学习·目标检测·计算机视觉·无人机