一、形态学其他操作(用的不多,如果忘了也可以通过膨胀腐蚀得到相同效果)
1.开运算
2.闭运算
3.形态学梯度
4.顶帽
5.黑帽
#include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat srcImg = imread("E://1.jpg");
Mat dstImg;
Mat element = getStructuringElement(MORPH_RECT, Size(, )); //morphologyEx(srcImg, dstImg, MORPH_OPEN, element); //开运算
//morphologyEx(srcImg, dstImg, MORPH_CLOSE, element); //闭运算
//morphologyEx(srcImg, dstImg, MORPH_GRADIENT, element); //形态学梯度运算
//morphologyEx(srcImg, dstImg, MORPH_TOPHAT, element); //顶帽运算
morphologyEx(srcImg, dstImg, MORPH_BLACKHAT, element); //黒帽运算 imshow("src", srcImg);
imshow("dst", dstImg);
waitKey();
}
二、小应用(找出差异,对差异进行操作)
#include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat img1=imread("E://A.jpg");
Mat img2=imread("E://B.jpg");
Mat diff;
absdiff(img1,img2,diff);//用这个做减法可以避免出现负值=0的情况
imshow("img1",img1);
imshow("img2",img2);
imshow("diff",diff); Mat gray;
cvtColor(diff,gray,COLOR_BGR2GRAY);//转为灰度图
//adaptiveThreshold(gray,gray,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY_INV,15,3);//自适应阈值有点不懂
threshold(gray,gray,,,THRESH_BINARY);
imshow("threshold",gray); //进行形态学操作
Mat element=getStructuringElement(MORPH_RECT,Size(,));//定义结构元素
morphologyEx(gray,gray,MORPH_OPEN,element);//开运算
medianBlur(gray,gray,);//中值滤波
dilate(gray,gray,element);
imshow("dst",gray); waitKey();
}