OpenCV c++滑動條的創(chuàng)建和使用代碼
什么是滑動條
滑動條是 OpenCV 動態(tài)調(diào)節(jié)參數(shù)特別好用的工具,它依附于窗口而存在。
創(chuàng)建滑動條
在 OpenCV 中,可以使用createTrackbar
函數(shù)來創(chuàng)建一個可以調(diào)整數(shù)值的滑動條,并將滑動條附加到指定的窗口上。
參考代碼
int createTrackbar(const string & trackbarname, const string & winname, int * value, int count, TrackbarCallback onChange = 0, void * userdata = 0)
其中,trackbarname
表示我們創(chuàng)建的滑動條的名字。winname
表示這個滑動條吸附在的窗口的名字。value
表示滑塊的位置,在創(chuàng)建時,滑塊的初始位置就是該變量的值。count
表示滑塊可以到達的最大值,最小值始終為 0。onChange
表示指向回調(diào)函數(shù)的指針,每次滑塊位置改變時,這個函數(shù)都會進行回調(diào)?;卣{(diào)的類型為void xx(int, void*)
,其中第一個參數(shù)表示軌跡條的位置,第二個參數(shù)表示用戶數(shù)據(jù)userdata
。userdate
表示傳給回調(diào)函數(shù)的用戶數(shù)據(jù)。
#include<opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc_c.h> #include <opencv2/imgproc/types_c.h> #include<opencv2/imgproc.hpp> #include<iostream> using namespace std; using namespace cv; Mat image, srcImage; int thresholds = 50; void threshold_track(int, void*) { Mat result; threshold(srcImage, result, thresholds, 255, THRESH_BINARY); //Canny(srcImage, result, thresholds, thresholds * 3, 3); imshow("邊緣檢測", result); } int main() { image = cv::imread("...cc.png"); if (!image.data) return 1; cvtColor(image, srcImage, COLOR_BGR2GRAY); namedWindow("邊緣檢測", WINDOW_AUTOSIZE); createTrackbar("閾值", "邊緣檢測", &thresholds, 300, threshold_track); waitKey(0); return 0; }
獲取當(dāng)前滑動條位置
在 OpenCV 中,可以使用getTrackbarPos()
函數(shù)來獲取當(dāng)前滑動條的位置。
參考代碼
int getTrackbarPos(const string& trackbarname, const string& winname);
其中第一個參數(shù)表示滑動條的名字,第二個參數(shù)表示軌跡條的父窗口的名稱。
總結(jié)
到此這篇關(guān)于OpenCV c++滑動條的創(chuàng)建和使用代碼的文章就介紹到這了,更多相關(guān)OpenCV c++滑動條創(chuàng)建使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++獲取文件哈希值(hash)和獲取torrent(bt種子)磁力鏈接哈希值
這二個代碼一個是獲取文件哈希值的,另外一個是獲取torrent文件磁力鏈接的哈希值2013-11-11