Python OpenCV image processing: open close operation

Operation:

One of the important manipulation of image morphology is based on the combination of expansion and corrosion;

It is mainly used in binary image analysis, gray image can also be used;

Open operation = corrosion + expansion, input image + structural element

Function: used to eliminate small objects, separate objects at fine points, smooth the boundary of large objects without obviously changing their area. Extract horizontal or vertical lines

Closed operation:

One of the important manipulation of image morphology is based on the combination of expansion and corrosion;

It is mainly used in binary image analysis, gray image can also be used;

Open operation = expansion + corrosion, input image + structural element

Function: it is used to fill small cavities in objects, connect adjacent objects and smooth their boundaries without obviously changing their area

import cv2
import numpy as np
from matplotlib import pyplot as plt

__author__ = "zxsuperstar"
__email__ = "zxsuperstar@163.com"

"""
//Opening and closing operation
"""

def open_demo(image):
    cv2.imshow("image", image)
    # plt.subplot(131), plt.imshow(image), plt.title("image")

    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    cv2.imshow("binary", binary)
    # plt.subplot(132), plt.imshow(binary), plt.title("binary")
    """
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))  # Elliptical structure
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))  # Cross structure
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))  # Rectangle structure
    """
    # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))  # Define kernel matrix 
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,1))  #Defining kernel matrix to extract horizontal line
    binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

    # dst = cv2.erode(binary,kernel) #corrosion
    # # dst = cv2.dilate(binary, kernel)  # expand
    cv2.imshow("open",binary)
    # plt.subplot(133), plt.imshow(binary), plt.title("open")
    # plt.show()


def close_demo(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    cv2.imshow("binary", binary)
    """
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))  # Elliptical structure
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))  # Cross structure
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))  # Rectangle structure
    """
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))  #Define kernel matrix
    binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

    # dst = cv2.erode(binary,kernel) #corrosion
    # # dst = cv2.dilate(binary, kernel)  # expand
    cv2.imshow("close",binary)

if __name__ == "__main__":
    image = cv2.imread("slant1.jpg") #blue green red
    image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)

    open_demo(image)
    # close_demo(image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


Operation result:

On operation:Closed operation:

Open operation: extract horizontal or vertical lines

Posted on Sat, 30 Nov 2019 03:43:15 -0800 by larryg