前言
这是我《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;
}
运行效果:
解析及注意事项
全注释代码
#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 膨胀