- Building Computer Vision Projects with OpenCV 4 and C++
- David Millán Escrivá Prateek Joshi Vinícius G. Mendon?a Roy Shilkrot
- 473字
- 2021-07-02 12:28:38
Creating the graphical user interface
Before we start with the image processing algorithms, we create the main user interface for our application. We are going to use the Qt-based user interface to allow us to create single buttons. The application receives one input parameter to load the image to process, and we are going to create four buttons, as follows:
- Show histogram
- Equalize histogram
- Lomography effect
- Cartoonize effect
We can see the four results in the following screenshot:

Let's begin developing our project. First of all, we are going to include the OpenCV – required headers, define an image matrix to store the input image, and create a constant string to use the new command-line parser already available from OpenCV 3.0; in this constant, we allow only two input parameters, help and the required image input:
// OpenCV includes #include "opencv2/core/utility.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; // OpenCV command line parser functions // Keys accepted by command line parser const char* keys = { "{help h usage ? | | print this message}" "{@image | | Image to process}" };
The main function starts with the command-line parser variable; next, we set the about instruction and print the help message. This line sets up the help instructions of our final executable:
int main(int argc, const char** argv) { CommandLineParser parser(argc, argv, keys); parser.about("Chapter 4. PhotoTool v1.0.0"); //If requires help show if (parser.has("help")) { parser.printMessage(); return 0; }
If the user doesn't require help, then we have to get the file path image in the imgFile variable string and check that all required parameters are added with the parser.check() function:
String imgFile= parser.get<String>(0); // Check if params are correctly parsed in his variables if (!parser.check()) { parser.printErrors(); return 0; }
Now, we can read the image file with the imread function, and then create the window in which the input image will be shown later with the namedWindow function:
// Load image to process Mat img= imread(imgFile); // Create window namedWindow("Input");
With the image loaded and the window created, we only need to create the buttons for our interface and link them with the callback functions; each callback function is defined in the source code and we are going to explain these functions later in this chapter. We are going to create the buttons with the createButton function with the QT_PUSH_BUTTON constant to button style:
// Create UI buttons createButton("Show histogram", showHistoCallback, NULL, QT_PUSH_BUTTON, 0); createButton("Equalize histogram", equalizeCallback, NULL, QT_PUSH_BUTTON, 0); createButton("Lomography effect", lomoCallback, NULL, QT_PUSH_BUTTON, 0); createButton("Cartoonize effect", cartoonCallback, NULL, QT_PUSH_BUTTON, 0);
To finish our main function, we show the input image and wait for a key press to finish our application:
// Show image imshow("Input", img); waitKey(0); return 0;
Now, we only have to define each callback function, and in the next sections, we are going to do just that.
- GitHub Essentials
- 算法競賽入門經(jīng)典:習(xí)題與解答
- DB29forLinux,UNIX,Windows數(shù)據(jù)庫管理認(rèn)證指南
- 大話Oracle Grid:云時代的RAC
- 高維數(shù)據(jù)分析預(yù)處理技術(shù)
- SQL Server深入詳解
- 新手學(xué)會計(2013-2014實戰(zhàn)升級版)
- Oracle數(shù)據(jù)庫管理、開發(fā)與實踐
- SAS金融數(shù)據(jù)挖掘與建模:系統(tǒng)方法與案例解析
- 聯(lián)動Oracle:設(shè)計思想、架構(gòu)實現(xiàn)與AWR報告
- 二進制分析實戰(zhàn)
- openGauss數(shù)據(jù)庫核心技術(shù)
- SQL Server 2012數(shù)據(jù)庫技術(shù)及應(yīng)用(第4版)
- MySQL 8.0從入門到實戰(zhàn)
- Oracle 11g數(shù)據(jù)庫系統(tǒng)設(shè)計、開發(fā)、管理與應(yīng)用