main.cpp
#include <istream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char **argv) { Mat src, open_dst, close_dst, gradient_dst, top_hat_dst, black_hat_dst, dilate_dst, erode_dst, hit_miss_dst; // 加载图片 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); // 形态学开操作 char open_title[] = "open"; namedWindow(open_title, WINDOW_AUTOSIZE); Mat open_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, open_dst, MORPH_OPEN, open_kernel); imshow(open_title, open_dst); // 形态学关操作 char close_title[] = "close"; namedWindow(close_title, WINDOW_AUTOSIZE); Mat close_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, close_dst, MORPH_CLOSE, close_kernel); imshow(close_title, close_dst); // 形态学梯度操作 char gradient_title[] = "gradient"; namedWindow(gradient_title, WINDOW_AUTOSIZE); Mat gradient_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, gradient_dst, MORPH_GRADIENT, gradient_kernel); imshow(gradient_title, gradient_dst); // 形态学顶帽操作 char top_hat_title[] = "top_hat"; namedWindow(top_hat_title, WINDOW_AUTOSIZE); Mat top_hat_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, top_hat_dst, MORPH_TOPHAT, top_hat_kernel); imshow(top_hat_title, top_hat_dst); // 形态学黑帽操作 char black_hat_title[] = "black_hat"; namedWindow(black_hat_title, WINDOW_AUTOSIZE); Mat black_hat_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, black_hat_dst, MORPH_BLACKHAT, black_hat_kernel); imshow(black_hat_title, black_hat_dst); // 形态学膨胀操作 char dilate_title[] = "dilate"; namedWindow(dilate_title, WINDOW_AUTOSIZE); Mat dilate_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, dilate_dst, MORPH_DILATE, dilate_kernel); imshow(dilate_title, dilate_dst); // 形态学腐蚀操作 char erode_title[] = "erode"; namedWindow(erode_title, WINDOW_AUTOSIZE); Mat erode_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(src, erode_dst, MORPH_ERODE, dilate_kernel); imshow(erode_title, erode_dst); // 等待按键 waitKey(0); return 0; }