形态学操作—底帽运算

  底帽运算(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库)

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

猜你喜欢

转载自blog.csdn.net/qq_50993557/article/details/134723084