【OpenCV:从零到一】09:腐蚀和膨胀|滑动条

前言
这是我《OpenCV:从零到一》专栏的第九篇博客,想看跟多请戳这。
本文概要
滑动条的设置createTrackbar
getStructuringElement
腐蚀操作 erode
膨胀操作dilate
案例代码
大概内容: 滑动条控制图片的腐蚀/膨胀程度。

#include  
#include  
using namespace cv;

void CallBack_Demo(int, void*);

Mat src, dst;
char OUTPUT_WIN[] = "output image";
int element_size = 3;
int max_size = 21;

int main(int argc, char** argv) {
	src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);

	namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);
	createTrackbar("Element Size :", OUTPUT_WIN, &element_size, max_size, CallBack_Demo);
	CallBack_Demo(0, 0);

	waitKey(0);
	return 0;
}

void CallBack_Demo(int, void*) {
	int s = element_size * 2 + 1;
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
	// dilate(src, dst, structureElement, Point(-1, -1), 1);
	erode(src, dst, structureElement);
	imshow(OUTPUT_WIN, dst);
	return;
}

运行效果:


解析及注意事项

  • 滑动条可以方便的在一次运行中试验多组数据,不用像上一篇,那样运行这么多次。
  • createTrackbar(“属性名”, “输出的窗口名”, &初始值, 最大值, 回调函数);
  • 形态学的大多数操作都基于腐蚀和膨胀,他们跟卷积操作类似,假设有图像A和结构元素B,结构元素B在A上面移动,其中B定义其中心为锚点,计算B覆盖下A的最大或者最小的像素值用来替换锚点的像素,其中B作为结构体可以是任意形状。
  • erode(src, dst, 操作算子);操作算子可以通过下面的方法来获得
  • getStructuringElement(类型的宏, 核的大小, 锚点);类型的宏以及锚点对CROSS类型的影响如下图
    【OpenCV:从零到一】09:腐蚀和膨胀|滑动条_第1张图片

全注释代码

#include  
#include  
using namespace cv;

void CallBack_Demo(int, void*);

Mat src, dst;
char OUTPUT_WIN[] = "output image";
int element_size = 3;
int max_size = 21;

int main(int argc, char** argv) {
	src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);

	namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);
	createTrackbar("Element Size :", OUTPUT_WIN, &element_size, max_size, CallBack_Demo);
	/*
	参数列表
		const String & 	trackbarname,//Name of the created trackbar
		const String & 	winname,//Name of the window that will be used as a parent of the created trackbar.//窗口要已创建
		int * 	value,//Optional pointer to an integer variable whose value reflects the position of the slider. 
					 //Upon creation, the slider position is defined by this variable.//滑动条的初始值
		int count,//Maximal position of the slider. The minimal position is always 0.//滑动条的最大值(最小值默认为0)
		TrackbarCallback onChange = 0,//Pointer to the function to be called every time the slider changes position.
				//This function should be prototyped as void Foo(int,void*); //回调函数
				//where the first parameter is the trackbar position and the second parameter is the user data.
		void * 	userdata = 0 //	User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
	最后一个参数是用来回传回调函数里的数据的,一般很少用
	*/
	CallBack_Demo(0, 0);

	waitKey(0);
	return 0;
}

void CallBack_Demo(int, void*) {
	int s = element_size * 2 + 1;
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
	/*Returns a structuring element of the specified size and shape for morphological operations.
	t shape, //Element shape that could be one of MorphShapes
			//MorphShapes : MORPH_RECT   MORPH_CROSS  MORPH_ELLIPSE
	Size ksize,//Size of the structuring element.
	Point 	anchor = Point(-1,-1) //Anchor position within the element. The default value (−1,−1) means that the anchor is at the center.
		//Note that only the shape of a cross-shaped element depends on the anchor position. 
		//In other cases the anchor just regulates how much the result of the morphological operation is shifted.
	*/
	// dilate(src, dst, structureElement, Point(-1, -1), 1);
	erode(src, dst, structureElement);//dilate和erode的参数一模一样
	/*
	InputArray 	src,
	OutputArray 	dst,
	InputArray 	kernel,structuring element used for erosion; Kernel can be created using getStructuringElement
	Point 	anchor = Point(-1,-1),
	int 	iterations = 1,//number of times dilation is applied.
	int 	borderType = BORDER_CONSTANT,
	const Scalar & 	borderValue = morphologyDefaultBorderValue() //border value in case of a constant border
	*/
	imshow(OUTPUT_WIN, dst);
	return;
}

翻译笔记
slider 滑动器,滑杆;浮动块,滑动块;
prototyped 原型
morphology 形态学
erosion 腐蚀
dilation 膨胀

你可能感兴趣的:(OpenCV:从零到一,opencv,c++,计算机视觉)