本文主要是介绍python实现opencv学习二十一:图像的开闭操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作用:删除图像的小的干扰项
源代码如下:
# -*- coding=GBK -*-
import cv2 as cv#图像的开闭操作
def open_image(image):gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)cv.imshow("二值化", binary)kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)cv.imshow("开操作", binary)def close_image(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, (5, 5))binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)cv.imshow("闭操作", binary)src = cv.imread("C://1.jpg")
cv.imshow("原来", src)
open_image(src)
close_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
这篇关于python实现opencv学习二十一:图像的开闭操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!