#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;
}