形态学的高级形态往往是建立在腐蚀和膨胀这两个基本操作之上,关于腐蚀和膨胀请参考OpenCV中的腐蚀和膨胀(9)
使用OpenCV中函数morphologyEx进行形态学操作
(1)开运算 (Opening)
(2)闭运算 (Closing)
(3)形态梯度 (Morphological Gradient)
(4)顶帽 (Top Hat)
(5)黑帽(Black Hat)
开运算(Opening)
开运算是通过先对图像腐蚀再膨胀实现的
能够排除小团块的物体(假设物体背景明亮)
闭运算(Closing)
闭运算是通过先对图像膨胀后再腐蚀实现的
能够排除小型黑洞(黑色区域)
形态梯度(Morphological Gradient)
膨胀图与腐蚀图只差
顶帽(Top Hat)
原图像与开运算结果的图只差
黑帽(Black Hat)
闭运算结果图与原图像只差
函数morphologyEx参数介绍
void morphologyEx( InputArray src,// 预处理图像
OutputArray dst,// 处理结果图像
int op,// 需要运行的形态学操作
InputArray kernel,
Point anchor = Point(-1,-1),
int iterations =1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue()
);
op 有六种形式 (2--6)
Opening:MORPH_OPEN: 2 // 开运算
Closing:MORPH_CLOSE: 3 // 闭运算
Gradient:MORPH_GRADIENT: 4 // 形态梯度
Top Hat:MORPH_TOPHAT: 5 // 顶帽
Black Hat:MORPH_BLACKHAT: 6 // 黑帽
代码
- (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 {
// 由于 MORPH_X的取值范围是: 2,3,4,5 和 6
Mat element = getStructuringElement(morph_elem, cv::Size(2 * morph_size + 1, 2 * morph_size + 1), cv::Point(morph_size, morph_size));
/// 运行指定形态学操作
morphologyEx(m_src, m_dst, morph_operator, element);
_imgView2.image = MatToUIImage(m_dst);//morph_operator
}
创建公共变量
Mat m_src, m_dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
调用
[self initMat];
[self createSlider];
[self createImageView];
参考资料