形态学操作—底帽运算

底帽运算(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,并展示原始图像和底帽运算结果。

相关推荐
前端付豪43 分钟前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋7 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构