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

图像处理项目

项目简介

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

相关推荐
却道天凉_好个秋1 分钟前
OpenCV(四十七):FLANN特征匹配
人工智能·opencv·计算机视觉
代码代码快快显灵32 分钟前
Windows下Anaconda安装OpenCV以及OpenCV入门
图像处理·人工智能·opencv
码农进厂打螺丝37 分钟前
Stable Diffusion 3.5 FP8:量化优化与部署实践
人工智能·计算机视觉·stable diffusion
week_泽1 小时前
1、OpenCV 特征检测入门笔记
人工智能·笔记·opencv
ergevv1 小时前
RK3588 上 OpenCV ROI 拷贝性能差异的根本原因与优化方案
opencv·计算机视觉·图像·image·clone·拷贝
week_泽2 小时前
2、OpenCV Harris角点检测笔记
人工智能·笔记·opencv
啊阿狸不会拉杆4 小时前
《数字图像处理》实验2-空间域灰度变换与滤波处理
图像处理·人工智能·机器学习·计算机视觉·数字图像处理
GitCode官方5 小时前
Qwen-Image-Edit-2509 正式上线 AtomGit AI:重新定义 AI 图像编辑体验!
人工智能·计算机视觉·atomgit
啊阿狸不会拉杆5 小时前
《数字图像处理》实验6-图像分割方法
图像处理·人工智能·算法·计算机视觉·数字图像处理
一招定胜负6 小时前
杂记:cv2.imshow显示中文乱码解决过程
python·opencv