OpenCV学习笔记——膨胀与腐蚀实验

#include
#include
#include

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,C++)