概述
1、开运算
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
开运算:先腐蚀后膨胀, 去除噪声,去除白色小点、空洞
闭运算:先膨胀后腐蚀, 用来填充前景物体的小黑点
形态学梯度:膨胀减去腐蚀, 可以得到前景物体的轮廓
礼帽:原图减去开运算
黑帽:闭运算减去原图
使用函数morphologyEx()进行形态学其他操作
函数原型为:morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
op参数:
cv.MORPH_OPEN:开运算
cv.MORPH_CLOSE:闭运算
cv.MORPH_GRADIENT:形态学梯度
cv.MORPH_TOPHAT:礼帽
cv.MORPH_BLACKHAT:黑帽
kernel:内核或结构化内核大小
使用getStructuringElement(shape, ksize[, anchor]) -> retval获得结构化内核
shape:
cv.MORPH_RECT 矩形结构化核
cv.MORPH_ELLIPSE 椭圆结构化核
cv.MORPH_CROSS 交叉结构化核
ksize:
指定结构化核大小
anchor:默认为Point(-1,-1),内核中心点。省略时为默认值
anchor:默认为Point(-1,-1),内核中心点。省略时为默认值。
iterations:腐蚀次数。省略时为默认值1。
borderType:推断边缘类型,具体参见borderInterpolate函数。默认值为BORDER_DEFAULT 边缘值拷贝
borderValue:边缘值,具体可参见createMorphoogyFilter函数,可省略
"""
'''
结构化元素
# Rectangular Kernel
>>> cv.getStructuringElement(cv.MORPH_RECT,(5,5))
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], dtype=uint8)
# Elliptical Kernel
>>> cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
array([[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0]], dtype=uint8)
# Cross-shaped Kernel
>>> cv.getStructuringElement(cv.MORPH_CROSS,(5,5))
array([[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]], dtype=uint8)
'''
import cv2 as cv
import numpy as np
img_path = 'j_open.png'
img = cv.imread(img_path)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
def nothing(x):
pass
cv.namedWindow('OPEN')
# 创建滑动条
cv.createTrackbar('ks', 'OPEN', 2, 25, nothing)
while True:
ks = cv.getTrackbarPos('ks', 'OPEN')
if ks <= 1:
ks += 1
# Rectangular Kernel
rectKernel = cv.getStructuringElement(cv.MORPH_RECT, (ks, ks))
# Elliptical Kernel
ellKernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (ks, ks))
# Cross-shaped Kernel
crossKernel = cv.getStructuringElement(cv.MORPH_CROSS, (ks, ks))
rect = cv.morphologyEx(gray, cv.MORPH_OPEN, rectKernel)
ellip = cv.morphologyEx(gray, cv.MORPH_OPEN, ellKernel)
cross = cv.morphologyEx(gray, cv.MORPH_OPEN, crossKernel)
cv.putText(rect, 'rect:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
cv.putText(ellip, 'ellip:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
cv.putText(cross, 'cross:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
cv.putText(cross, 'cross:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
h1 = np.hstack((gray, rect))
h2 = np.hstack((ellip, cross))
cv.imshow('OPEN', np.vstack((h1, h2)))
k = cv.waitKey(100) & 0xff
if chr(k) == 'q':
break
if chr(k) == 's':
cv.imwrite('./outputs/ret_open' + str(ks) + '.jpg', rect)
cv.imwrite('./outputs/ellip_open' + str(ks) + '.jpg', ellip)
cv.imwrite('./outputs/cross_open' + str(ks) + '.jpg', cross)
2、开运算加深理解
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
# 开运算 == 先腐蚀后膨胀
kernel = np.ones((3, 3), np.uint8)
eg = np.array(range(0, 25), np.uint8).reshape(5, 5)
print('原矩阵')
print(eg)
print('*' * 60)
open_rect = cv.morphologyEx(eg, cv.MORPH_OPEN, kernel)
print('开运算结果')
print(open_rect)
print('*' * 60)
erode = cv.erode(eg, kernel)
print('腐蚀结果')
print(erode)
print('*' * 60)
dilate = cv.dilate(erode, kernel)
print('膨胀结果')
print(dilate)
print('*' * 60)
3、闭运算
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
开运算:先腐蚀后膨胀, 去除噪声,去除白色小点、空洞
闭运算:先膨胀后腐蚀, 用来填充前景物体的小黑点
形态学梯度:膨胀减去腐蚀, 可以得到前景物体的轮廓
礼帽:原图减去开运算
黑帽:闭运算减去原图
使用函数morphologyEx()进行形态学其他操作
函数原型为:morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
op参数:
cv.MORPH_OPEN:开运算
cv.MORPH_CLOSE:闭运算
cv.MORPH_GRADIENT:形态学梯度
cv.MORPH_TOPHAT:礼帽
cv.MORPH_BLACKHAT:黑帽
kernel:内核或结构化内核大小
使用getStructuringElement(shape, ksize[, anchor]) -> retval获得结构化内核
shape:
cv.MORPH_RECT 矩形结构化核
cv.MORPH_ELLIPSE 椭圆结构化核
cv.MORPH_CROSS 交叉结构化核
ksize:
指定结构化核大小
anchor:默认为Point(-1,-1),内核中心点。省略时为默认值
anchor:默认为Point(-1,-1),内核中心点。省略时为默认值。
iterations:腐蚀次数。省略时为默认值1。
borderType:推断边缘类型,具体参见borderInterpolate函数。默认值为BORDER_DEFAULT 边缘值拷贝
borderValue:边缘值,具体可参见createMorphoogyFilter函数,可省略
"""
'''
结构化元素
# Rectangular Kernel
>>> cv.getStructuringElement(cv.MORPH_RECT,(5,5))
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], dtype=uint8)
# Elliptical Kernel
>>> cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
array([[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0]], dtype=uint8)
# Cross-shaped Kernel
>>> cv.getStructuringElement(cv.MORPH_CROSS,(5,5))
array([[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]], dtype=uint8)
'''
import cv2 as cv
import numpy as np
img_path = 'j_close.png'
img = cv.imread(img_path)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
def nothing(x):
pass
cv.namedWindow('CLOSE')
# 创建滑动条
cv.createTrackbar('ks', 'CLOSE', 2, 25, nothing)
while True:
ks = cv.getTrackbarPos('ks', 'CLOSE')
if ks <= 1:
ks += 1
# Rectangular Kernel
rectKernel = cv.getStructuringElement(cv.MORPH_RECT, (ks, ks))
# Elliptical Kernel
ellKernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (ks, ks))
# Cross-shaped Kernel
crossKernel = cv.getStructuringElement(cv.MORPH_CROSS, (ks, ks))
rect = cv.morphologyEx(gray, cv.MORPH_CLOSE, rectKernel)
ellip = cv.morphologyEx(gray, cv.MORPH_CLOSE, ellKernel)
cross = cv.morphologyEx(gray, cv.MORPH_CLOSE, crossKernel)
# cv.putText(rect, 'rect:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
# cv.putText(ellip, 'ellip:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
# cv.putText(cross, 'cross:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
# cv.putText(cross, 'cross:' + str(ks), (80, 20), cv.FONT_HERSHEY_SIMPLEX, .65, (255, 255, 255), 2)
h1 = np.hstack((gray, rect))
h2 = np.hstack((ellip, cross))
cv.imshow('CLOSE', np.vstack((h1, h2)))
k = cv.waitKey(100) & 0xff
if chr(k) == 'q':
break
if chr(k) == 's':
cv.imwrite('./outputs/ret_close' + str(ks) + '.jpg', rect)
cv.imwrite('./outputs/ellip_close' + str(ks) + '.jpg', ellip)
cv.imwrite('./outputs/cross_close' + str(ks) + '.jpg', cross)
4、闭运算加深理解
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
# 闭运算 == 先膨胀后腐蚀
kernel = np.ones((3, 3), np.uint8)
eg = np.array(range(0, 25), np.uint8).reshape(5, 5)
print('原矩阵')
print(eg)
print('*' * 60)
close_rect = cv.morphologyEx(eg, cv.MORPH_CLOSE, kernel)
print('闭运算结果')
print(close_rect)
print('*' * 60)
dilate = cv.dilate(eg, kernel)
print('膨胀结果')
print(dilate)
print('*' * 60)
erode = cv.erode(dilate, kernel)
print('腐蚀结果')
print(erode)
print('*' * 60)
最后
以上就是无辜书本为你收集整理的opencv——形态学变换_开闭运算的全部内容,希望文章能够帮你解决opencv——形态学变换_开闭运算所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复