From 3a8d7ec75a4c8fd485cd3cf17b973e8cce60e34b Mon Sep 17 00:00:00 2001 From: MurtazaSaherwala <149921079+MurtazaSaherwala@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:53:07 +0530 Subject: [PATCH] Merge pull request #26524 from MurtazaSaherwala:DocumentationUpdation Updated trackbar callback function and improved documentation #26524 This Fixes #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 --- apps/createsamples/utility.cpp | 3 - modules/highgui/include/opencv2/highgui.hpp | 24 ++++---- .../HighGUI/AddingImagesTrackbar.cpp | 59 ++++++++++--------- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/apps/createsamples/utility.cpp b/apps/createsamples/utility.cpp index b57d3fcb81..87ba4eac5a 100644 --- a/apps/createsamples/utility.cpp +++ b/apps/createsamples/utility.cpp @@ -766,7 +766,6 @@ CvBackgroundData* icvCreateBackgroundData( const char* filename, Size winsize ) } if( count > 0 ) { - //rewind( input ); fseek( input, 0, SEEK_SET ); datasize += sizeof( *data ) + sizeof( char* ) * count; data = (CvBackgroundData*) fastMalloc( datasize ); @@ -872,8 +871,6 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data, reader->src = img; - //reader->offset.x = round % data->winsize.width; - //reader->offset.y = round / data->winsize.width; reader->offset = offset; reader->point = reader->offset; reader->scale = MAX( diff --git a/modules/highgui/include/opencv2/highgui.hpp b/modules/highgui/include/opencv2/highgui.hpp index 35b64bceae..dec9af5b46 100644 --- a/modules/highgui/include/opencv2/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui.hpp @@ -531,16 +531,20 @@ control panel. Clicking the label of each trackbar enables editing the trackbar values manually. @param trackbarname Name of the created trackbar. -@param winname Name of the window that will be used as a parent of the created trackbar. -@param 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. -@param count Maximal position of the slider. The minimal position is always 0. -@param onChange 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 (see the next parameter). If the callback is -the NULL pointer, no callbacks are called, but only value is updated. -@param userdata User data that is passed as is to the callback. It can be used to handle trackbar -events without using global variables. +@param winname Name of the window that will contain the trackbar. +@param value Pointer to the integer value that will be changed by the trackbar. +Pass `nullptr` if the value pointer is not used. In this case, manually handle +the trackbar position in the callback function. +@param count Maximum position of the trackbar. +@param onChange Pointer to the function to be called every time the slider changes position. +This function should have the prototype void Foo(int, void\*);, where the first parameter is +the trackbar position, and the second parameter is the user data (see the next parameter). +If the callback is a nullptr, no callbacks are called, but the trackbar's value will still be +updated automatically. +@param userdata Optional user data that is passed to the callback. +@note If the `value` pointer is `nullptr`, the trackbar position must be manually managed. +Call the callback function manually with the desired initial value to avoid runtime warnings. +@see #tutorial_trackbar */ CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname, int* value, int count, diff --git a/samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp b/samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp index 82bbb3743a..3650b15dea 100644 --- a/samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp +++ b/samples/cpp/tutorial_code/HighGUI/AddingImagesTrackbar.cpp @@ -27,12 +27,12 @@ Mat dst; * @function on_trackbar * @brief Callback for trackbar */ -static void on_trackbar( int, void* ) -{ - alpha = (double) alpha_slider/alpha_slider_max ; - beta = ( 1.0 - alpha ); - addWeighted( src1, alpha, src2, beta, 0.0, dst); - imshow( "Linear Blend", dst ); +static void on_trackbar(int pos, void* userdata) { + (void) userdata; + alpha = (double)pos / alpha_slider_max; + beta = (1.0 - alpha); + addWeighted(src1, alpha, src2, beta, 0.0, dst); + imshow("Linear Blend", dst); } //![on_trackbar] @@ -40,34 +40,35 @@ static void on_trackbar( int, void* ) * @function main * @brief Main function */ -int main( void ) +int main(void) { - //![load] - /// Read images ( both have to be of the same size and type ) - src1 = imread( samples::findFile("LinuxLogo.jpg") ); - src2 = imread( samples::findFile("WindowsLogo.jpg") ); - //![load] + //![load] + /// Read images (both must be of the same size and type) + src1 = imread(samples::findFile("LinuxLogo.jpg")); + src2 = imread(samples::findFile("WindowsLogo.jpg")); + //![load] - if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; } - if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; } + if (src1.empty()) { cout << "Error loading src1 \n"; return -1; } + if (src2.empty()) { cout << "Error loading src2 \n"; return -1; } - /// Initialize values - alpha_slider = 0; + // Initialize trackbar value + alpha_slider = 0; - //![window] - namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window - //![window] + //![window] + namedWindow("Linear Blend", WINDOW_AUTOSIZE); //Create Window + //![window] - //![create_trackbar] - char TrackbarName[50]; - snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max ); - createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar ); - //![create_trackbar] + //![create_trackbar] + char TrackbarName[50]; + snprintf(TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max); + // Example userdata: Pass a pointer to an integer as userdata + createTrackbar(TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar); + //![create_trackbar] - /// Show some stuff - on_trackbar( alpha_slider, 0 ); + /// Show initial result + on_trackbar(alpha_slider, nullptr); - /// Wait until user press some key - waitKey(0); - return 0; + /// Wait for user input + waitKey(0); + return 0; }