前言
在上一章中描述了如何进行形态学操作(膨胀与腐蚀),详细描述可点击查看(https://www.jianshu.com/writer#/notebooks/47386368/notes/77582086)
目标
本章中,将学习如何:
- 开操作——Open
- 闭操作——Close
- 形态学梯度——Morphological Gradient
- 顶帽——Top hat
- 黑帽——Black hat
开操作——Open
- 先腐蚀后膨胀
- 可以去掉小的对象,假设对象是前景色,背景是黑色
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
// 1、加载图像,可以是BGR或者灰度图像
Mat src = imread("/gdsfr.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
Mat dst = Mat(src.size(), src.type());
char output_win[] = "morphology image";
namedWindow(output_win, WINDOW_AUTOSIZE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(src, dst, MORPH_OPEN, kernel);
imshow(output_win, dst);
waitKey(0);
return 0;
}

闭操作——Close
- 先膨胀后腐蚀
- 可以填充小的洞(fillhole),假设对象是前景色,背景是黑色
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
// 1、加载图像,可以是BGR或者灰度图像
Mat src = imread("/gfdg.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
Mat dst = Mat(src.size(), src.type());
char output_win[] = "morphology image";
namedWindow(output_win, WINDOW_AUTOSIZE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(src, dst, MORPH_CLOSE, kernel);
imshow(output_win, dst);
waitKey(0);
return 0;
}

形态学梯度——Morphological Gradient
- 膨胀减去腐蚀,提取物体边缘
- 又称为基本梯度(其他还包括--内部梯度、方向梯度)
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
// 1、加载图像,可以是BGR或者灰度图像
Mat src = imread("/hsdf.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
Mat dst = Mat(src.size(), src.type());
char output_win[] = "morphology image";
namedWindow(output_win, WINDOW_AUTOSIZE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(src, dst, MORPH_GRADIENT, kernel);
imshow(output_win, dst);
waitKey(0);
return 0;
}

顶帽——Top hat
- 顶帽是原图像与开操作之间的差值图像
- 突出原图像中比周围亮的区域
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
// 1、加载图像,可以是BGR或者灰度图像
Mat src = imread("/agds.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
Mat dst = Mat(src.size(), src.type());
char output_win[] = "morphology image";
namedWindow(output_win, WINDOW_AUTOSIZE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(src, dst, MORPH_TOPHAT, kernel);
imshow(output_win, dst);
waitKey(0);
return 0;
}

黑帽——Black hat
- 黑帽是闭操作图像与原图像的差值图像
- 突出原图像中比周围暗的区域
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
// 1、加载图像,可以是BGR或者灰度图像
Mat src = imread("/gfdg.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
Mat dst = Mat(src.size(), src.type());
char output_win[] = "morphology image";
namedWindow(output_win, WINDOW_AUTOSIZE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(src, dst, MORPH_BLACKHAT, kernel);
imshow(output_win, dst);
waitKey(0);
return 0;
}

相关AIP:
void morphologyEx( InputArray src, OutputArray dst,int op, InputArray kernel,Point anchor = Point(-1,-1), int iterations = 1,int borderType = BORDER_CONSTANT,const Scalar& borderValue = morphologyDefaultBorderValue() );
- 第一个参数,InputArray类型的src,输入图像,即源图像,填Mat类的对象即可。图像位深应该为以下五种之一:CV_8U, CV_16U,CV_16S, CV_32F 或CV_64F。
- 第二个参数,OutputArray类型的dst,即目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型。
- 第三个参数,int类型的op,表示形态学运算的类型,可以是如下之一的标识符:
- MORPH_OPEN – 开运算(Opening operation)
- MORPH_CLOSE – 闭运算(Closing operation)
- MORPH_GRADIENT -形态学梯度(Morphological gradient)
- MORPH_TOPHAT - “顶帽”(“Top hat”)
- MORPH_BLACKHAT - “黑帽”(“Black hat“)
- 第四个参数,InputArray类型的kernel,形态学运算的内核。若为NULL时,表示的是使用参考点位于中心3x3的核。我们一般使用函数 getStructuringElement配合这个参数的使用。getStructuringElement函数会返回指定形状和尺寸的结构元素(内核矩阵)。关于getStructuringElement我们上篇文章中讲过了,这里为了大家参阅方便,再写一遍:
其中,getStructuringElement函数的第一个参数表示内核的形状,我们可以选择如下三种形状之一:
- 矩形: MORPH_RECT
- 交叉形: MORPH_CROSS
- 椭圆形: MORPH_ELLIPSE
- 第五个参数,Point类型的anchor,锚的位置,其有默认值(-1,-1),表示锚位于中心。
- 第六个参数,int类型的iterations,迭代使用函数的次数,默认值为1。
- 第七个参数,int类型的borderType,用于推断图像外部像素的某种边界模式。注意它有默认值BORDER_ CONSTANT。
- 第八个参数,const Scalar&类型的borderValue,当边界为常数时的边界值,有默认值morphologyDefaultBorderValue(),一般我们不用去管他。需要用到它时,可以看官方文档中的createMorphologyFilter()函数得到更详细的解释。
网友评论