形态学操作—底帽运算

底帽运算(Bottom-hat transformation),也称为黑帽运算,是形态学图像处理中的一种操作。它与顶帽运算相反,通过闭运算的结果与原始图像的差异来突出图像中的暗区域特征。

原理

底帽运算通过对图像执行闭运算(先膨胀后腐蚀)来平滑图像并移除较大尺度的特征,然后将闭运算结果与原始图像进行差分,以突出图像中的暗区域特征。

作用和适用场景

  • 提取暗区域特征: 底帽运算有助于突出原始图像中的暗区域特征,如阴影、均匀区域中的细微细节等。
  • 图像增强: 可用于增强图像中的暗部细节,使其更加清晰。

数学公式

底帽运算可以表示为: BottomHat ( f , S E ) = Closing ( f , S E ) − f \text{BottomHat}(f, SE) = \text{Closing}(f, SE) - f BottomHat(f,SE)=Closing(f,SE)−f

其中, f f f 是原始图像, S E SE SE是结构元素, Closing ( f , S E ) \text{Closing}(f, SE) Closing(f,SE)表示图像 f f f的闭运算结果。

代码示例(使用Python的OpenCV库)

python 复制代码
import cv2
import numpy as np

def show_images(image):
    cv2.namedWindow('image',cv2.WINDOW_KEEPRATIO)
    cv2.imshow('image',image)
    cv2.waitKey()
    cv2.destroyAllWindows()

def bottom_hat(image):
    # 定义结构元素(这里使用一个 5x5 的正方形结构元素)
    # 定义结构元素
    kernel = np.ones((5, 5), np.uint8)
    # 应用闭运算
    closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
    # 应用底帽运算
    bottomhat = closing - image
    return bottomhat

if __name__ == '__main__':
    # 读取图像
    img = cv2.imread('cat-dog.png', flags=0)
    re_img=bottom_hat(img)
    # top_row = np.hstack((img, re_img[0]))
    # bottom_row = np.hstack((re_img[1], re_img[2])) #水平
    # combined_img = np.vstack((img, re_img))# 垂直
    combined_img=np.hstack((img,re_img))
    show_images(combined_img)

这段代码使用了OpenCV库进行底帽运算。它首先读取了一张灰度图像,然后定义了一个 (5 \times 5) 的矩形结构元素。接着,使用 cv2.morphologyEx 函数进行闭运算得到 closing 图像,最后通过图像减法得到底帽运算结果 bottomhat,并展示原始图像和底帽运算结果。

相关推荐
曲幽1 天前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户8356290780511 天前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞1 天前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪1 天前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户0332126663671 天前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫1 天前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风2 天前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽2 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic