形态学操作—底帽运算

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

相关推荐
云程笔记17 分钟前
004.环境搭建基础篇:Python、CUDA、cuDNN、PyTorch/TensorFlow安装与版本兼容性踩坑
pytorch·python·tensorflow
知行合一。。。6 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y6 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange6 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium276 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499997 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉7 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi7 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^7 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool
宝贝儿好7 小时前
【LLM】第二章:文本表示:词袋模型、小案例:基于文本的推荐系统(酒店推荐)
人工智能·python·深度学习·神经网络·自然语言处理·机器人·语音识别