Python-OpenCV 图像处理(二十四):图像形态学(顶帽、黑帽、形态学梯度)

顶帽(Top Hat):

原图像与开运算图的区别(差值),突出原图像中比周围亮的区域

黑帽(Black Hat):

闭操作图像 - 原图像,突出原图像中比周围暗的区域

形态学梯度(Gradient):

基础梯度:基础梯度是用膨胀后的图像减去腐蚀后的图像得到差值图像,称为梯度图像也是opencv中支持的计算形态学梯度的方法,而此方法得到梯度有称为基本梯度。

内部梯度:是用原图像减去腐蚀之后的图像得到差值图像,称为图像的内部梯度。

外部梯度:图像膨胀之后再减去原来的图像得到的差值图像,称为图像的外部梯度。

 

import cv2
import numpy as np
from matplotlib import pyplot as plt

__author__ = "zxsuperstar"
__email__ = "[email protected]"

"""
顶帽、黑帽、形态学梯度
"""

def top_hat_demo(image): #顶帽
    cv2.imshow("image", image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dst = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)
    # 增加亮度
    cimage = np.array(gray.shape, np.uint8)
    cimage = 100
    dst = cv2.add(dst,cimage)
    cv2.imshow("dst", dst)


def black_hat_demo(image): #黑帽
    cv2.imshow("image", image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dst = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)
    # 增加亮度
    cimage = np.array(gray.shape, np.uint8)
    cimage = 100
    dst = cv2.add(dst,cimage)
    cv2.imshow("dst", dst)


def threshold_top_hat_demo(image):#顶帽 二值图像
    cv2.imshow("image", image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dst = cv2.morphologyEx(binary, cv2.MORPH_TOPHAT, kernel)
    cv2.imshow("dst", dst)


def threshold_black_hat_demo(image):#黑帽  二值图像
    cv2.imshow("image", image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dst = cv2.morphologyEx(binary, cv2.MORPH_BLACKHAT, kernel)
    cv2.imshow("dst", dst)


def gradient_threshold_hat_demo(image):
    cv2.imshow("image", image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    ret, binary = cv2.threshold(gray, 0 ,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
    dst = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel) #基本梯度
    cv2.imshow("dst", dst)


def gradients_threshold_hat_demo(image):
    cv2.imshow("image", image)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dm = cv2.dilate(image, kernel)
    em = cv2.erode(image, kernel)
    dst1 = cv2.subtract(image, em) #内部梯度
    dst2 = cv2.subtract(image, dm)  # 外部梯度
    cv2.imshow("internal", dst1)
    cv2.imshow("external", dst2)


if __name__ == "__main__":
    image = cv2.imread("slant1.jpg") #blue green red
    image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
    # top_hat_demo(image)
    black_hat_demo(image)
    # threshold_top_hat_demo(image)
    # threshold_black_hat_demo(image)
    # gradient_threshold_hat_demo(image)
    # gradients_threshold_hat_demo(image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


运行结果 :

顶帽                                                                                         黑帽

Python-OpenCV 图像处理(二十四):图像形态学(顶帽、黑帽、形态学梯度)_第1张图片Python-OpenCV 图像处理(二十四):图像形态学(顶帽、黑帽、形态学梯度)_第2张图片

说明:

cv2.morphologyEx()

看前三个参数就行了,后面的就用默认值

第一个参数 输入

第二个参数 操作类型

MORTH_OPEN                函数做开运算

MORTH_CLOSE              函数做闭运算

MORTH_GRADIENT       函数做形态学梯度运算

MORTH_TOPHAT            函数做顶帽运算

MORTH_BLACKHAT       函数做黑帽运算

MORTH_DILATE              函数做膨胀运算

MORTH_ERODE             函数做腐蚀运算

第三个参数  内核类型    用getStructuringElement函数得到
 

 

你可能感兴趣的:(python,图像处理)