Python + OpenCV opening and closing operations of the image processing

Open operation: one of the important morphological image manipulation, based on the expansion operation in combination with corrosion formation; mainly used in the analysis of the binary image, gray-scale image may

= + Expandable corrosion on operation , the input structure element image +

Action: to eliminate small objects, while the larger objects smooth boundary is not significantly change the area, horizontal or vertical line extraction

Closing operations: one of the important morphological image manipulation, based on the expansion operation in combination with corrosion formation; mainly used in the analysis of the binary image, gray-scale image may

= + Expandable closing operation corrosion , structural elements of the input image +

Action: At the same time a small hole for the packed object, the object is connected adjacent the smooth boundary area does not significantly change the

python achieve

import cv2


def open_demo(image):
    print(image.shape)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imshow("binary", binary)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
    cv2.imshow("open result", binary)


def close_demo(image):
    print(image.shape)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imshow("binary", binary)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
    cv2.imshow("close result", binary)


if __name__ == "__main__":
    img = cv2.imread("image/open.jpg")
    cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
    cv2.imshow("input image", img)
    open_demo(img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Open operating results, keeping the other structural elements unchanged, eliminate small objects

 

When we change the size of the kernel, you can achieve different effects, such as we set ksize = (15,1), the image can be extracted in a horizontal straight line

 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1))
 binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

Similarly, we let ksize = (1,15), you can extract the image of the vertical line. The results are as follows

Closing operation result, Fills an enclosed area, others remain unchanged

When we change the shape of the core, but also to achieve different effects, such as extraction of the image circle

 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
 binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

The results are as follows

src parameters: input image

op parameters: the type of operation

  MORTH_OPEN                函数做开运算

  MORTH_CLOSE              函数做闭运算

  MORTH_GRADIENT       函数做形态学梯度运算

  MORTH_TOPHAT            函数做顶帽运算

  MORTH_BLACKHAT       函数做黑帽运算

  MORTH_DILATE              函数做膨胀运算

  MORTH_ERODE             函数做腐蚀运算

kernel参数 :内核类型,用getStructuringElement函数得到

Guess you like

Origin www.cnblogs.com/qianxia/p/11106090.html