Opencv腐蚀与膨胀-滚动条

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

using namespace std;
using namespace cv;

Mat srcImage, dstDilateImage, dstErodeImage, dstImage;
int g_nDilateValue = 0, g_nErodeValue = 0, g_nDilorEro = 0, g_nValue = 0;
Mat DilateElement, ErodeElement, Element;

//有滚动条事件时,可以进入回调函数
void on_Trackbar(int, void *)
{
	DilateElement = getStructuringElement(MORPH_RECT, Size(2 * g_nDilateValue + 1, 2 * g_nDilateValue + 1));
	dilate(srcImage, dstDilateImage, DilateElement);
	imshow("【膨胀窗口】", dstDilateImage);
	ErodeElement = getStructuringElement(MORPH_RECT, Size(2 * g_nErodeValue + 1, 2 * g_nErodeValue + 1));
	erode(srcImage, dstErodeImage, ErodeElement);
	imshow("【腐蚀窗口】", dstErodeImage);

	if (!g_nDilorEro)
	{
		Element = getStructuringElement(MORPH_RECT, Size(2 * g_nValue + 1, 2 * g_nValue + 1));
		dilate(srcImage, dstDilateImage, Element);
		imshow("【测试窗口】", dstDilateImage);
	}
	else
	{
		Element = getStructuringElement(MORPH_RECT, Size(2 * g_nValue + 1, 2 * g_nValue + 1));
		erode(srcImage, dstErodeImage, Element);
		imshow("【测试窗口】", dstErodeImage);
	}
}

int main()
{
	srcImage = imread("tiger.jpg");
	imshow("【原图】", srcImage);
	dstDilateImage.create(srcImage.size(), srcImage.type());
	dstErodeImage.create(srcImage.size(), srcImage.type());

	namedWindow("【膨胀窗口】");
	createTrackbar("Value", "【膨胀窗口】", &g_nDilateValue, 100, on_Trackbar);
	on_Trackbar(g_nDilateValue, 0);
	namedWindow("【腐蚀窗口】");
	createTrackbar("Value", "【腐蚀窗口】", &g_nErodeValue, 100, on_Trackbar);
	on_Trackbar(g_nErodeValue, 0);

	namedWindow("【测试窗口】");
	createTrackbar("膨胀/腐蚀", "【测试窗口】", &g_nDilorEro, 1, on_Trackbar);
	on_Trackbar(g_nDilorEro, 0);
	createTrackbar("Value", "【测试窗口】", &g_nValue, 100, on_Trackbar);
	on_Trackbar(g_nValue, 0);

	waitKey(0);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值