形态学操作—底帽运算

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

相关推荐
nbsaas-boot39 分钟前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯41 分钟前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_78943 分钟前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
chao_7895 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴6 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
noravinsc6 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
ajassi20006 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
沉默媛7 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
Deng9452013148 小时前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术
2401_878624798 小时前
pytorch 自动微分
人工智能·pytorch·python·机器学习