当前位置:网站首页>How to use scroll bars to dynamically adjust parameters in opencv
How to use scroll bars to dynamically adjust parameters in opencv
2022-07-06 17:42:00 【Xiaobai learns vision】
Click on the above “ Xiaobai studies vision ”, Optional plus " Star standard " or “ Roof placement ”
Heavy dry goods , First time delivery
OpenCV There are many functions in the image processing have some parameters, you can have multiple choices , At this time, if developers like fast trial and error , When you find the best combination or type of parameter .OpenCV Pass through HighGUI The scrollbar provides such a convenient debugging method , It's just OpenCV Official tutorial inside the scroll bar code implementation is relatively simple , Even a little rough . This article will further improve on the basis of the official tutorial , Get a more general version of the code .
function
OpenCV Use the scroll bar in the , First you need to create a window , Then create the scroll bar , The scrollbar itself is attached to the window , The function to create a scroll bar is as follows :
int cv::createTrackbar(
const String & trackbarname,
const String & winname,
int * value,
int count,
TrackbarCallback onChange = 0,
void * userdata = 0
)
Parameter interpretation
trackbarname Indicates the name of the scroll bar
winname Represents the name of the corresponding attached window
value Represents the value on the scroll bar
count Represents the maximum value of the scroll bar range , The value range is [0, count]
onChange A response handler that represents something that happens when you drag a scroll bar , You need to customize it
userdata Express Whether to pass parameters to the event handler , Supports pointers of unsigned type
Basic usage of scroll bar - Dynamically adjust parameters
Use the scroll bar to adjust the brightness dynamically
First create an input image window , And then call createTrackbar Create a scroll bar to attach to the window , Bind the defined function onchange, among onchange It's essentially an event callback function , It is defined in the following format :
typedef void(* cv::TrackbarCallback) (int pos, void *userdata)
among pos Returns the position of the current scroll bar slider ,userdata Is the user data parameter passed back by this method . here onchange The function definition and implementation are as follows :
int max_count = 150;
int cnt = 50;
Mat temp;
void onchange(int, void*) {
Mat m = Mat::zeros(temp.size(), temp.type());
m = Scalar(cnt, cnt, cnt);
Mat dst;
add(temp, m, dst);
imshow(" Brightness adjustment ", dst);
}
Create scroll bar and bind callback function , Display window and image code
Mat image = imread("D:/images/1024.png"); // B, G, R
if (image.empty()) {
printf("could not load image....\n");
return -1;
}
temp = image.clone();
namedWindow(" Input window ", WINDOW_FREERATIO);
createTrackbar("My Bar:", " Input window ", &cnt, max_count, onchange);
imshow(" Input window ", image);
onchange(50, 0);
waitKey(0);
The operation results are as follows :
Scroll bar advanced usage - Parameter passing
Adjust image brightness and contrast dynamically
The above example follows OpenCV The official tutorial is very similar , The disadvantage is to define a bunch of global temporary variables , Not a good programming habit . and userdata This parameter is not fully utilized , So I reorganized the code , The brightness and contrast of the image are adjusted , utilize userdata To pass parameters , Kill this bunch of temporary variables .
The code implementation first creates two trackbar, One to adjust the brightness , One to adjust the contrast , The two functions are bound to each other , And then go through userdata Pass on Mat object , Through the callback function pos Parameter to get the position of the scroll bar slider , Data acquisition , So let's look at this first , Two scrollbar callback event binding function code implementation :
static void on_lightness(int pos, void* userdata) {
Mat image = *((Mat*)userdata);
Mat dst = Mat::zeros(image.size(), image.type());
Mat m = Mat::zeros(image.size(), image.type());
addWeighted(image, 1.0, m, 0, pos, dst);
imshow(" Brightness and contrast adjustment ", dst);
}
static void on_contrast(int pos, void* userdata) {
Mat image = *((Mat*)userdata);
Mat dst = Mat::zeros(image.size(), image.type());
Mat m = Mat::zeros(image.size(), image.type());
double contrast = pos / 100.0;
addWeighted(image, contrast, m, 0.0, 0, dst);
imshow(" Brightness and contrast adjustment ", dst);
}
Create a scroll bar , Binding event functions , Display window code implementation as follows :
Hello
int main(int argc, char** argv) {
Mat image = imread("D:/images/1024.png"); // B, G, R
if (image.empty()) {
printf("could not load image....\n");
return -1;
}
imshow(" Input window ", image);
namedWindow(" Brightness and contrast adjustment ", WINDOW_AUTOSIZE);
int lightness = 50;
int max_value = 100;
int contrast_value = 100;
createTrackbar("Value Bar:", " Brightness and contrast adjustment ", &lightness, max_value, on_lightness, (void*)(&image));
createTrackbar("Contrast Bar:", " Brightness and contrast adjustment ", &contrast_value, 200, on_contrast, (void*)(&image));
on_lightness(50, &image);
waitKey(0);
return 0;
}
among userdata The pass in parameter is required , Convert to void* I.e. no type pointer / Any type of pointer , Through this line of code in the event function :
Mat image = *((Mat*)userdata);
First convert to Mat Type a pointer , And then it's converted to data usage .
The operation results are as follows :
The good news !
Xiaobai learns visual knowledge about the planet
Open to the outside world
download 1:OpenCV-Contrib Chinese version of extension module
stay 「 Xiaobai studies vision 」 Official account back office reply : Extension module Chinese course , You can download the first copy of the whole network OpenCV Extension module tutorial Chinese version , Cover expansion module installation 、SFM Algorithm 、 Stereo vision 、 Target tracking 、 Biological vision 、 Super resolution processing and other more than 20 chapters .
download 2:Python Visual combat project 52 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :Python Visual combat project , You can download, including image segmentation 、 Mask detection 、 Lane line detection 、 Vehicle count 、 Add Eyeliner 、 License plate recognition 、 Character recognition 、 Emotional tests 、 Text content extraction 、 Face recognition, etc 31 A visual combat project , Help fast school computer vision .
download 3:OpenCV Actual project 20 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :OpenCV Actual project 20 speak , You can download the 20 Based on OpenCV Realization 20 A real project , Realization OpenCV Learn advanced .
Communication group
Welcome to join the official account reader group to communicate with your colleagues , There are SLAM、 3 d visual 、 sensor 、 Autopilot 、 Computational photography 、 testing 、 Division 、 distinguish 、 Medical imaging 、GAN、 Wechat groups such as algorithm competition ( It will be subdivided gradually in the future ), Please scan the following micro signal clustering , remarks :” nickname + School / company + Research direction “, for example :” Zhang San + Shanghai Jiaotong University + Vision SLAM“. Please note... According to the format , Otherwise, it will not pass . After successful addition, they will be invited to relevant wechat groups according to the research direction . Please do not send ads in the group , Or you'll be invited out , Thanks for your understanding ~
边栏推荐
- How uipath determines that an object is null
- 2022年大厂Android面试题汇总(二)(含答案)
- Kernel link script parsing
- Automatic operation and maintenance sharp weapon ansible Foundation
- 07 personal R & D products and promotion - human resources information management system
- PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口
- 【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符
- 03个人研发的产品及推广-计划服务配置器V3.0
- Total / statistics function of MySQL
- pip install pyodbc : ERROR: Command errored out with exit status 1
猜你喜欢
轻量级计划服务工具研发与实践
Flink parsing (III): memory management
C # nanoframework lighting and key esp32
OpenCV中如何使用滚动条动态调整参数
04个人研发的产品及推广-数据推送工具
关于Selenium启动Chrome浏览器闪退问题
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
C WinForm series button easy to use
Wordcloud colormap color set and custom colors
[ASM] introduction and use of bytecode operation classwriter class
随机推荐
Remote code execution penetration test - B module test
[reverse intermediate] eager to try
Wordcloud colormap color set and custom colors
How uipath determines that an object is null
C version selenium operation chrome full screen mode display (F11)
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
Single responsibility principle
Hongmeng introduction and development environment construction
全网最全tcpdump和Wireshark抓包实践
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
pip install pyodbc : ERROR: Command errored out with exit status 1
Binary search strategy
yarn : 无法加载文件 D:\ProgramFiles\nodejs\yarn.ps1,因为在此系统上禁止运行脚本
[ciscn 2021 South China]rsa writeup
Grafana 9 is officially released, which is easier to use and more cool!
Concept and basic knowledge of network layering
灵活报表v1.0(简单版)
MySQL stored procedure
06个人研发的产品及推广-代码统计工具
当前系统缺少NTFS格式转换器(convert.exe)