[OpenCV study notes] 5. Morphological processing

  • Commonly used morphological processing methods include: corrosion, expansion, open operation, closed operation, top hat operation, bottom hat operation, among which corrosion and expansion are the most basic methods, and other methods are produced by the combination of the two.

1. Corrosion

  • The image erosion operation is similar to the median smoothing operation, and the minimum value in the rectangular neighborhood of each position is taken as the gray value output at that position . The difference is that the neighborhood here is no longer a simple rectangular structure, but can also be an elliptical structure, a cross-shaped structure, etc.
  • Because the minimum value in each neighborhood is taken, the overall average brightness of the output image after corrosion will be lower than the original image, the area of ​​the brighter area in the image will become smaller or even disappear, and the area of ​​the darker area will increase . Image III and structure elementSSThe corrosion operation of S is recorded as:
    E = I ⊖ SE=I\ominus SE=IS
erode(src, element[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])//腐蚀操作
//src-输入矩阵
//element-结构元
//anchor-结构元的锚点
//iterations-腐蚀操作次数
//borderType-边界扩充类型
//borderValue-边界扩充值
getStructuringElement(shape, ksize[, anchor])//产生结构元
//shape-结构元形状
//		MORPH_RECT  产生矩形结构元
//		MORPH_ELLIPSEM  产生椭圆形结构元
//		MORPH_CROSS  产生十字交叉形结构元
//ksize-结构元尺寸
//anchor-结构元锚点

2. Expansion

  • Similar to the median smoothing and erosion operations, the expansion takes the maximum value in the neighborhood of each location . The overall average brightness of the expanded output image will increase compared to the original image, and the size of brighter objects in the image will become larger. Image III and structure elementSSThe expansion operation of S is recorded as:
    D = I ⊕ SD=I\oplus SD=IS
dilate(src, element[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])//膨胀操作

3. Open operation and close operation

  • Open operation : first corrode and then expand
    I ∘ S = (I ⊖ S) ⊕ SI\circ S=(I\ominus S)\oplus SIS=(IS)S
    has the functions of eliminating small areas with high brightness, separating objects at delicate points, and smoothing the boundaries of larger objects without changing their area significantly.
  • Close operation : first expansion and then corrosion
    I ∙ S = (I ⊕ S) ⊖ SI\bullet S=(I\oplus S)\ominus SIS=(IS)S
    has the functions of filling small black holes in white objects, connecting adjacent objects, the same structural element, multiple iterations, and smoothing its boundaries without changing its area significantly.
morphologyEx(src, op, element[, dst[, anchor[, iterations[, borderType[, borderValue ]]]]]))
//op-形态学处理各种运算
//		MORPH_OPEN  	开运算
//		MORPH_CLOSE     闭运算
//		MORPH_GRADIENT  形态梯度
//		MORPH_TOPHAT    顶帽运算
//		MORPH_BLACKHAT  底帽运算

4. Other morphological processing operations

  • The top hat transformation and the bottom hat transformation are based on the opening operation and the closing operation respectively.

  • Top hat transformation : the image subtracts the open operation result
    T hat (I) = I − I ∘ S T_{hat}(I)=II\circ SThat(I)=IIThe S
    open operation can eliminate the brighter area under the background, and the original image can be subtracted from the open operation result to get the brighter gray area in the original image, so it is also called the white background hat transformation, which can also correct uneven lighting.

  • Bottom hat transformation : image subtracts the closed operation result
    B hat (I) = I ∙ S − I B_{hat}(I)=I\bullet SIBhat(I)=ISI
    closed operation can delete the darker area under the higher brightness background, and subtract the closed operation result from the original image to get the darker gray area in the original image, so it is also called black hat transformation.

  • Morphological gradient
    G = I ⊕ S − I ⊖ SG=I\oplus SI\ominus SG=ISIS
    is the result of expansion minus the result of corrosion, expansion increases the area of ​​high-brightness areas, and corrosion reduces the area of ​​high-brightness areas, soG (r, c) ≥ 0 G(r,c)\geq 0G(r,c)0 , and what you get is the boundary of the object in the image.

Guess you like

Origin blog.csdn.net/weixin_44496838/article/details/102966241