膨胀
彩图和二值图都可进行膨胀腐蚀
import cv2 as cv
import numpy as np
#腐蚀
def erode_demo(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)# cv.THRESH_BINARY_INV 二值图取反
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))#结构元素支持rect矩形 cross十字交叉 ellipse椭圆形 15越大腐蚀越严重
dst = cv.erode(binary, kernel)
cv.imshow("erode_demo", dst)
#膨胀
def dilate_demo(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
dst = cv.dilate(binary, kernel)
cv.imshow("dilate_demo", dst)
print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("../opencv-python-img/lena.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
#对二值图进行膨胀腐蚀
erode_demo(src)
#dilate_demo(src)
#对彩图进行腐蚀膨胀
# kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
# dst = cv.erode(src, kernel)
# cv.imshow("result", dst)
cv.waitKey(0)
cv.destroyAllWindows()
开操作
闭操作
开闭操作作用:
开操作和腐蚀区别(同样是去除噪点):开操作去除噪点的同时,尽量保留其它结构元素不变。
闭操作和膨胀区别:填充闭合区域,其它结构元素不变。
kernel为(5,5)的开操作结果:
kernel 为(15,1)的开操作结果:
kernel 为(1,15)的开操作结果:
kernel 为(1,15)的闭操作结果:
kernel为(15,1)的闭操作:
import cv2 as cv
import numpy as np
def open_demo(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,15))#(15,1)提取水平线(1,15)提取竖直线 rect矩形 cross十字交叉 ellipse椭圆形
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
cv.imshow("open-result", binary)
def close_demo(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 1))
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)
cv.imshow("close_demo", binary)
print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("../opencv-python-img/morph01.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open_demo(src)
#close_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
顶帽 tophat
黑帽blackhat
灰度图 顶帽操作结果
灰度图 图像增强后的结果:理论上结果更清晰些
灰度图 黑帽操作结果:
二值图 顶帽操作:
二值图 黑帽操作
import cv2 as cv
import numpy as np
#顶帽tophat操作
def top_hat_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel) #直接显示dst不清楚
cimage = np.array(gray.shape, np.uint8)#由于直接显示dst不清楚,进行图像增强。增加常量,大小是gray.shape
cimage = 120;
dst = cv.add(dst, cimage)
cv.imshow("tophat", dst)
#黑帽操作
def black_hat_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
dst = cv.morphologyEx(gray, cv.MORPH_BLACKHAT, kernel)
cimage = np.array(gray.shape, np.uint8)#由于直接显示dst不清楚,进行图像增强。增加常量,大小是gray.shape
cimage = 120;
dst = cv.add(dst, cimage)
cv.imshow("black_hat", dst)
#对二值图进行顶帽、黑帽、基本梯度操作
def hat_binary_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
#dst = cv.morphologyEx(binary, cv.MORPH_TOPHAT, kernel)# (15,15)
#dst = cv.morphologyEx(binary, cv.MORPH_BLACKHAT, kernel)# (15,15)
#dst = cv.morphologyEx(binary, cv.MORPH_GRADIENT, kernel)# (3,3)
cv.imshow("tophat", dst)
#内梯度 外梯度操作
def gradient2_demo(image):
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dm = cv.dilate(image, kernel)
em = cv.erode(image, kernel)
dst1 = cv.subtract(image, em) # internal gradient 内梯度
dst2 = cv.subtract(dm, image) # external gradient 外梯度
cv.imshow("internal", dst1)
cv.imshow("external", dst2)
print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("../opencv-python-img/morph.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
top_hat_demo(src)
# black_hat_demo(src)
# hat_binary_demo(src)
# gradient2_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()