#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
char* window_name = "膨胀/腐蚀";
int choice_slider = 0, size_slider = 5;
Mat src, dst;
void process()
{
Mat st_elem =
getStructuringElement(MORPH_RECT, Size(size_slider, size_slider));
if (choice_slider == 0)
{
erode(src, dst, st_elem);
}
else
dilate(src, dst, st_elem);
imshow(window_name, dst);
}
void on_trackbar(int, void*)
{
process();
}
void on_size_slider(int, void*)
{
int size = max(1, size_slider);
size = size % 2 == 0 ? size + 1 : size;
setTrackbarPos("KernelSize", window_name, size);
process();
}
int main()
{
src = imread("樱木花道.jpg");
imshow("Original Image", src);
namedWindow(window_name);
Mat st_elem =
getStructuringElement(MORPH_RECT, Size(size_slider, size_slider));
erode(src, dst, st_elem);
imshow(window_name, dst);
createTrackbar("Erode/Dilate", window_name, &choice_slider, 1, on_trackbar);
createTrackbar("KernelSize", window_name, &size_slider, 21, on_size_slider);
waitKey(0);
return 0;
}