S1:对原图像进行腐蚀
S2:腐蚀结果再次膨胀
S1:对原图像进行膨胀
S2:膨胀结果再次进行腐蚀
S1:对原图像膨胀
S2:对原图像腐蚀。这两步都要使用相同的结构元素
S3:膨胀结果减去腐蚀结果。
实际上就是提取原图像中一些较为分散的较小区域。
S1:对原图像开运算
S2:原图像减去开运算
S1:对原图像闭运算
S2:闭运算结果减去原图像
void cv::morphologyEx(InputArray src,
OutputArray dst,
int cp,
InputArray kernel,
Point anchor = Point(-1,-1),
int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar & borderValue = morphologyDefaultBorderValue()
)
·cp:形态学操作的类型标志。
·iterations:处理的次数。
#include
#include
using namespace cv; //opencv的命名空间
using namespace std;
//主函数
int main()
{
//生成用于膨胀的原图像
Mat src = (Mat_(9, 12) << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255,0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 0, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
namedWindow("src", WINDOW_NORMAL);//可以自由调节显示图像尺寸
imshow("src", src);
//3*3矩形结构元素
Mat kernel = getStructuringElement(0, Size(3, 3));
//对二值化矩阵进行形态学操作
Mat open, close, gradient, tophat, blackhat, hitmiss;
//对二值化矩阵进行开运算
morphologyEx(src, open, MORPH_OPEN, kernel);
namedWindow("open", WINDOW_NORMAL);//可以自由调节显示图像尺寸
imshow("open", open);
//对二值化矩阵进行闭运算
morphologyEx(src, close, MORPH_CLOSE, kernel);
namedWindow("close", WINDOW_NORMAL);//可以自由调节显示图像尺寸
imshow("close", close);
//对二值化矩阵进行梯度运算
morphologyEx(src, gradient, MORPH_GRADIENT, kernel);
namedWindow("gradient", WINDOW_NORMAL);//可以自由调节显示图像尺寸
imshow("gradient", gradient);
//对二值化矩阵进行顶帽运算
morphologyEx(src, tophat, MORPH_TOPHAT, kernel);
namedWindow("tophat", MORPH_TOPHAT);//可以自由调节显示图像尺寸
imshow("tophat", tophat);
//对二值化矩阵进行黑帽运算
morphologyEx(src, blackhat, MORPH_BLACKHAT, kernel);
namedWindow("blackhat", MORPH_BLACKHAT);//可以自由调节显示图像尺寸
imshow("blackhat", blackhat);
//对二值化矩阵进行击中击不中运算
morphologyEx(src, hitmiss, MORPH_HITMISS, kernel);
namedWindow("hitmiss", MORPH_HITMISS);//可以自由调节显示图像尺寸
imshow("hitmiss", hitmiss);
cout << "用图像验证形态学操作效果" << endl;
waitKey(0);//等待函数用于显示图像,按下键盘任意键后退出
int width = 500;
int heigh = 500;
Mat snow = imread("E:/opencv/opencv-4.6.0-vc14_vc15/opencv/snow.jpg");
resize(snow, snow,Size(width,heigh), INTER_CUBIC);
imshow("原图", snow);
threshold(snow, snow, 50, 255, THRESH_BINARY);
imshow("二值化后的图像", snow);
//5*5矩形结构元素
Mat kernel_snow = getStructuringElement(0, Size(5, 5));
Mat open_snow, close_snow, gradient_snow, tophat_snow, blackhat_snow, hitmiss_snow1;
//对二值化矩阵进行开运算
morphologyEx(snow, open_snow, MORPH_OPEN, kernel_snow);
imshow("open_snow", open_snow);
//对二值化矩阵进行闭运算
morphologyEx(snow, close_snow, MORPH_CLOSE, kernel_snow);
imshow("close_snow", close_snow);
//对二值化矩阵进行梯度运算
morphologyEx(snow, gradient_snow, MORPH_GRADIENT, kernel_snow);
imshow("gradient_snow", gradient_snow);
//对二值化矩阵进行顶帽运算
morphologyEx(snow, tophat_snow, MORPH_TOPHAT, kernel_snow);
imshow("tophat_snow", tophat_snow);
//对二值化矩阵进行黑帽运算
morphologyEx(snow, blackhat_snow, MORPH_BLACKHAT, kernel_snow);
imshow("blackhat_snow", blackhat_snow);
//对二值化矩阵进行击中击不中运算
morphologyEx(snow, hitmiss_snow1, MORPH_HITMISS, kernel_snow);
imshow("hitmiss_snow", hitmiss_snow1);
waitKey(0);//等待函数用于显示图像,按下键盘任意键后退出
return 0;
}