开操作- open
定义:先腐蚀后膨胀
作用:可以去掉小的白噪点,填充小的白洞。能排除小区域物体、消除孤立点、去噪、平滑物体的轮廓。
闭操作- close
定义:先膨胀后腐蚀
作用:去掉小的黑噪点,可以填充小的黑洞(fill hole补洞)。填充目标区域内的离散小空洞和分散部分。
形态学梯度- Morphological Gradient
定义:膨胀减去腐蚀。又称为基本梯度(其它还包括-内部梯度、方向梯度)
作用:边缘提取,对二值图像进行这一操作,可以将团块(blob)的边缘突出出来,保留物体的边缘轮廓。
顶帽 – top hat
顶帽 是原图像与开操作之间的差值图像
作用:显示开操作所去掉的小白噪点。
黑帽 – black hat
黑帽是闭操作图像与源图像的差值图像
作用:显示闭操作所填充的小黑洞。
API介绍
morphologyEx(src, dest, CV_MOP_BLACKHAT, kernel);
- Mat src – 输入图像
- Mat dest – 输出结果
- int OPT – CV_MOP_OPEN/ CV_MOP_CLOSE/ CV_MOP_GRADIENT / CV_MOP_TOPHAT/ CV_MOP_BLACKHAT 形态学操作类型
- Mat kernel 结构元素
- int Iteration 迭代次数,默认是1
演示
//****************Cuda
#include "opencv2/cudaarithm.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/cudafeatures2d.hpp"
#include "opencv2/cudaobjdetect.hpp"
#include "opencv2/cudafilters.hpp"
//***************OpenCV
#include <fstream>
#include <string>
#include<iostream>
#include "opencv2/opencv_modules.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/timelapsers.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
// #include <opencv2/nofree/nofree.hpp>
#include<opencv2/xfeatures2d.hpp>
#include "ctime"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::detail;
bool readCamera(const string& filename, Mat& cameraMatrix, Mat& distCoeffs, float& ratio);
int main(int argc, char** argv) {
src = imread("G:\\data1\\image1.jpg");
namedWindow("input image", CV_WINDOW_NORMAL);
imshow("input image", src);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
Mat dst;
morphologyEx(src, dst, CV_MOP_GRADIENT, kernel);
namedWindow("output image", CV_WINDOW_NORMAL);
imshow("output image", dst);
waitKey(0);
return 0;
}
参考:https://zhuanlan.zhihu.com/mengcius-opencvbase