Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Python OpenCV 365 day learning plan, enter the graphics realm with eraser. This blog is the 39th in the series.

Foundation of basic knowledge

The open and close operation of image is one of the important operations in morphology. It is formed based on the combination of corrosion and expansion operation, and can be used in binary image or gray image.

Open and close operation

Since these two operations are so similar, we’ll just go through them together.

Open operation = first corrosion, then expansion, function: eliminate small objects in the image, separate objects. Delete the small interference block of binary image to reduce the problem of too many noise points after image binarization. To sum up: eliminate small interference areas in the image.

Before operation = expand, then corrode, action: Fill the small holes in the foreground object, erase the small black spots on the foreground object. It boils down to filling small enclosed areas.

The function prototype

The function prototype of the open and close operation is as follows:

dst = cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Copy the code

The parameters are described as follows:

  • src: Picture to be operated;
  • op: open and close operation selection;
  • kernerl: This parameter is called filter (KerNAL) in filtering and structural element in morphology. Structural element is made up of a special shape, such as line, rectangle, circle, etc.

The value of op is as follows:

  • cv2.MORPH_OPEN: Open operation;
  • cv2.MORPH_CLOSE: Closed operation (close);
  • cv2.MORPH_GRADIENT: Morphological gradient (Morph-grad), which can highlight the edge of blob and retain the edge contour of the object;
  • cv2.MORPH_TOPHAT: top-hat, which will highlight the part brighter than the original outline;
  • cv2.MORPH_BLACKHAT: Black-hat, which will highlight areas darker than the original outline.

Let’s take a look at the test code:

import cv2 as cv
import numpy as np

# open operation
def open(image) :

    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0.255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5.5))
    dst = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open", dst)

# close operation
def close(image) :

    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0.255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5.5))
    dst = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)
    cv.imshow("close", dst)


src = cv.imread("./font.png")

cv.imshow("src", src)
open(src)
close(src)

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

Cv. THRESH_BINARY_INV = cv.THRESH_BINARY_INV = cv.THRESH_BINARY_INV = cv.THRESH_BINARY_INV = cv.THRESH_BINARY_INV

If you want the effect to be more obvious, you can add some noise to the original image, and then use the on operation to remove it.

For example, the convolution kernel size implementation in the open operation can be modified to achieve the following effect.

# open operation
def open(image) :

    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0.255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (13.13))
    dst = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open", dst)
Copy the code

If the open and close operation is directly applied to the color image, the effect like oil painting can be obtained, as follows:

# open operation
def open(image) :

    # gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    # ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (11.11))
    dst = cv.morphologyEx(image, cv.MORPH_OPEN, kernel)
    cv.imshow("open", dst)
Copy the code

Open operation for color images, some small white areas will be filled with surrounding colors, the overall image will be blurred.

The principle of open and closed operation is briefly summarized: open operation, expand the black area, reduce the white area, ratio operation, reduce the black area, expand the white area.