By thresholding the binary image can be obtained, but often will not form a complete image of the object, becomes incomplete, by morphology processing, it becomes full, or to get rid of excess pixels. Conventional morphological processing algorithms include: erosion, dilation, opening, closing operation, morphological gradient, top cap and a bottom cap arithmetic operation.
1. corrosion
Etching operation is similar to the smoothing value, also has a core, but the convolution operation is not performed, but rather, instead of the anchor position of the minimum pixel value of the pixel values of the core, which would cause dark areas in the image area increases the lighter area is a region reduced. If it is a black, white foreground binary map, it will make white foreground color of the object is small, as is corrosion of the same.
Nuclear etching operation may be not only rectangular, but may be cross-shaped and elliptical, provide OpenCV getStructuringElement () function to obtain the core with the following parameters:
= Kernel cv2.getStructuringElement (Shape, ksize, Anchor) Shape: the shape of the nuclear cv2.MORPH_RECT: Rectangular cv2.MORPH_CROSS: cruciform (rectangular cross anchor centered) cv2.MORPH_ELLIPSE: oval (elliptical rectangle inscribed ) ksize: nuclear size, rectangular width and height format (width, height) Anchor: anchor core, the default value ( - . 1 - . 1 ), i.e., the center point of the core
opencv provide Erode () function etching operation, which corresponds to the following parameters:
= DST cv2.erode (the src, Kernel, Anchor, Iterations, borderType, borderValue): the src: input matrix image object, the image is binarized kernel: nuclear etching operation can be obtained by the function getStructuringElement () Anchor: Anchor the default is ( - . 1 , - . 1 ) Iterations: number of times of the etching operation, by default. 1 borderType: boundary types, have default values borderValue: boundary value, have default values
Etching operation and effect of the code is as follows:
You can see the binary image in opencv white font smaller area, like being eroded. Note that this is black and white, if it is black and white, the effect will be the opposite, fonts, but will expand.
#coding:utf-8 import cv2 as cv img = cv.imread(r"C:\Users\Administrator\Desktop\logo.png") img_cvt = cv.cvtColor(img,cv.COLOR_BGR2GRAY) ret,img_thr = cv.threshold(img_cvt,200,255,cv.THRESH_BINARY_INV) kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,5)) dst = cv.erode(img_thr,kernel,iterations=1) cv.imshow("img",img) cv.imshow("img_thr",img_thr) cv.imshow("dst",dst) cv.waitKey(0) cv.destroyAllWindows()
2. expansion
Dilation and erosion operations on the contrary, is the pixel value of the pixel value takes the maximum value in place of the core of the anchor position, so that the image will be increased in lighter areas, dark areas decreases. If it is a black, white foreground binary map, it will make the color white foreground objects larger area, like the expansion of the same
opencv provide dilate () function expansion operation, which corresponds to the following parameters:
= DST cv2.dilate (the src, Kernel, Anchor, Iterations, borderType, borderValue) the src: input matrix image object, the image is binarized kernel: nuclear etching operation may () function obtained by getStructuringElement Anchor: anchor, the default is ( - . 1 , - . 1 ) Iterations: number of times of the etching operation, by default. 1 borderType: Category boundary borderValue: boundary value
Operation and effect of the expansion of the code as follows:
You can see the binary image in opencv white font larger area, like the expansion of the
#coding:utf-8 import cv2 as cv img = cv.imread(r"C:\Users\Administrator\Desktop\logo.png") img_cvt = cv.cvtColor(img,cv.COLOR_BGR2GRAY) ret,img_thr = cv.threshold(img_cvt,200,255,cv.THRESH_BINARY_INV) kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,5)) dst = cv.dilate(img_thr,kernel,iterations=1) cv.imshow("img",img) cv.imshow("img_thr",img_thr) cv.imshow("dst",dst) cv.waitKey(0) cv.destroyAllWindows()
3. The opening operation and closing operation, the top cap, the top cap
Opening operation: the first etching operation, after the expansion operation is mainly used to remove some of the lighter parts, the first part not etched, and then inflated.
Closing operation : first inflated operation, after etching operation, mainly used to remove some of the dark part.
Morphological gradient : dilation result minus the erosion operation result, you can get profile information.
Top hat operation : open the original image minus the result of the operation.
Bottom-hat operation : closing operation result by subtracting the original image.
Opening, closing operation, calculation top cap, a bottom cap operation, morphological gradient, OpenCV provides a unified function cv2.morphologyEx (), which corresponds to the following parameters:
= DST cv2.morphologyEx (the src, OP, Kernel, Anchor, Iterations, borderType, borderValue) the src: input matrix image object as a binary image op: Type morphological operations cv2.MORPH_OPEN opening operation and closing operation cv2.MORPH_CLOSE CV2. MORPH_GRADIENT morphological gradient cv2.MORPH_TOPHAT overcap operation cv2.MORPH_BLACKHAT bottom cap computation kernel: nuclear etching operation can be obtained by the function getStructuringElement () Anchor: anchor default ( - . 1 , - . 1 ) Iterations: number of times of the etching operation The default is 1 borderType: border species borderValue: boundary value
Using the code and the corresponding results are as follows:
#coding:utf-8 import cv2 as cv import matplotlib.pyplot as plt img = cv.imread(r"C:\Users\Administrator\Desktop\logo.png") img_cvt = cv.cvtColor(img,cv.COLOR_BGR2GRAY) ret,img_thr = cv.threshold(img_cvt,200,255,cv.THRESH_BINARY_INV) kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,5)) open = cv.morphologyEx(img_thr,cv.MORPH_OPEN,kernel,iterations=1) close = cv.morphologyEx(img_thr,cv.MORPH_CLOSE,kernel,iterations=1) gradient = cv.morphologyEx(img_thr,cv.MORPH_GRADIENT,kernel,iterations=1) tophat = cv.morphologyEx(img_thr,cv.MORPH_TOPHAT,kernel,iterations=1) blackhat = cv.morphologyEx(img_thr,cv.MORPH_BLACKHAT,kernel,iterations=1) images=[img_thr,open,close,gradient,tophat,blackhat] titles=["img_thr","open","close","gradient","tophat","blackhat"] for i in range(6): plt.subplot(2,3,i+1),plt.imshow(images[i],"gray") plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show()
4. Application Examples
There follows a Chinese picture, when we cut characters, which often need to know whether the characters underlined, to facilitate subsequent processing.
Our first thought might be to use Hough line detection algorithms, but directly detected, there will be a lot of interference. We can adopt a horizontal nuclear matrix, to corrupt fonts, only underscore the picture, then Hough line detection, such interference is small, the accuracy would be much higher. Specific implementation code and results are as follows:
#coding:utf-8 import cv2 as cv img = cv.imread(r"C:\Users\Administrator\Desktop\chinese.png") img_cvt = cv.cvtColor(img,cv.COLOR_BGR2GRAY) ret,img_thr = cv.threshold(img_cvt,100,255,cv.THRESH_BINARY) kernel = cv.getStructuringElement(cv.MORPH_RECT,(30,1)) #由于是1*30的矩阵,字体会被横向空隙的白色腐蚀掉,而下划线横向都是黑色,不会腐蚀 dst = cv.dilate(img_thr,kernel,iterations=1) #由于是白底黑字,所有进行膨胀操作来去除黑色字体 cv.imshow("img_thr",img_thr) cv.imshow("dst",dst) cv.waitKey(0) cv.destroyAllWindows()