#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<iostream>
using namespace std;
using namespace cv;
/*
開操作:先腐蝕後膨脹 作用:能夠去掉一些小的對象,保留大的對象
閉操作:先膨脹後腐蝕 作用:將小洞給填充上
形態學梯度:膨脹減去腐蝕 作用:求出邊緣
頂帽變換:原圖像與開操作之間的差值圖像 作用:保留那些小的對象
黑帽操作:閉操作與原圖像的差值圖像 作用:保留補上的洞
*/
int main(int argc, char** argv)
{
// read image
Mat src = imread("C:/Users/qxq/Pictures/image/fox.jpg");
if (src.empty())
{
printf("Could not load image...");
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
char output_title[] = "morphology demo";
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
Mat dst;
//結構元素
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
//開操作
//morphologyEx(src, dst, CV_MOP_OPEN, kernel);
//閉操作
//morphologyEx(src, dst, CV_MOP_CLOSE, kernel);
//形態學梯度
//morphologyEx(src, dst, CV_MOP_GRADIENT, kernel);
//頂帽操作
//morphologyEx(src, dst, CV_MOP_TOPHAT, kernel);
//黑帽操作
morphologyEx(src, dst, CV_MOP_BLACKHAT, kernel);
imshow(output_title, dst);
waitKey(0);
return 0;
}