Skip to content

Commit 3a8d7ec

Browse files
Merge pull request opencv#26524 from MurtazaSaherwala:DocumentationUpdation
Updated trackbar callback function and improved documentation opencv#26524 This Fixes opencv#26467 Description: This pull request improve the OpenCV documentation regarding the Trackbar functionality. The current documentation does not provide clear guidance on certain aspects, such as handling the value pointer deprecation and utilizing callback arguments in C. This update addresses those gaps and provides an updated example for better clarity. Changes: Updated Documentation: Clarified the usage of the value pointer and explained how to pass an initial value, since the value pointer is deprecated. Added more detailed explanations about callback arguments in C, ensuring that users understand how to access and use them in Trackbar callbacks. Added a note on how to properly handle initial value passing without relying on the deprecated value pointer. Updated Tutorial Example: Renamed and used callback function parameters to make them more understandable. Included a demonstration on how to utilize userdata in the callback function. Additional Notes: Removed reliance on the value pointer for updating trackbar values. Users are now encouraged to use other mechanisms as per the current implementation to avoid the runtime warning. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [] The feature is well documented and sample code can be built with the project CMake
1 parent 1f2e7ad commit 3a8d7ec

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

apps/createsamples/utility.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,6 @@ CvBackgroundData* icvCreateBackgroundData( const char* filename, Size winsize )
766766
}
767767
if( count > 0 )
768768
{
769-
//rewind( input );
770769
fseek( input, 0, SEEK_SET );
771770
datasize += sizeof( *data ) + sizeof( char* ) * count;
772771
data = (CvBackgroundData*) fastMalloc( datasize );
@@ -872,8 +871,6 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data,
872871

873872
reader->src = img;
874873

875-
//reader->offset.x = round % data->winsize.width;
876-
//reader->offset.y = round / data->winsize.width;
877874
reader->offset = offset;
878875
reader->point = reader->offset;
879876
reader->scale = MAX(

modules/highgui/include/opencv2/highgui.hpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,20 @@ control panel.
531531
Clicking the label of each trackbar enables editing the trackbar values manually.
532532
533533
@param trackbarname Name of the created trackbar.
534-
@param winname Name of the window that will be used as a parent of the created trackbar.
535-
@param value Optional pointer to an integer variable whose value reflects the position of the
536-
slider. Upon creation, the slider position is defined by this variable.
537-
@param count Maximal position of the slider. The minimal position is always 0.
538-
@param onChange Pointer to the function to be called every time the slider changes position. This
539-
function should be prototyped as void Foo(int,void\*); , where the first parameter is the trackbar
540-
position and the second parameter is the user data (see the next parameter). If the callback is
541-
the NULL pointer, no callbacks are called, but only value is updated.
542-
@param userdata User data that is passed as is to the callback. It can be used to handle trackbar
543-
events without using global variables.
534+
@param winname Name of the window that will contain the trackbar.
535+
@param value Pointer to the integer value that will be changed by the trackbar.
536+
Pass `nullptr` if the value pointer is not used. In this case, manually handle
537+
the trackbar position in the callback function.
538+
@param count Maximum position of the trackbar.
539+
@param onChange Pointer to the function to be called every time the slider changes position.
540+
This function should have the prototype void Foo(int, void\*);, where the first parameter is
541+
the trackbar position, and the second parameter is the user data (see the next parameter).
542+
If the callback is a nullptr, no callbacks are called, but the trackbar's value will still be
543+
updated automatically.
544+
@param userdata Optional user data that is passed to the callback.
545+
@note If the `value` pointer is `nullptr`, the trackbar position must be manually managed.
546+
Call the callback function manually with the desired initial value to avoid runtime warnings.
547+
@see #tutorial_trackbar
544548
*/
545549
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,
546550
int* value, int count,

samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,48 @@ Mat dst;
2727
* @function on_trackbar
2828
* @brief Callback for trackbar
2929
*/
30-
static void on_trackbar( int, void* )
31-
{
32-
alpha = (double) alpha_slider/alpha_slider_max ;
33-
beta = ( 1.0 - alpha );
34-
addWeighted( src1, alpha, src2, beta, 0.0, dst);
35-
imshow( "Linear Blend", dst );
30+
static void on_trackbar(int pos, void* userdata) {
31+
(void) userdata;
32+
alpha = (double)pos / alpha_slider_max;
33+
beta = (1.0 - alpha);
34+
addWeighted(src1, alpha, src2, beta, 0.0, dst);
35+
imshow("Linear Blend", dst);
3636
}
3737
//![on_trackbar]
3838

3939
/**
4040
* @function main
4141
* @brief Main function
4242
*/
43-
int main( void )
43+
int main(void)
4444
{
45-
//![load]
46-
/// Read images ( both have to be of the same size and type )
47-
src1 = imread( samples::findFile("LinuxLogo.jpg") );
48-
src2 = imread( samples::findFile("WindowsLogo.jpg") );
49-
//![load]
45+
//![load]
46+
/// Read images (both must be of the same size and type)
47+
src1 = imread(samples::findFile("LinuxLogo.jpg"));
48+
src2 = imread(samples::findFile("WindowsLogo.jpg"));
49+
//![load]
5050

51-
if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
52-
if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
51+
if (src1.empty()) { cout << "Error loading src1 \n"; return -1; }
52+
if (src2.empty()) { cout << "Error loading src2 \n"; return -1; }
5353

54-
/// Initialize values
55-
alpha_slider = 0;
54+
// Initialize trackbar value
55+
alpha_slider = 0;
5656

57-
//![window]
58-
namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
59-
//![window]
57+
//![window]
58+
namedWindow("Linear Blend", WINDOW_AUTOSIZE); //Create Window
59+
//![window]
6060

61-
//![create_trackbar]
62-
char TrackbarName[50];
63-
snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max );
64-
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
65-
//![create_trackbar]
61+
//![create_trackbar]
62+
char TrackbarName[50];
63+
snprintf(TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max);
64+
// Example userdata: Pass a pointer to an integer as userdata
65+
createTrackbar(TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar);
66+
//![create_trackbar]
6667

67-
/// Show some stuff
68-
on_trackbar( alpha_slider, 0 );
68+
/// Show initial result
69+
on_trackbar(alpha_slider, nullptr);
6970

70-
/// Wait until user press some key
71-
waitKey(0);
72-
return 0;
71+
/// Wait for user input
72+
waitKey(0);
73+
return 0;
7374
}

0 commit comments

Comments
 (0)