OpenCV c++滑動條的創(chuàng)建和使用代碼
什么是滑動條
滑動條是 OpenCV 動態(tài)調節(jié)參數特別好用的工具,它依附于窗口而存在。
創(chuàng)建滑動條
在 OpenCV 中,可以使用createTrackbar
函數來創(chuàng)建一個可以調整數值的滑動條,并將滑動條附加到指定的窗口上。
參考代碼
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
表示指向回調函數的指針,每次滑塊位置改變時,這個函數都會進行回調。回調的類型為void xx(int, void*)
,其中第一個參數表示軌跡條的位置,第二個參數表示用戶數據userdata
。userdate
表示傳給回調函數的用戶數據。
#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; }
獲取當前滑動條位置
在 OpenCV 中,可以使用getTrackbarPos()
函數來獲取當前滑動條的位置。
參考代碼
int getTrackbarPos(const string& trackbarname, const string& winname);
其中第一個參數表示滑動條的名字,第二個參數表示軌跡條的父窗口的名稱。
總結
到此這篇關于OpenCV c++滑動條的創(chuàng)建和使用代碼的文章就介紹到這了,更多相關OpenCV c++滑動條創(chuàng)建使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++獲取文件哈希值(hash)和獲取torrent(bt種子)磁力鏈接哈希值
這二個代碼一個是獲取文件哈希值的,另外一個是獲取torrent文件磁力鏈接的哈希值2013-11-11