#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
Mat src, dst;
char OUTPUT_WIN[] = "output image";
int element_size = 3; //设定结构元素的大小
int max_size = 21;
void CallBack_Demo(int, void*);
int main(int argc, char** argv)
{
src = imread("3.jpg");
if (!src.data)
{
printf("无\n");
return -1;
}
namedWindow("input image", WINDOW_FREERATIO);
imshow("input image", src);
namedWindow("output image", WINDOW_FREERATIO);
createTrackbar("Element Size:", OUTPUT_WIN, &element_size, max_size, CallBack_Demo);//调整结构大小 生成滑块调节
CallBack_Demo(0, 0);
waitKey(0);
return 0;
}
void CallBack_Demo(int, void*)
{
int s = element_size * 2 + 1;
Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1)); //(形状,大小,锚点位置)
dilate(src, dst, structureElement, Point(-1, -1), 1); //膨胀
erode(src, dst, structureElement); //腐蚀
imshow(OUTPUT_WIN, dst);
return;
}
效果图
|