形态学 ??形态学处理包括:开运算、闭运算、形态学梯度、顶帽、黑帽、击中或击不中变换。通过函数 cv::morphologyEx实现。
文章图片
??开运算Opening
??dst=open(src,element)=dilate(erode(src,element))
文章图片
文章图片
??闭运算Closing
??dst=close(src,element)=erode(dilate(src,element))
文章图片
文章图片
??形态学梯度Morphological Gradient
??dst=morphgrad(src,element)=dilate(src,element)?erode(src,element)
文章图片
??顶帽Top Hat
??dst=tophat(src,element)=src?open(src,element)
文章图片
??黑帽Black Hat
??dst=blackhat(src,element)=close(src,element)?src
文章图片
??击中或击不中变换Hit-or-Miss
文章图片
代码示例
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"using namespace cv;
Mat src, dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int max_operator = 4;
int max_elem = 2;
int max_kernel_size = 21;
char* window_name = "Morphology";
void Morphology_Operations(int, void*);
int main(int argc, char** argv)
{
String imageName("../data/baboon.jpg");
src = https://www.it610.com/article/imread(imageName, IMREAD_COLOR);
if (src.empty()) { return -1;
}namedWindow(window_name, WINDOW_AUTOSIZE);
createTrackbar("Operator:\n 0:Opening 1:Closing\n 2:Gradient 3:Top Hat \n 4:Black Hat", window_name,
&morph_operator, max_operator, Morphology_Operations);
createTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
&morph_elem, max_elem, Morphology_Operations);
createTrackbar("Kernel size:\n 2n +1", window_name,
&morph_size, max_kernel_size, Morphology_Operations);
Morphology_Operations(0, 0);
waitKey(0);
return 0;
}void Morphology_Operations(int, void*)
{
int operation = morph_operator + 2;
// 见开头的表,MORPH_X : 2,3,4,5 and 6
Mat element = getStructuringElement(morph_elem,
Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
morphologyEx(src, dst, operation, element);
// 形态学处理
imshow(window_name, dst);
}
运行结果
原图像
文章图片
开运算、闭运算
文章图片
文章图片
形态学梯度
文章图片
顶帽、黑帽
文章图片
文章图片
击中或击不中
#include
#include
#include using namespace cv;
int main()
{
Mat input_image = (Mat_(8, 8) <<
0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 0, 0, 0, 255,
0, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 0, 255, 0, 0,
0, 0, 255, 0, 0, 0, 0, 0,
0, 0, 255, 0, 0, 255, 255, 0,
0, 255, 0, 255, 0, 0, 255, 0,
0, 255, 255, 255, 0, 0, 0, 0);
Mat kernel = (Mat_(3, 3) <<
0, 1, 0,
1, -1, 1,
0, 1, 0);
Mat output_image;
morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
// 形态学处理:hit or missconst int rate = 50;
// 放大显示
kernel = (kernel + 1) * 127;
kernel.convertTo(kernel, CV_8U);
resize(input_image, input_image, Size(), rate, rate, INTER_NEAREST);
// 原图像
imshow("Original", input_image);
resize(kernel, kernel, Size(), rate, rate, INTER_NEAREST);
// 掩模
imshow("kernel", kernel);
resize(output_image, output_image, Size(), rate, rate, INTER_NEAREST);
// 击中或击不中
imshow("Hit or Miss", output_image);
waitKey(0);
return 0;
}
运行结果
文章图片
文章图片
文章图片
【图像处理|【OpenCV】形态学处理】
推荐阅读
- 人脸识别|【人脸识别系列】| 实现自动化妆
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- opencv|图像处理之椒盐噪声的添加与去除
- 人脸识别|【人脸识别系列】| 实现人脸截图保存并编写128维特征向量
- opencv|网络爬虫入门练习
- OpenCV|【OpenCV 完整例程】89. 带阻滤波器的传递函数
- OpenCV|【OpenCV 完整例程】90. 频率域陷波滤波器
- OpenCV|【OpenCV 完整例程】22. 图像添加非中文文字
- OpenCV|【OpenCV 完整例程】91. 高斯噪声、瑞利噪声、爱尔兰噪声
- opencv|python+opencv车道线,实线虚线的检测