具体看:这个博客:https://blog.csdn.net/qq_36387683/article/details/80479793
==========================================================
膨胀是用周边最大的像素值替换最小的,所以 越膨胀,你的图片白色越多。
腐蚀是用周边最小的像素值替换最大的,所以,越腐蚀,你的图片的黑色越多。
======================================================
下面是代码,代码我没有敲,只是看了基础
=========================================
1 #include <opencv2/opencv.hpp> 2 #include <iostream> 3 using namespace cv; 4 5 Mat src, dst; 6 char OUTPUT_WIN[] = "output image"; 7 int element_size = 3; 8 int max_size = 21; 9 void CallBack_Demo(int, void*); 10 int main(int argc, char** argv) { 11 12 src = imread("D:/vcprojects/images/test1.png"); 13 if (!src.data) { 14 printf("could not load image... "); 15 return -1; 16 } 17 namedWindow("input image", CV_WINDOW_AUTOSIZE); 18 imshow("input image", src); 19 20 namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE); 21 createTrackbar("Element Size :", OUTPUT_WIN, &element_size, max_size, CallBack_Demo); 22 CallBack_Demo(0, 0); 23 24 waitKey(0); 25 return 0; 26 } 27 28 void CallBack_Demo(int, void*) { 29 int s = element_size * 2 + 1; 30 Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1)); 31 // dilate(src, dst, structureElement, Point(-1, -1), 1); 32 erode(src, dst, structureElement); 33 imshow(OUTPUT_WIN, dst); 34 return; 35 }