Advanced morphology is usually based on the two basic operations of corrosion and expansion. For corrosion and expansion, please refer to Corrosion and expansion in OpenCV (9).
Use the function morphologyEx in OpenCV for morphological operations
(1) Opening
(2) Closing operation
(3) Morphological Gradient
(4) Top Hat
(5) Black Hat
Opening operation
Operation is achieved by etching and then expanding the image
Able to exclude small clumps of objects (assuming the object has a bright background)
Closing operation
The closed operation is achieved by expanding the image and then corroding it
Can rule out small black holes (black areas)
Morphological Gradient
The expansion diagram differs only from the corrosion diagram
Top Hat
The original image differs only from the open one
Black Hat
The closed operation result is only different from the original image
Function morphologyEx parameter description
Void morphologyEx(InputArray SRC,// preprocess the image
OutputArray DST,// Process the resulting image
Int op,// The morphology operation to run
InputArray kernel,
Point anchor = Point(-1,-1),
int iterations =1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue()
);
Op has six forms (2–6)
Opening:MORPH_OPEN: 2 // Open operation
Closing:MORPH_CLOSE: 3 // Closing operation
Gradient:MORPH_GRADIENT: 4 // Morphological Gradient
Top Hat:MORPH_TOPHAT: 5 // Top Hat
Black Hat:MORPH_BLACKHAT: 6
code
– (void)initMat {
self.img = [UIImage imageNamed:@”123.png”];
UIImageToMat(_img, m_src);
[self morphologyEx];
}
– (void)createImageView {
CGFloat h = self.view.frame.size.width * 2 / 3;
self.imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, h)];
[self.view addSubview:_imgView1];
_imgView1.backgroundColor = [UIColor lightGrayColor];
_imgView1.image = _img;
self.imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_imgView1.frame) + 5, self.view.frame.size.width, h)];
[self.view addSubview:_imgView2];
_imgView2.backgroundColor = [UIColor lightGrayColor];
_imgView2.image = _img;
}
– (void)createSlider {
NSArray *tempArr = @[@”Operator :”, @”Element :”, @”Kernel Size :”];
CGFloat w = self.view.frame.size.width – 100;
CGFloat y = self.view.frame.size.height – 50;
for (NSInteger i = 0; i < 3; i++) {
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(90, y – 45 * i, w, 40)];
[self.view addSubview:slider];
[slider addTarget:self action:@selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
[self sliderMumValueWith:i slider:slider];
slider.tag = 1000 + i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, y – 45 * i, 80, 40)];
label.textAlignment = NSTextAlignmentRight;
[self.view addSubview:label];
label.font = [UIFont systemFontOfSize:10];
label.text = tempArr[i];
}
}
– (void)sliderMumValueWith:(NSInteger)i slider:(UISlider *)slider {
switch (i) {
case 0: {
slider.continuous = YES; // NO makes it call only once you let go
slider.minimumValue = 2;
slider.maximumValue = 6;
} break;
case 1: {
slider.continuous = YES; // NO makes it call only once you let go
slider.minimumValue = 0;
slider.maximumValue = 2;
} break;
case 2: {
slider.continuous = YES; // NO makes it call only once you let go
slider.minimumValue = 0;
slider.maximumValue = 21;
} break;
}
}
– (void)morphologyEx {
// since MORPH_X ranges from 2,3,4,5, and 6
Mat element = getStructuringElement(morph_elem, cv::Size(2 * morph_size + 1, 2 * morph_size + 1), cv::Point(morph_size, morph_size));
// run the specified morphology operation
morphologyEx(m_src, m_dst, morph_operator, element);
_imgView2.image = MatToUIImage(m_dst); //morph_operator
}
Creating a public variable
Mat m_src, m_dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
call
[self initMat];
[self createSlider];
[self createImageView];
The resources
OpenCV for iOS learning Notes (10) — Morphological transformation
More Morphological transformation