【Ubuntu+OpenCV】HighGUI之trackbar(滑動條)的建立–學習筆記【2】

來源:互聯網
上載者:User

一、要在一副映像上加一個滑動條的步驟:


1.先建立一個視窗,該視窗就是而後要把trackba放上去的父視窗。即trackbar屬於那個視窗。


2.建立trackbar。要用到函數cvCreateTrackbar函數。

 在opencv內建的pdf檔案裡面可知道該函數的原型如下:

Creates a trackbar and attaches it to the specified window<br />int cvCreateTrackbar(<br />const char* trackbarName,<br />const char* windowName,<br />int* value,<br />int count,<br />CvTrackbarCallback onChange );<br />trackbarName Name of the created trackbar.<br />windowName Name of the window which will be used as a parent for created trackbar.<br />value Pointer to an integer variable, whose value will reflect the position of the slider. Upon<br />creation, the slider position is defined by this variable.<br />count Maximal position of the slider. Minimal position is always 0.<br />onChange Pointer to the function to be called every time the slider changes position. This function<br />should be prototyped as void Foo(int); Can be NULL if callback is not required.<br />The function cvCreateTrackbar creates a trackbar (a.k.a. slider or range control) with the<br />specified name and range, assigns a variable to be syncronized with trackbar position and speci-<br />fies a callback function to be called on trackbar position change. The created trackbar is displayed<br />on the top of the given window.

trackbarName——就是trackbar的名字,你想給個什麼名字就取什麼名字

windowName——用於指定trackbar要依附在那個視窗上,注意

:該視窗必須先於調用cvCreateTrackbar之前建立,否則trackbar不  會顯示在視窗上。

value——自訂一個整數指標變數。trackbar的位置改變的時候,*value就會隨著改變。經驗:

同時*value的值確定了trackbar最一開始時的位置!你大可以把value=10,5;看一看trackbar的初始位置!!實驗出真知嘛!!哈哈哈……

count——確定trackbar的範圍,最小值預設是0,最大值就是count。滑動條位置取值範圍:[0,count]

onChange——
trackbar位置改變的時候所調用的函數。該函數的原型必須符合以下原型:void xxxxx(  int  ) ;函數的名字你隨便取。
經驗
:onChange只是一個函數預留位置,其實可以改名字是任何你自己定義的函數名!!

3、總之:

1、該函數在名為windowName的視窗上建立一個名為trackbarName的滑動條,滑動條的滑動範圍是    [0,count];至於滑動條是在視窗頂部還是底部,這與系統有關。



2、當滑動條位置發生改變的時候同時發生以下兩件事,(a)value的值隨之改變變為當前的滑動條的位置值,(b)調用名字為

onChange的

函數。


二、實戰例子

 

對一張灰階映像進行canny邊緣檢測。用捲軸來確定threshold1、threshold2

//2011/06/14<br />#include "cv.h"<br />#include "highgui.h"<br />#include <stdio.h><br />IplImage* src = NULL ;<br />IplImage* dst = NULL ;<br />static const char* wnd_name = "canny" ;<br />static const char* file_name = "lena.jpg" ;<br />static const char* trackbar_name = "threshold" ;<br />void on_track( int pos )<br />{<br />if( src->nChannels != 1 )<br />{<br />printf("source image is not gray/n");<br />}</p><p>if( pos == 0 )<br />{<br />cvShowImage(wnd_name,src);<br />}<br />else<br />{<br />cvCanny(src,dst,pos,pos * 3 ,3);<br />cvShowImage(wnd_name,dst);<br />}</p><p>}<br />int main( int argc,char** argv)<br />{<br />int value = 0 ;<br />src = cvLoadImage( file_name,0 );<br />dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);</p><p>cvNamedWindow(wnd_name,CV_WINDOW_AUTOSIZE ) ;<br />cvCreateTrackbar(<br />trackbar_name,//const char* trackbarName,<br />wnd_name,//const char* windowName,<br />&value,//int* value,<br />100,//int count,<br />on_track//CvTrackbarCallback onChange<br />);<br />on_track(0);</p><p>cvWaitKey(0);</p><p>cvDestroyAllWindows();<br />cvReleaseImage(&src);<br />cvReleaseImage(&dst);<br />return 0 ;<br />}

 

三、結果

ubuntu+opencv環境下,程式的編譯參見鄙人的**學習筆記【1】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.