膨胀:
类似于卷积操作,卷积核可以是任何形状,把卷积核覆盖的区域中像素值最大的像素点,赋值给卷积核的锚点。
腐蚀:
与膨胀类似,把最小值赋值给锚点。
#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void dilate_fun(int, void*); void erode_fun(int, void*); Mat src; Mat image_dilate; Mat image_erode; int element_dilate = 3; int element_erode = 3; int max_size = 11; int main(int argc, char** argv) { src = imread("D:/OpenCVprj/image/test3.jpg"); imshow("src", src); namedWindow("image_dilate", CV_WINDOW_AUTOSIZE); namedWindow("image_erode", CV_WINDOW_AUTOSIZE); createTrackbar("size", "image_dilate", &element_dilate, max_size, dilate_fun); createTrackbar("size", "image_erode", &element_erode, max_size, erode_fun); dilate_fun(0, 0); erode_fun(0, 0); waitKey(0); return 0; } void dilate_fun(int, void*) { int s = 2 * element_dilate + 1; Mat kernel = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1)); dilate(src, image_dilate, kernel); imshow("image_dilate", image_dilate); return; } void erode_fun(int, void*) { int s = 2 * element_erode + 1; Mat kernel = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1)); erode(src, image_erode, kernel); imshow("image_erode", image_erode); return; }
结果: