目录
getStructuringElement(int shape, Size ksize, Point anchor)
形态学操作-膨胀
跟卷积操作类似,假设有图像A和结构元素B,结构元素B在A上面移动,其中B定义其中心为锚点,计算B覆盖下A的最大像素值用来替换锚点的像素,其中B作为结构体可以是任意形状
形态学操作-腐蚀
腐蚀跟膨胀操作的过程类似,唯一不同的是以最小值替换锚点重叠下图像的像素值
getStructuringElement(int shape, Size ksize, Point anchor)
- 形状 (MORPH_RECT \MORPH_CROSS \MORPH_ELLIPSE)
矩形 交叉型 椭圆形
- 大小
- 锚点 默认是Point(-1, -1)意思就是中心像素
膨胀 dilate(src, dst, kernel)
腐蚀 erode(src, dst, kernel)
动态调整结构元素大小createTrackbar(const String & trackbarname, const String winName, int* value, int count, Trackbarcallback func, void* userdata=0)
#include <iostream>
#include <cstring>
#include <opencv2/highgui/highgui.hpp>
#include<cmath>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
Mat src, dst;
int pz_size = 0;//膨胀程度
int pzMax_size = 21;//膨胀最大程度
int fs_size = 0;//腐蚀程度
int fsMax_size = 21;//腐蚀程度
//膨胀
void pz(int, void*)
{
int pzValue = pz_size * 2 + 1;
//膨胀操作的内核,明确它的形状
Mat structureElement = getStructuringElement(MORPH_RECT, Size(pzValue, pzValue), Point(-1, -1));
dilate(src, dst, structureElement);
imshow("output", dst);
}
//腐蚀
void fs(int, void*)
{
int fsValue = fs_size * 2 + 1;
//交叉型
Mat structureElement = getStructuringElement(MORPH_RECT, Size(fsValue, fsValue), Point(-1, -1));
erode(src, dst, structureElement);
imshow("output", dst);
}
//腐蚀与膨胀一同
void fun(int, void*)
{
int pzValue = pz_size * 2 + 1;
int fsValue = fs_size * 2 + 1;
//先进行膨胀
Mat structureElement = getStructuringElement(MORPH_RECT, Size(pzValue, pzValue), Point(-1, -1));
dilate(src, dst, structureElement);
//后进行腐蚀
structureElement = getStructuringElement(MORPH_RECT, Size(fsValue, fsValue), Point(-1, -1));
erode(dst, dst, structureElement);
imshow("output", dst);
}
int main()
{
src = imread("D:/opencv/2/a.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
namedWindow("input");
imshow("input", src);
namedWindow("output");
createTrackbar("膨胀值", "output", &pz_size, pzMax_size, fun);
createTrackbar("腐蚀值", "output", &fs_size, fsMax_size, fun);
pz(0, 0);
waitKey(0);
return 0;
}