#include<iostream>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
//-------------------------------------【全局函数声明】------------------------------------------
// 描述:全局函数声明
//-------------------------------------------------------------------------------------------
//void dilate_test();
//void erode_test();
void on_erode(int num, void*);
void on_dilate(int num, void*);
//-------------------------------------【全局变量】------------------------------------------
// 描述:全局变量
//-------------------------------------------------------------------------------------------
//int b_nStructElementSize = 3;//结构元素
Mat b_srcImage;
Mat b_dstImage;
//-------------------------------------【int main()】------------------------------------------
// 描述:主函数接口
//---------------------------------------------------------------------------------------------
int main(int argc, char ** argv)
{
//【1】输入图像
b_srcImage = imread("G:\\VS_File\\Picture\\loze02.jpg");//灰度图读取
if (b_srcImage.empty())
{
return -1;
}
namedWindow("原始图像", 1);
imshow("原始图像", b_srcImage);
【】读取
int dilate_num = 1;
createTrackbar("dilate", "原始图像",&dilate_num,15,on_dilate,0);
on_dilate(dilate_num, 0);
int erode_num = 1;
createTrackbar("erode", "原始图像",&erode_num,15,on_erode,0 );
on_erode(erode_num,0);
//dilate_test();
//erode_test();
waitKey(0);
cv::destroyAllWindows();
return 0;
}
//-------------------------------------【dilate()】------------------------------------------
// 描述:图像膨胀
//---------------------------------------------------------------------------------------------------
void on_dilate(int num, void*)
{
int b_nStructElementSize = num;
Mat element = getStructuringElement(MORPH_RECT, Size(b_nStructElementSize * 2 + 1, b_nStructElementSize * 2 + 1), Point(b_nStructElementSize, b_nStructElementSize));
dilate(b_srcImage, b_dstImage, element);
namedWindow("膨胀效果图", 1);
imshow("膨胀效果图", b_dstImage);
}
//void dilate_test()
//{
// Mat b_srcImage = imread("G:\\VS_File\\Picture\\loze02.jpg");//灰度图读取
// if (b_srcImage.empty())
// {
// return ;
// }
// namedWindow("原始图像", 1);
// imshow("原始图像", b_srcImage);
// int b_nStructElementSize = 3;//结构元素
// Mat element = getStructuringElement(MORPH_RECT, Size(b_nStructElementSize * 2 + 1, b_nStructElementSize * 2 + 1), Point(b_nStructElementSize, b_nStructElementSize));
// Mat b_dstImage;
// dilate(b_srcImage, b_dstImage, element);
// namedWindow("膨胀效果图", 1);
// imshow("膨胀效果图", b_dstImage);
//}
//-------------------------------------【erode()】------------------------------------------
// 描述:图像腐蚀
//---------------------------------------------------------------------------------------------------
void on_erode(int num, void*)
{
int b_nStructElementSize = num;
Mat element = getStructuringElement(MORPH_RECT, Size(b_nStructElementSize * 2 + 1, b_nStructElementSize * 2 + 1), Point(b_nStructElementSize, b_nStructElementSize));
erode(b_srcImage, b_dstImage, element);
namedWindow("腐蚀效果图", 1);
imshow("腐蚀效果图", b_dstImage);
}
//void erode_test()
//{
// Mat b_srcImage = imread("G:\\VS_File\\Picture\\loze02.jpg");//灰度图读取
// if (b_srcImage.empty())
// {
// return;
// }
// namedWindow("原始图像", 1);
// imshow("原始图像", b_srcImage);
// int b_nStructElementSize = 3;//结构元素
// Mat element = getStructuringElement(MORPH_RECT, Size(b_nStructElementSize * 2 + 1, b_nStructElementSize * 2 + 1), Point(b_nStructElementSize, b_nStructElementSize));
// Mat b_dstImage;
// erode(b_srcImage, b_dstImage, element);
// namedWindow("腐蚀效果图", 1);
// imshow("腐蚀效果图", b_dstImage);
//}
OpenCV学习笔记——膨胀与腐蚀实验
最新推荐文章于 2022-08-13 23:18:43 发布
这篇博客介绍了如何使用OpenCV进行图像膨胀和腐蚀操作。通过创建跟踪条动态调整结构元素大小,展示了膨胀和腐蚀效果,帮助读者理解这两种基本的形态学图像处理技术。
摘要由CSDN通过智能技术生成