1.膨胀操作
1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 4 using namespace std; 5 using namespace cv; 6 7 int main(int argc, char **argv) 8 { 9 Mat src = imread("D:/meinv.jpg"); 10 cvNamedWindow("src_image", CV_WINDOW_AUTOSIZE); 11 imshow("src_image", src); 12 13 //获取自定义核 14 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); 15 Mat out; 16 17 dilate(src, out, element); 18 cvNamedWindow("dilate_image", CV_WINDOW_AUTOSIZE); 19 imshow("dilate_image", out); 20 imwrite("D:/learn-opencv/dilate.jpg",out); 21 22 23 waitKey(0); 24 return 0; 25 }
显示结果:
(1)原图
(2)膨胀操作图
2.腐蚀操作
1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 4 using namespace std; 5 using namespace cv; 6 7 int main(int argc, char **argv) 8 { 9 Mat src = imread("D:/meinv.jpg"); 10 cvNamedWindow("src_image", CV_WINDOW_AUTOSIZE); 11 imshow("src_image", src); 12 13 //获取自定义核 14 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); 15 Mat out; 16 17 erode(src, out, element); 18 cvNamedWindow("erode_image", CV_WINDOW_AUTOSIZE); 19 imshow("erode_image", out); 20 imwrite("D:/learn-opencv/dilate.jpg",out); 21 22 23 waitKey(0); 24 return 0; 25 }
显示效果: