OpenCV这么简单为啥不学——1.7、实现OpenCV自带的七种形态学转换操作
前言
计算机视觉市场巨大而且持续增长,且这方面没有标准API,如今的计算机视觉软件大概有以下三种:
1、研究代码(慢,不稳定,独立并与其他库不兼容)
2、耗费很高的商业化工具(比如Halcon, MATLAB+Simulink)
3、依赖硬件的一些特别的解决方案(比如视频监控,制造控制系统,医疗设备)这是如今的现状,而标准的API将简化计算机视觉程序和解决方案的开发,OpenCV致力于成为这样的标准API。
OpenCV致力于真实世界的实时应用,通过优化的C代码的编写对其执行速度带来了可观的提升,并且可以通过购买Intel的IPP高性能多媒体函数库(Integrated Performance Primitives)得到更快的处理速度。
故而我们选择学习OpenCV,我们来一步步的学习OpenCV。
1、erode腐蚀
腐蚀API
腐蚀 | erode | erosion = cv2.erode(src=girl_pic, kernel=kernel) | 对滑窗中的像素点按位乘,再从中取最小值点作为输出。可以去浅色噪点 | 浅色成分被腐蚀 |
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # erode 腐蚀 erosion = cv2.erode(src=girl_pic, kernel=kernel) cv2.imshow('erosion', erosion) cv2.waitKey(0) cv2.destroyAllWindows()
实例效果:
2、dilate膨胀
dilate膨胀API
膨胀 | dilate | dilation = cv2.dilate(src=girl_pic, kernel=kernel) | 对滑窗中的像素点按位乘,再从中取最大值点作为输出。可以增加浅色成分 | 浅色成分得膨胀 |
实例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # dilate 膨胀 dilation = cv2.dilate(src=girl_pic, kernel=kernel) cv2.imshow('dilation', dilation) cv2.waitKey(0) cv2.destroyAllWindows()
膨胀效果
3、morphology-open开运算
morphology-open_API
开运算 | morphology-open | opening = cv2.morphologyEx(girl_pic, cv2.MORPH_OPEN, kernel) | 先腐蚀,后膨胀,去白噪点 | 先合再开,对浅色成分不利 |
实例代码
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # open 开运算 opening = cv2.morphologyEx(girl_pic, cv2.MORPH_OPEN, kernel) cv2.imshow('opening', opening) cv2.waitKey(0) cv2.destroyAllWindows()
实例效果:
4、morphology-close闭运算
闭运算 | morphology-close | closing = cv2.morphologyEx(girl_pic, cv2.MORPH_CLOSE, kernel) | 先膨胀,后腐蚀,去黑噪点 | 先开再合,浅色成分得势 |
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # close 闭运算 closing = cv2.morphologyEx(girl_pic, cv2.MORPH_CLOSE, kernel) cv2.imshow('closing', closing) cv2.waitKey(0) cv2.destroyAllWindows()
实际效果:
5、morphology-grandient形态学梯度
形态学梯度 | morphology-grandient | gradient = cv2.morphologyEx(girl_pic, cv2.MORPH_GRADIENT, kernel) | 一幅图像腐蚀与膨胀的区别,可以得到轮廓 | 数值上解释为:膨胀减去腐蚀 |
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # gradient 形态学梯度 gradient = cv2.morphologyEx(girl_pic, cv2.MORPH_GRADIENT, kernel) cv2.imshow('gradient', gradient) cv2.waitKey(0) cv2.destroyAllWindows()
示例效果:
6、tophat礼帽
礼帽 | tophat | tophat = cv2.morphologyEx(girl_pic, cv2.MORPH_TOPHAT, kernel) | 原图像减去开运算的差 | 数值上解释为:原图像减去开运算 |
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # tophat 礼帽 tophat = cv2.morphologyEx(girl_pic, cv2.MORPH_TOPHAT, kernel) cv2.imshow('tophat', tophat) cv2.waitKey(0) cv2.destroyAllWindows()
实际效果:
7、blackhat黑帽
黑帽 | blackhat | blackhat = cv2.morphologyEx(girl_pic, cv2.MORPH_BLACKHAT, kernel) | 闭运算减去原图像的差 | 数值上解释为:闭运算减去原图像 |
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # blackhat 黑帽 blackhat = cv2.morphologyEx(girl_pic, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('blackhat', blackhat) cv2.waitKey(0) cv2.destroyAllWindows()
实际效果:
开运算+礼帽=原图
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # 开运算 opening = cv2.morphologyEx(girl_pic, cv2.MORPH_OPEN, kernel) # 礼帽 tophat = cv2.morphologyEx(girl_pic, cv2.MORPH_TOPHAT, kernel) # 拼接开运算+礼貌 open_and_tophat = cv2.add(opening, tophat) cv2.imshow('open_and_tophat', open_and_tophat) cv2.waitKey(0) cv2.destroyAllWindows()
实际效果:
闭运算 - 黑帽 = 原图
示例代码:
import cv2 import numpy as np girl_pic = cv2.imread('800_600.jpg') kernel = np.ones((5, 5), np.uint8) # 闭运算 closing = cv2.morphologyEx(girl_pic, cv2.MORPH_CLOSE, kernel) # 黑帽 blackhat = cv2.morphologyEx(girl_pic, cv2.MORPH_BLACKHAT, kernel) # close-blackhat 闭运算 - 黑帽 = 原图 close_subtract_blackhat = closing - blackhat cv2.imshow('close_sub_blackhat', close_subtract_blackhat) cv2.waitKey(0) cv2.destroyAllWindows()
实际效果: