形态学操作

l开操作- open

l闭操作- close

l形态学梯度- Morphological Gradient

l顶帽 – top hat

l黑帽 – black hat

开操作- open

先腐蚀后膨胀 可以去掉小的对象,假设对象是前景色,背景是黑色

morphologyEx(src, dest, CV_MOP_BLACKHAT, kernel);
 - Mat src – 输入图像
 - Mat dest – 输出结果
 - int OPT – CV_MOP_OPEN/ CV_MOP_CLOSE/ CV_MOP_GRADIENT / CV_MOP_TOPHAT/ CV_MOP_BLACKHAT 形态学操作类型
Mat kernel 结构元素
int Iteration 迭代次数,默认是1

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;


int main(void) {
	Mat src, dest;
	src = imread("C:/Users/Robin/Pictures/34.jpg");
	if (!src.data) {
		std::cout << "could not load image1..\n";
		return -1;
	}

	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	char output_title[] = "morphlogy demo";
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);

	//开操作
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(src, dest, CV_MOP_OPEN, kernel);
	imshow(output_title, dest);

	waitKey(0);	
	return 0;
}

闭操作-close

先膨胀后腐蚀(bin2) 可以填充小的洞(fill hole),假设对象是前景色,背景是黑色

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;


int main(void) {
	Mat src, dest;
	src = imread("C:/Users/Robin/Pictures/34.jpg");
	if (!src.data) {
		std::cout << "could not load image1..\n";
		return -1;
	}

	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	char output_title[] = "morphlogy demo";
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);

	//闭操作
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(src, dest, CV_MOP_CLOSE, kernel);
	imshow(output_title, dest);

	waitKey(0);	
	return 0;
}

形态学梯度- Morphological Gradient

膨胀减去腐蚀 又称为基本梯度(其它还包括-内部梯度、方向梯度)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;


int main(void) {
	Mat src, dest;
	src = imread("C:/Users/Robin/Pictures/34.jpg");
	if (!src.data) {
		std::cout << "could not load image1..\n";
		return -1;
	}

	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	char output_title[] = "morphlogy demo";
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);

	//梯度
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(src, dest, CV_MOP_GRADIENT, kernel);
	imshow(output_title, dest);

	waitKey(0);	
	return 0;
}

顶帽 – top hat

顶帽 是原图像与开操作之间的差值图像

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;


int main(void) {
	Mat src, dest;
	src = imread("C:/Users/Robin/Pictures/34.jpg");
	if (!src.data) {
		std::cout << "could not load image1..\n";
		return -1;
	}

	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	char output_title[] = "morphlogy demo";
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);

	//顶帽
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(src, dest, CV_MOP_TOPHAT, kernel);
	imshow(output_title, dest);

	waitKey(0);	
	return 0;
}

黑帽

黑帽是闭操作图像与源图像的差值图像

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;


int main(void) {
	Mat src, dest;
	src = imread("C:/Users/Robin/Pictures/34.jpg");
	if (!src.data) {
		std::cout << "could not load image1..\n";
		return -1;
	}

	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	char output_title[] = "morphlogy demo";
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);

	//黑帽
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(src, dest, CV_MOP_BLACKHAT, kernel);
	imshow(output_title, dest);

	waitKey(0);	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值