第十九天開閉操作 提取水平、垂直線
阿新 • • 發佈:2018-11-11
開閉操作都是基於先腐蝕再膨脹進行運算的, kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (**, **)) 腐蝕時候將x軸設為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_ELLIPSE, (15, 15)) 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, 15)) binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel) ##閉操作 cv.imshow("close_demo", binary) print("--------- Python OpenCV Tutorial ---------") src = cv.imread("D:/vcprojects/images/circle.png") cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.imshow("input image", src) open_demo(src) cv.waitKey(0) cv.destroyAllWindows()