天天看点

opencv 去除噪点 java,【opencv学习笔记 26开闭操作】 去噪点 去除水平线 去除竖直线...

import cv2 as cv

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)

# 结构元素 结构元素类型 核大小 如果核为(15,1)这种长条形的核,则可以提取水平或者竖直线

kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))

# 形态学操作 图像 操作(open)结构元素

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_INV | cv.THRESH_OTSU)

cv.imshow("binary", binary)

kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))

binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)

cv.imshow("close_demo", binary)

src = cv.imread("image26.jpg")

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)

cv.imshow("input image", src)

open_demo(src)

cv.waitKey(0)

cv.destroyAllWindows()