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

项目简介

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

功能列表

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

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

代码分析

图像二值化 - binarize_image

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

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连通邻居。

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连通)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值