main.cpp
#include <istream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int dilate_size = 1;
char dilate_title[] = "dilate_dst";
const char *dilate_bar = "dilate";
int erode_size = 1;
char erode_title[] = "erode_dst";
const char *erode_bar = "dilate";
Mat src, dilate_dst, erode_dst, dilate_element, erode_element;
void dilate_callback(int position, void *userdata);
void erode_callback(int position, void *userdata);
int main(int argc, char **argv) {
// 加载图片
src = imread("../../picture/bg1.webp", 1);
if (!src.data) {
printf("No image data \n");
return -1;
}
char src_title[] = "src";
namedWindow(src_title, WINDOW_AUTOSIZE);
imshow(src_title, src);
// 膨胀
namedWindow(dilate_title, WINDOW_AUTOSIZE);
createTrackbar(dilate_bar, dilate_title, &dilate_size, 50, dilate_callback);
// 腐蚀
namedWindow(erode_title, WINDOW_AUTOSIZE);
createTrackbar(erode_bar, erode_title, &erode_size, 50, erode_callback);
// 等待按键
waitKey(0);
return 0;
}
void dilate_callback(int position, void *userdata) {
if (position <= 0) {
position = 1;
}
dilate_size = position;
dilate_element = getStructuringElement(MORPH_RECT, Size(dilate_size, dilate_size));
dilate(src, dilate_dst, dilate_element);
imshow(dilate_title, dilate_dst);
}
void erode_callback(int position, void *userdata) {
if (position <= 0) {
position = 1;
}
erode_size = position;
erode_element = getStructuringElement(MORPH_RECT, Size(erode_size, erode_size));
erode(src, erode_dst, erode_element);
imshow(erode_title, erode_dst);
}