I won’t say much nonsense, everyone should just look at the code~
# coding=utf-8import cv2
import numpy as np
img=cv2.imread('timg.jpeg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
# Create a rectangular structural unit
g=cv2.getStructuringElement(cv2.MORPH_RECT,(9,9))
# Morphological processing,Open operation
img_open=cv2.morphologyEx(img,cv2.MORPH_OPEN,g)
img_hat=img-img_open
cv2.imshow('img',img)
# cv2.imshow('erode',edge_dilate)
cv2.imshow('img_open',img_open)
cv2.imshow('img_open_',img_hat)
cv2.waitKey(0)
cv2.destroyAllWindows()
Supplementary knowledge: python image opening and closing operations
Open operation and close operation are to process corrosion and expansion in a certain order. But the two are not reversible, that is, the original image cannot be obtained by first opening and then closing.
The close operation is used to connect objects that have been mistakenly divided into many small pieces, and the open operation is used to remove the spots formed by image noise.
The closed operation code is as follows:
import cv2
def closeopration(img):
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
iClose = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)return iClose
image = cv2.imread('t.png')print(image.shape)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
iClose = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
cv2.imshow('image', image)
cv2.imshow('iClose', iClose)
cv2.waitKey(0)
The above Python-openCV open operation example is all the content shared by the editor, I hope to give you a reference.