前言
這是我《OpenCV:從零到一》專欄的第九篇博客,想看跟多請戳這。
本文概要
滑動條的設置createTrackbar
getStructuringElement
腐蝕操作 erode
膨脹操作dilate
案例代碼
大概內容: 滑動條控制圖片的腐蝕/膨脹程度。
#include <opencv2/opencv.hpp>
#include <iostream>
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類型的影響如下圖
全註釋代碼
#include <opencv2/opencv.hpp>
#include <iostream>
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 膨脹