Opencv learning notes 4 and opencv learning notes 4
Generally, more advanced morphological transformations, such as open and closed operations, morphological gradient, "Top Hat", and "Black Hat", can be combined with common corrosion expansion techniques to achieve the desired effect.
1. open operation:First corrosion and then expansion, used to eliminate small objects, separation objects at slender points, smoothing the boundaries of large objects without significantly changing the area, is to make the image more smooth, fill in small gaps.
2. Closed Operation:First expand and then corrode, can exclude small black hole (black area), is not to let the picture has a small branch stretched out.
3. Morphological Gradient (Morphological Gradient ):
The difference between the Expansion Diagram and the corrosion diagram is as follows:
Binary ImageThis operation can highlight the edge of the block (blob. We can use morphological gradients to reserve the edge contour of an object.
4. Top Hat ):
The Top Hat operation is often translated as the "Top Hat" operation. The difference between the original image and the result graph of the "open operation" described above is that the mathematical expression is as follows:
The top hat operation is often used to separate the patches that are lit near the Neighboring ones. When an image has a large background and small items are relatively regular, you can use the top hat operation to extract the background.
5. Black Hat ):
The difference between the result map and the original image when the Black Hat operation is "closed. The mathematical expression is:
After the black hat operation, the area is darker than the area around the source image contour, and this operation is related to the size of the selected core.
Therefore, the black hat operation is used to separate patches that are darker than neighboring near points.
6. API function implementation:
Void morphologyEx (InputArray src, OutputArray dst, int op, InputArraykernel, Pointanchor = Point (-1,-1), intiterations = 1, intborderType = BORDER_CONSTANT, constScalar & borderValue = morphologyDefaultBorderValue ());
- The first parameter is "src" of the InputArray type. Enter the input image, that is, the source image. Enter the object of the Mat class. The image depth should be one of the following five types: CV_8U, CV_16U, CV_16S, CV_32F, or CV_64F.
- The second parameter is OutputArray type dst, which is the output parameter of the target image and function. It must have the same size and type as the source image.
- The third parameter, op of the int type, indicates the type of morphological operation. It can be one of the following identifiers:
- MORPH_OPEN-open operation (Opening operation)
- MORPH_CLOSE-closed operation (Closing operation)
- MORPH_GRADIENT-Morphological gradient (Morphological gradient)
- MORPH_TOPHAT-"Top hat" ("Top hat ")
- MORPH_BLACKHAT-"Black hat" ("Black hat")
You can also select the CV version identifier, such as CV_MOP_CLOSE, CV_MOP_GRADIENT, CV_MOP_TOPHAT, and CV_MOP_BLACKHAT. This should be the identifier of the OpenCV1.0 series, which is the same as the above "MORPH_OPEN.
- The fourth parameter is the InputArray-type kernel, which is the kernel of morphological operations. If it is NULL, it indicates that the reference point is located in the core of the Center 3x3. We generally use the getStructuringElement function in combination with this parameter. The getStructuringElement function returns the structure elements (kernel matrix) of the specified shape and size ). We have discussed getStructuringElement in the previous article. For your convenience, write it again:
The first parameter of the getStructuringElement function represents the kernel shape. We can choose one of the following three shapes:
-
- Rectangle: MORPH_RECT
- Crossover: MORPH_CROSS
- Elliptical: MORPH_ELLIPSE
- The fifth parameter is anchor of the Point type. It has the default value (-1,-1), indicating that the anchor is located in the center.
- The sixth parameter, iterations of the int type, indicates the number of times the function is used for iteration. The default value is 1.
- The seventh parameter, borderType of the int type, is used to deduce a certain boundary pattern of the image's external pixels. Note that it has the default value BORDER _ CONSTANT.
- The eighth parameter is the borderValue of the const Scalar & type. When the boundary is constant, the boundary value has the default value morphologyDefaultBorderValue (). Generally, we do not need to worry about it. When you need to use it, you can refer to the createMorphologyFilter () function in the official documentation for a more detailed explanation.
Here to see the mess, this is the original post: http://blog.csdn.net/poem_qianmo/article/details/24599073