默认的腐蚀与膨胀操作,原理:点击打开链接
#include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; int main() { Mat src = imread("lena.jpg", 0); Mat dst, eroded, dilated; Mat element(7, 7, CV_8U, Scalar(1)); threshold(src, dst, 100, 255, THRESH_BINARY_INV);//二图像值化 namedWindow("dst", 0); imshow("dst", dst); erode(dst, eroded, Mat());//腐蚀 namedWindow("eroded", 0); imshow("eroded", eroded); dilate(dst, dilated, Mat());//膨胀 namedWindow("dilated", 0); imshow("dilated", dilated); waitKey(0); }
1、threshold 函数用于将灰度图像处理为二值化图像,第一个参数为输入的灰度图像,第二个参数为输出的二值化图像,第三个参数为阈值,第四个参数为满足阈值条件的像素转变成的数据,第五个参数为正反切换。还有一个THRESH_BINARY,效果相反。
在加入了强度系数之后:
#include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; int main() { Mat src = imread("lena.jpg", 0); Mat dst, eroded, dilated; Mat element(7, 7, CV_8U, Scalar(1)); threshold(src, dst, 100, 255, THRESH_BINARY_INV); namedWindow("dst", 0); imshow("dst", dst); erode(dst, eroded, element); namedWindow("eroded", 0); imshow("eroded", eroded); dilate(dst, dilated, element); namedWindow("dilated", 0); imshow("dilated", dilated); waitKey(0); }
1、element 矩阵是腐蚀和膨胀的强度,这里用的是7*7的结构元素。