【OpenCV:从零到一】10:形态学操作(开操作、闭操作、形态学梯度、顶帽、黑帽)

前言
这是我《OpenCV:从零到一》专栏的第十篇博客,想看跟多请戳
本文概要
开操作- open 先腐蚀后膨胀 可以去掉小的对象,假设对象是前景色,背景是黑色
闭操作- close 可以填充小的洞(fill hole),假设对象是前景色,背景是黑色
形态学梯度- Morphological Gradient 膨胀减去腐蚀 又称为基本梯度(其它还包括-内部梯度、方向梯度)
顶帽 – top hat
黑帽 – black hat
morphologyEx
案例代码
大概内容:黑帽 。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
	if (!src.data) {
		printf("could not load image...\n");
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);
	char output_title[] = "morphology demo";
	namedWindow(output_title, WINDOW_AUTOSIZE);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));//开闭操作用的算子
	morphologyEx(src, dst, MORPH_BLACKHAT, kernel);
	imshow(output_title, dst);

	waitKey(0);
	return 0;
}

运行效果:
仅示范黑帽
在这里插入图片描述
解析及注意事项

  • 这个函数比腐蚀和膨胀操作多了一个op参数,用来判断执行哪一个操作,其他参数都是一样的(甚至怀疑腐蚀和膨胀就是这个函数包装了一下而已)
  • CV_MOP_BLACKHAT在opencv4里面被改了,和之前那些单纯去掉CV_的不一样,这个还需要把morphology补全一些,写成MORPH_BLACKHAT 情况比较特殊。
  • 下面是所有op种类
    在这里插入图片描述
  • 注意这里的iteration,当他大于1的时候(比如2)执行复杂的操作的时候(比如开操作)的执行顺序是erode -> erode -> dilate -> dilate (and not erode -> dilate -> erode -> dilate).

全注释代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
	if (!src.data) {
		printf("could not load image...\n");
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);
	char output_title[] = "morphology demo";
	namedWindow(output_title, WINDOW_AUTOSIZE);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));//开闭操作用的算子
	morphologyEx(src, dst, MORPH_BLACKHAT, kernel);
	/*
	InputArray 	src,
	OutputArray 	dst,
	int 	op,//Type of a morphological operation, see MorphTypes
	InputArray 	kernel,//Structuring element. It can be created using getStructuringElement.
	Point 	anchor = Point(-1,-1),
	int 	iterations = 1,
	int 	borderType = BORDER_CONSTANT,
	const Scalar & 	borderValue = morphologyDefaultBorderValue() 	

	The number of iterations is the number of times erosion or dilatation operation will be applied. 
	For instance, an opening operation (MORPH_OPEN) with two iterations is equivalent to apply successively: 
	erode -> erode -> dilate -> dilate (and not erode -> dilate -> erode -> dilate).
	*/
	imshow(output_title, dst);

	waitKey(0);
	return 0;
}

翻译笔记
iterations 迭代
equivalent n.等值;同义词;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值