一 定義
1 開操作: 是圖像的輪廓變得光滑,斷開的較窄的狹頸和消除細的突出物.
使結構元B對集合A進行開操作,定義為:


集合A
原始圖片
2 幾何解釋
1)開操作的何解釋
A○B的邊界由B中的點建立
當B在A的邊界內側滾動時,B所能到達的A的邊界的最遠點。
(2)閉操作的幾何解釋
A•B的邊界由B中的點建立
B在A的邊界外側滾動
滿足〖(B)〗_z⋂A≠”Ø” 的所有點的集合
3 相關函數
morphologyEx
函數原型
morphologyEx( src, op, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None )
參數介紹
. @param src Source image. The number of channels can be arbitrary. The depth should be one of
. CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
來源圖片。通道數可以是任意的。深度應該是其中之一 。 CV_8U,CV_16U,CV_16S,CV_32F或CV_64F。
. @param op Type of a morphological operation, see #MorphTypes
形態操作的類型 cv.MORPH_OPEN, cv.MORPH_CLOSE
. @param kernel Structuring element. It can be created using #getStructuringElement.
內核(element)
. @param anchor Anchor position with the kernel. Negative values mean that the anchor is at the
. kernel center.
用內核錨定位置。負值意味着錨點位於 。核心中心。
. @param iterations Number of times erosion and dilation are applied.
侵蝕和擴張的次數。
. @param borderType Pixel extrapolation method, see #BorderTypes
像素外推方法,
. @param borderValue Border value in case of a constant border. The default value has a special
. meaning.
邊界不變的邊界值。默認值有一個特殊值 。含義。
getStructuringElement
形態學處理的核心就是定義結構元素,在OpenCV-Python中,可以使用其自帶的getStructuringElement函數,也可以直接使用NumPy的ndarray來定義一個結構元素。首先來看用getStructuringElement函數定義一個結構元素:
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
這就定義了一個5×5的十字形結構元素,如下:
也可以用NumPy來定義結構元素,如下:
NpKernel = np.uint8(np.zeros((5,5))) for i in range(5): NpKernel[2, i] = 1 #感謝chenpingjun1990的提醒,現在是正確的 NpKernel[i, 2] = 1
這兩者方式定義的結構元素完全一樣:
[[0 0 1 0 0] [0 0 1 0 0] [1 1 1 1 1] [0 0 1 0 0] [0 0 1 0 0]]
用OpenCV-Python內置的常量定義橢圓(MORPH_ELLIPSE)和十字形結構(MORPH_CROSS)元素要簡單一些,如果定義矩形(MORPH_RECT)和自定義結構元素,則兩者差不多。
原文:https://blog.csdn.net/sunny2038/article/details/9137759
4 實驗結果展示
先開操作再閉操作,組成形態學濾波器。
a 圖是受噪聲污染的指紋的二值圖像,噪聲為黑色背景上的亮元素和亮指紋部分的暗元素
b圖是使用的結構元
c圖是使用結構元素對圖a腐蝕的結果:背景噪聲消除了,指紋中的噪聲尺寸增加
d圖是使用結構元素對圖c膨脹的結果:包含於指紋中的噪聲分量的尺寸被減小或被完全消除,帶來的問題是:在指紋紋路間產生了新的間斷
e圖是對圖d膨脹的結果,圖d的大部分間斷被恢復,但指紋的線路變粗了
f圖是對圖e腐蝕的結果,即對圖d中開操作的閉操作。最后結果消除了噪聲斑點
缺點:指紋線路還是有斷點,可以通過加入限制性條件解決。
5 圖片腐蝕
代碼
# coding=utf-8 import cv2 import numpy as np img = cv2.imread('fengjing.jpg', 0) # OpenCV定義的結構元素 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # 腐蝕圖像 eroded = cv2.erode(img, kernel) # 顯示腐蝕后的圖像 cv2.imshow("Eroded Image", eroded) # 膨脹圖像 dilated = cv2.dilate(img, kernel) # 顯示膨脹后的圖像 cv2.imshow("Dilated Image", dilated) # 原圖像 cv2.imshow("Origin", img) # NumPy定義的結構元素 NpKernel = np.uint8(np.ones((3, 3))) Nperoded = cv2.erode(img, NpKernel) # 顯示腐蝕后的圖像 cv2.imshow("Eroded by NumPy kernel", Nperoded) cv2.waitKey(0) cv2.destroyAllWindows()
效果展示
六 圖像的開閉操作
代碼
import cv2 as cv def open_image(image): '圖像開操作' gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY) # res=cv.bitwise_not(gray) #圖像二值化 ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow('binary',binary) #獲取形態學結構kernel,采用的形態學方式MORPH_RECT kernel=cv.getStructuringElement(cv.MORPH_RECT,(5,5)) #圖像的開操作 binary=cv.morphologyEx(binary,cv.MORPH_OPEN,kernel) cv.imshow('open',binary) def close_image(image): '圖像閉操作' gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY) # res=cv.bitwise_not(gray) ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU) kernel=cv.getStructuringElement(cv.MORPH_RECT,(5,5)) #操作函數morphologyEx binary=cv.morphologyEx(binary,cv.MORPH_CLOSE,kernel) cv.imshow('close',binary) src = cv.imread("fengjing.jpg") cv.imshow("原來", src) open_image(src) close_image(src) cv.waitKey(0) cv.destroyAllWindows()
效果展示
閉運算用來連接被誤分為許多小塊的對象,而開運算用於移除圖像噪音形成的斑點.因此,某些情況下可以連續運用這兩種運算。如對一副二值圖連續使用閉運算和開運算,將獲得圖像中的主要對象。同樣,如果想消除圖像中的噪聲(即圖像中的“小點”),也可以對圖像先用開運算后用閉運算,不過這樣也會消除一些破碎的對象。