├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── ch01
└── read-me.txt
├── ch02
└── Hello_Qt_OpenCV
│ ├── Hello_Qt_OpenCV.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ └── mainwindow.ui
├── ch03
├── Hello_Qt_OpenCV
│ ├── Hello_Qt_OpenCV.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── resources.qrc
│ ├── test.jpg
│ ├── translation_de.qm
│ ├── translation_de.ts
│ ├── translation_tr.qm
│ └── translation_tr.ts
├── Plugin_User
│ ├── Plugin_User.pro
│ ├── cvplugininterface.h
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ └── mainwindow.ui
├── cvplugininterface.h
└── median_filter_plugin
│ ├── cvplugininterface.h
│ ├── median_filter_plugin.cpp
│ ├── median_filter_plugin.h
│ ├── median_filter_plugin.pro
│ └── median_filter_plugin_global.h
├── ch04
├── ImageViewer
│ ├── ImageViewer.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ └── mainwindow.ui
└── Painter_Test
│ ├── Painter_Test.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── qblinkingwidget.cpp
│ └── qblinkingwidget.h
├── ch05
├── Graphics_Viewer
│ ├── Graphics_Viewer.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── qcustomgraphicseffect.cpp
│ ├── qcustomgraphicseffect.h
│ ├── qenhancedgraphicsview.cpp
│ └── qenhancedgraphicsview.h
└── computer_vision
│ ├── compute_vision.pro
│ ├── cvplugininterface
│ └── cvplugininterface.h
│ ├── mainapp
│ ├── language_tr.qm
│ ├── language_tr.ts
│ ├── main.cpp
│ ├── mainapp.pro
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── qenhancedgraphicsview.cpp
│ └── qenhancedgraphicsview.h
│ ├── resources
│ ├── languages
│ │ └── Turkish - TR.qm
│ ├── readme.txt
│ └── themes
│ │ ├── Dark.thm
│ │ └── Light.thm
│ └── template_plugin
│ ├── plugin.ui
│ ├── template_plugin.cpp
│ ├── template_plugin.h
│ ├── template_plugin.pro
│ └── template_plugin_global.h
├── ch06
├── color_plugin
│ ├── color_plugin.cpp
│ ├── color_plugin.h
│ ├── color_plugin.pro
│ ├── color_plugin_global.h
│ └── plugin.ui
├── copymakeborder_plugin
│ ├── copymakeborder_plugin.cpp
│ ├── copymakeborder_plugin.h
│ ├── copymakeborder_plugin.pro
│ ├── copymakeborder_plugin_global.h
│ └── plugin.ui
├── filter_plugin
│ ├── filter_plugin.cpp
│ ├── filter_plugin.h
│ ├── filter_plugin.pro
│ ├── filter_plugin_global.h
│ └── plugin.ui
├── fourier_plugin
│ ├── fourier_plugin.cpp
│ ├── fourier_plugin.h
│ ├── fourier_plugin.pro
│ ├── fourier_plugin_global.h
│ └── plugin.ui
├── segmentation_plugin
│ ├── plugin.ui
│ ├── segmentation_plugin.cpp
│ ├── segmentation_plugin.h
│ ├── segmentation_plugin.pro
│ └── segmentation_plugin_global.h
└── transform_plugin
│ ├── plugin.ui
│ ├── transform_plugin.cpp
│ ├── transform_plugin.h
│ ├── transform_plugin.pro
│ └── transform_plugin_global.h
├── ch07
└── keypoint_plugin
│ ├── keypoint_plugin.cpp
│ ├── keypoint_plugin.h
│ ├── keypoint_plugin.pro
│ ├── keypoint_plugin_global.h
│ └── plugin.ui
├── ch08
└── MultithreadedCV
│ ├── MultithreadedCV.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── videoprocessor.cpp
│ ├── videoprocessor.h
│ ├── videoprocessorthread.cpp
│ └── videoprocessorthread.h
├── ch09
├── BackgroundDetect
│ ├── BackgroundDetect.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── qcvbackgrounddetect.cpp
│ └── qcvbackgrounddetect.h
├── MeanShiftTracker
│ ├── MeanShiftTracker.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── qcvmeanshiftthread.cpp
│ └── qcvmeanshiftthread.h
└── histogram_plugin
│ ├── histogram_plugin.cpp
│ ├── histogram_plugin.h
│ ├── histogram_plugin.pro
│ ├── histogram_plugin_global.h
│ └── plugin.ui
├── ch10
├── GuiTest
│ ├── GuiTest.pro
│ ├── testableform.cpp
│ ├── testableform.h
│ ├── testableform.ui
│ └── tst_guitesttest.cpp
└── HelloTest
│ ├── HelloTest.pro
│ ├── pixelcounter.cpp
│ ├── pixelcounter.h
│ └── tst_hellotesttest.cpp
└── ch12
└── CvQml
├── CvQml.pro
├── main.cpp
├── main.qml
├── qimageprocessor.cpp
├── qimageprocessor.h
├── qimageviewer.cpp
├── qimageviewer.h
├── qml.qrc
└── qtquickcontrols2.conf
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear in the root of a volume
35 | .DocumentRevisions-V100
36 | .fseventsd
37 | .Spotlight-V100
38 | .TemporaryItems
39 | .Trashes
40 | .VolumeIcon.icns
41 |
42 | # Directories potentially created on remote AFP share
43 | .AppleDB
44 | .AppleDesktop
45 | Network Trash Folder
46 | Temporary Items
47 | .apdisk
48 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # Computer Vision with OpenCV 3 and Qt5
5 | This is the code repository for [Computer Vision with OpenCV 3 and Qt5](https://www.packtpub.com/application-development/computer-vision-opencv-3-and-qt5?utm_source=github&utm_medium=repository&utm_campaign=9781788472395), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish.
6 | ## About the Book
7 | Developers have been using OpenCV library to develop computer vision applications for a long time. However, they now need a more effective tool to get the job done and in a much better and modern way. Qt is one of the major frameworks available for this task at the moment.
8 | ## Instructions and Navigation
9 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02.
10 |
11 | Chapter 11 has no code file, rest all chapter code files are present in their respective folder.
12 |
13 | The code will look like the following:
14 | ```
15 | #include "mainwindow.h"
16 | #include
17 | int main(int argc, char *argv[])
18 | {
19 | QApplication a(argc, argv);
20 | MainWindow w;
21 | w.show();
22 | return a.exec();
23 | }
24 | ```
25 |
26 | Although every required tool and software, the correct version, and how it is installed and configured is covered in the initial chapters of the book, the following is a list that can be used as a quick reference:
27 | * A regular computer with a more recent version of Windows, macOS, or Linux (such as Ubuntu) operating system installed on it.
28 | * Microsoft Visual Studio (on Windows)
29 | * Xcode (on macOS)
30 | * CMake
31 | * Qt Framework
32 | * OpenCV Framework
33 | To get an idea of what a regular computer is these days, you can search online or ask a local
34 | shop; however, the one you already have is most probably enough to get you started.
35 |
36 | ## Related Products
37 | * [Mastering OpenCV 3 - Second Edition](https://www.packtpub.com/application-development/mastering-opencv-3-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781786467171)
38 |
39 | * [Machine Learning for OpenCV](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv?utm_source=github&utm_medium=repository&utm_campaign=9781783980284)
40 |
41 | * [Computer Vision with Python 3](https://www.packtpub.com/application-development/computer-vision-python-3?utm_source=github&utm_medium=repository&utm_campaign=9781788299763)
42 |
43 | ### Suggestions and Feedback
44 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions.
45 | ### Download a free PDF
46 |
47 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to download a free PDF copy of this book.
48 |
https://packt.link/free-ebook/9781788472395
--------------------------------------------------------------------------------
/ch01/read-me.txt:
--------------------------------------------------------------------------------
1 | Create and test the projects for chapter 1 as they are mentioned in the book.
2 | They are meant to test your installation.
--------------------------------------------------------------------------------
/ch02/Hello_Qt_OpenCV/Hello_Qt_OpenCV.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-08-27T11:21:12
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = Hello_Qt_OpenCV
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp
29 |
30 | HEADERS += \
31 | mainwindow.h
32 |
33 | FORMS += \
34 | mainwindow.ui
35 |
36 | win32: {
37 | include(c:/dev/opencv/opencv.pri)
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += /usr/local/include
47 | LIBS += -L/usr/local/lib \
48 | -lopencv_world
49 | }
50 |
--------------------------------------------------------------------------------
/ch02/Hello_Qt_OpenCV/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch02/Hello_Qt_OpenCV/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | loadSettings();
10 | }
11 |
12 | MainWindow::~MainWindow()
13 | {
14 | delete ui;
15 | }
16 |
17 | void MainWindow::on_inputPushButton_pressed()
18 | {
19 | QString fileName = QFileDialog::getOpenFileName(this, "Open Input Image", QDir::currentPath(), "Images (*.jpg *.png *.bmp)");
20 | if(QFile::exists(fileName))
21 | {
22 | ui->inputLineEdit->setText(fileName);
23 | }
24 | }
25 |
26 | void MainWindow::on_outputPushButton_pressed()
27 | {
28 | QString fileName = QFileDialog::getSaveFileName(this, "Select Output Image", QDir::currentPath(), "*.jpg;;*.png;;*.bmp");
29 | if(!fileName.isEmpty())
30 | {
31 | ui->outputLineEdit->setText(fileName);
32 | using namespace cv;
33 | Mat inpImg, outImg;
34 | inpImg = imread(ui->inputLineEdit->text().toStdString());
35 | if(ui->medianBlurRadioButton->isChecked())
36 | cv::medianBlur(inpImg, outImg, 5);
37 | else if(ui->gaussianBlurRadioButton->isChecked())
38 | cv::GaussianBlur(inpImg, outImg, Size(5, 5), 1.25);
39 | imwrite(fileName.toStdString(), outImg);
40 | if(ui->displayImageCheckBox->isChecked())
41 | imshow("Output Image", outImg);
42 | }
43 | }
44 |
45 | void MainWindow::closeEvent(QCloseEvent *event)
46 | {
47 | int result = QMessageBox::warning(this, "Exit", "Are you sure you want to close this program?", QMessageBox::Yes, QMessageBox::No);
48 | if(result == QMessageBox::Yes)
49 | {
50 | saveSettings();
51 | event->accept();
52 | }
53 | else
54 | {
55 | event->ignore();
56 | }
57 | }
58 |
59 | void MainWindow::loadSettings()
60 | {
61 | QSettings settings("Packt", "Hello_OpenCV_Qt", this);
62 | ui->inputLineEdit->setText(settings.value("inputLineEdit", "").toString());
63 | ui->outputLineEdit->setText(settings.value("outputLineEdit", "").toString());
64 | ui->medianBlurRadioButton->setChecked(settings.value("medianBlurRadioButton", true).toBool());
65 | ui->gaussianBlurRadioButton->setChecked(settings.value("gaussianBlurRadioButton", false).toBool());
66 | ui->displayImageCheckBox->setChecked(settings.value("displayImageCheckBox", false).toBool());
67 | }
68 |
69 | void MainWindow::saveSettings()
70 | {
71 | QSettings settings("Packt", "Hello_OpenCV_Qt", this);
72 | settings.setValue("inputLineEdit", ui->inputLineEdit->text());
73 | settings.setValue("outputLineEdit", ui->outputLineEdit->text());
74 | settings.setValue("medianBlurRadioButton", ui->medianBlurRadioButton->isChecked());
75 | settings.setValue("gaussianBlurRadioButton", ui->gaussianBlurRadioButton->isChecked());
76 | settings.setValue("displayImageCheckBox", ui->displayImageCheckBox->isChecked());
77 | }
78 |
--------------------------------------------------------------------------------
/ch02/Hello_Qt_OpenCV/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include "opencv2/opencv.hpp"
12 |
13 | namespace Ui {
14 | class MainWindow;
15 | }
16 |
17 | class MainWindow : public QMainWindow
18 | {
19 | Q_OBJECT
20 |
21 | public:
22 | explicit MainWindow(QWidget *parent = 0);
23 | ~MainWindow();
24 |
25 | protected:
26 | void closeEvent(QCloseEvent *event);
27 |
28 | private slots:
29 | void on_inputPushButton_pressed();
30 |
31 | void on_outputPushButton_pressed();
32 |
33 | private:
34 | Ui::MainWindow *ui;
35 |
36 | void loadSettings();
37 | void saveSettings();
38 |
39 | };
40 |
41 | #endif // MAINWINDOW_H
42 |
--------------------------------------------------------------------------------
/ch02/Hello_Qt_OpenCV/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 446
10 | 187
11 |
12 |
13 |
14 | Hello_Qt_OpenCV
15 |
16 |
17 |
18 | -
19 |
20 |
-
21 |
22 |
23 | Input Image :
24 |
25 |
26 |
27 | -
28 |
29 |
30 | -
31 |
32 |
33 | Browse
34 |
35 |
36 |
37 |
38 |
39 | -
40 |
41 |
42 | Filter type
43 |
44 |
45 |
-
46 |
47 |
48 | Median Blur
49 |
50 |
51 | true
52 |
53 |
54 |
55 | -
56 |
57 |
58 | Gaussian Blur
59 |
60 |
61 |
62 |
63 |
64 |
65 | -
66 |
67 |
-
68 |
69 |
70 | Output Image :
71 |
72 |
73 |
74 | -
75 |
76 |
77 | -
78 |
79 |
80 | Browse
81 |
82 |
83 |
84 |
85 |
86 | -
87 |
88 |
89 | Display Image After Saving
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/Hello_Qt_OpenCV.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-08-27T11:21:12
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = Hello_Qt_OpenCV
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp
29 |
30 | HEADERS += \
31 | mainwindow.h
32 |
33 | FORMS += \
34 | mainwindow.ui
35 |
36 | win32: {
37 | include(c:/dev/opencv/opencv.pri)
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += /usr/local/include
47 | LIBS += -L/usr/local/lib \
48 | -lopencv_world
49 | }
50 |
51 | RESOURCES += \
52 | resources.qrc
53 |
54 | TRANSLATIONS = translation_de.ts translation_tr.ts
55 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 | return a.exec();
10 | }
11 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | loadSettings();
10 |
11 | turkishTranslator = new QTranslator(this);
12 | turkishTranslator->load(":/translations/translation_tr.qm");
13 |
14 | germanTranslator = new QTranslator(this);
15 | germanTranslator->load(":/translations/translation_de.qm");
16 | }
17 |
18 | MainWindow::~MainWindow()
19 | {
20 | delete ui;
21 | }
22 |
23 | void MainWindow::on_inputPushButton_pressed()
24 | {
25 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open Input Image"), QDir::currentPath(), tr("Images") + " (*.jpg *.png *.bmp)");
26 | if(QFile::exists(fileName))
27 | {
28 | ui->inputLineEdit->setText(fileName);
29 | }
30 | }
31 |
32 | void MainWindow::on_outputPushButton_pressed()
33 | {
34 | QString fileName = QFileDialog::getSaveFileName(this, tr("Select Output Image"), QDir::currentPath(), "*.jpg;;*.png;;*.bmp");
35 | if(!fileName.isEmpty())
36 | {
37 | ui->outputLineEdit->setText(fileName);
38 | using namespace cv;
39 |
40 | Mat inpImg, outImg;
41 | inpImg = imread(ui->inputLineEdit->text().toStdString());
42 | if(ui->medianBlurRadioButton->isChecked())
43 | cv::medianBlur(inpImg, outImg, 5);
44 | else if(ui->gaussianBlurRadioButton->isChecked())
45 | cv::GaussianBlur(inpImg, outImg, Size(5, 5), 1.25);
46 | imwrite(fileName.toStdString(), outImg);
47 | if(ui->displayImageCheckBox->isChecked())
48 | imshow(tr("Output Image").toStdString(), outImg);
49 | }
50 | }
51 |
52 | void MainWindow::closeEvent(QCloseEvent *event)
53 | {
54 | int result = QMessageBox::warning(this, tr("Exit"), tr("Are you sure you want to close this program?"), QMessageBox::Yes, QMessageBox::No);
55 | if(result == QMessageBox::Yes)
56 | {
57 | saveSettings();
58 | event->accept();
59 | }
60 | else
61 | {
62 | event->ignore();
63 | }
64 | }
65 |
66 | void MainWindow::loadSettings()
67 | {
68 | QSettings settings("Packt", "Hello_OpenCV_Qt", this);
69 | ui->inputLineEdit->setText(settings.value("inputLineEdit", "").toString());
70 | ui->outputLineEdit->setText(settings.value("outputLineEdit", "").toString());
71 | ui->medianBlurRadioButton->setChecked(settings.value("medianBlurRadioButton", true).toBool());
72 | ui->gaussianBlurRadioButton->setChecked(settings.value("gaussianBlurRadioButton", false).toBool());
73 | ui->displayImageCheckBox->setChecked(settings.value("displayImageCheckBox", false).toBool());
74 | }
75 |
76 | void MainWindow::saveSettings()
77 | {
78 | QSettings settings("Packt", "Hello_OpenCV_Qt", this);
79 | settings.setValue("inputLineEdit", ui->inputLineEdit->text());
80 | settings.setValue("outputLineEdit", ui->outputLineEdit->text());
81 | settings.setValue("medianBlurRadioButton", ui->medianBlurRadioButton->isChecked());
82 | settings.setValue("gaussianBlurRadioButton", ui->gaussianBlurRadioButton->isChecked());
83 | settings.setValue("displayImageCheckBox", ui->displayImageCheckBox->isChecked());
84 | }
85 |
86 | void MainWindow::changeEvent(QEvent *event)
87 | {
88 | if(event->type() == QEvent::LanguageChange)
89 | {
90 | ui->retranslateUi(this);
91 | }
92 | else
93 | {
94 | QMainWindow::changeEvent(event);
95 | }
96 | }
97 |
98 | void MainWindow::on_actionTurkish_triggered()
99 | {
100 | qApp->installTranslator(turkishTranslator);
101 | }
102 |
103 | void MainWindow::on_actionGerman_triggered()
104 | {
105 | qApp->installTranslator(germanTranslator);
106 | }
107 |
108 | void MainWindow::on_actionEnglish_triggered()
109 | {
110 | qApp->removeTranslator(turkishTranslator);
111 | qApp->removeTranslator(germanTranslator);
112 | }
113 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include "opencv2/opencv.hpp"
14 |
15 | namespace Ui {
16 | class MainWindow;
17 | }
18 |
19 | class MainWindow : public QMainWindow
20 | {
21 | Q_OBJECT
22 |
23 | public:
24 | explicit MainWindow(QWidget *parent = 0);
25 | ~MainWindow();
26 |
27 | protected:
28 | void closeEvent(QCloseEvent *event);
29 | void changeEvent(QEvent *event);
30 |
31 | private slots:
32 | void on_inputPushButton_pressed();
33 |
34 | void on_outputPushButton_pressed();
35 |
36 | void on_actionTurkish_triggered();
37 |
38 | void on_actionEnglish_triggered();
39 |
40 | void on_actionGerman_triggered();
41 |
42 | private:
43 | Ui::MainWindow *ui;
44 |
45 | void loadSettings();
46 | void saveSettings();
47 |
48 | QTranslator *turkishTranslator;
49 | QTranslator *germanTranslator;
50 |
51 | };
52 |
53 | #endif // MAINWINDOW_H
54 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 560
10 | 296
11 |
12 |
13 |
14 | Hello_Qt_OpenCV
15 |
16 |
17 | *
18 | {
19 | font: 75 11pt;
20 | background-color: rgb(220, 220, 220);
21 | }
22 | QPushButton, QLineEdit, QGroupBox
23 | {
24 | border: 2px solid rgb(0, 0, 0);
25 | border-radius: 10px;
26 | min-width: 80px;
27 | min-height: 35px;
28 | }
29 | QPushButton
30 | {
31 | background-color: rgb(0, 255, 0);
32 | }
33 | QLineEdit
34 | {
35 | background-color: rgb(0, 170, 255);
36 | }
37 | QPushButton:hover, QRadioButton:hover, QCheckBox:hover
38 | {
39 | color: red;
40 | }
41 | QPushButton:!hover, QRadioButton:!hover, QCheckBox:!hover
42 | {
43 | color: black;
44 | }
45 |
46 |
47 |
48 | -
49 |
50 |
-
51 |
52 |
53 | Input Image :
54 |
55 |
56 |
57 | -
58 |
59 |
60 | -
61 |
62 |
63 | Browse
64 |
65 |
66 |
67 |
68 |
69 | -
70 |
71 |
72 | Filter type
73 |
74 |
75 |
-
76 |
77 |
78 | Median Blur
79 |
80 |
81 | true
82 |
83 |
84 |
85 | -
86 |
87 |
88 | Gaussian Blur
89 |
90 |
91 |
92 |
93 |
94 |
95 | -
96 |
97 |
-
98 |
99 |
100 | Output Image :
101 |
102 |
103 |
104 | -
105 |
106 |
107 | -
108 |
109 |
110 | Browse
111 |
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | Display Image After Saving
120 |
121 |
122 |
123 |
124 |
125 |
144 |
145 |
146 | Turkish
147 |
148 |
149 |
150 |
151 | German
152 |
153 |
154 |
155 |
156 | English
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/resources.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | test.jpg
4 |
5 |
6 | translation_de.qm
7 | translation_tr.qm
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/test.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5/8da7d4414e8347cf9e75018d3f7aa21f619e499f/ch03/Hello_Qt_OpenCV/test.jpg
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/translation_de.qm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5/8da7d4414e8347cf9e75018d3f7aa21f619e499f/ch03/Hello_Qt_OpenCV/translation_de.qm
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/translation_de.ts:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | MainWindow
6 |
7 |
8 | Hello_Qt_OpenCV
9 |
10 |
11 |
12 |
13 | Input Image :
14 | Ein Bild :
15 |
16 |
17 |
18 |
19 | Browse
20 | Öffnen
21 |
22 |
23 |
24 | Filter type
25 | Filter Tip
26 |
27 |
28 |
29 | Median Blur
30 |
31 |
32 |
33 |
34 | Gaussian Blur
35 |
36 |
37 |
38 |
39 | Output Image :
40 | Aus Bild :
41 |
42 |
43 |
44 | Display Image After Saving
45 |
46 |
47 |
48 |
49 | Open Input Image
50 |
51 |
52 |
53 |
54 | Images
55 |
56 |
57 |
58 |
59 | Select Output Image
60 |
61 |
62 |
63 |
64 | Output Image
65 |
66 |
67 |
68 |
69 | Exit
70 |
71 |
72 |
73 |
74 | Are you sure you want to close this program?
75 |
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/translation_tr.qm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5/8da7d4414e8347cf9e75018d3f7aa21f619e499f/ch03/Hello_Qt_OpenCV/translation_tr.qm
--------------------------------------------------------------------------------
/ch03/Hello_Qt_OpenCV/translation_tr.ts:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | MainWindow
6 |
7 |
8 | Hello_Qt_OpenCV
9 |
10 |
11 |
12 |
13 | Input Image :
14 | Resim seç :
15 |
16 |
17 |
18 |
19 | Browse
20 | Ara
21 |
22 |
23 |
24 | Filter type
25 | Filtre türü
26 |
27 |
28 |
29 | Median Blur
30 |
31 |
32 |
33 |
34 | Gaussian Blur
35 |
36 |
37 |
38 |
39 | Output Image :
40 | Resim seç :
41 |
42 |
43 |
44 | Display Image After Saving
45 | Kaydet ve Görüntüle
46 |
47 |
48 |
49 | Open Input Image
50 | Resim Seç
51 |
52 |
53 |
54 | Images
55 | Resimler
56 |
57 |
58 |
59 | Select Output Image
60 | Resim Seç
61 |
62 |
63 |
64 | Output Image
65 | Resim Seç
66 |
67 |
68 |
69 | Exit
70 | Çıkış
71 |
72 |
73 |
74 | Are you sure you want to close this program?
75 | Çıkmak istediğinizden emin misiniz?
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/Plugin_User.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-09-10T13:12:21
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = Plugin_User
12 | TEMPLATE = app
13 |
14 | CONFIG += plugin
15 |
16 | # The following define makes your compiler emit warnings if you use
17 | # any feature of Qt which as been marked as deprecated (the exact warnings
18 | # depend on your compiler). Please consult the documentation of the
19 | # deprecated API in order to know how to port your code away from it.
20 | DEFINES += QT_DEPRECATED_WARNINGS
21 |
22 | # You can also make your code fail to compile if you use deprecated APIs.
23 | # In order to do so, uncomment the following line.
24 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
25 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
26 |
27 |
28 | SOURCES += \
29 | main.cpp \
30 | mainwindow.cpp
31 |
32 | HEADERS += \
33 | mainwindow.h \
34 | cvplugininterface.h
35 |
36 | FORMS += \
37 | mainwindow.ui
38 |
39 | win32: {
40 | include(c:/dev/opencv/opencv.pri)
41 | }
42 |
43 | unix: !macx{
44 | CONFIG += link_pkgconfig
45 | PKGCONFIG += opencv
46 | }
47 |
48 | unix: macx{
49 | INCLUDEPATH += /usr/local/include
50 | LIBS += -L/usr/local/lib \
51 | -lopencv_world
52 | }
53 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/cvplugininterface.h:
--------------------------------------------------------------------------------
1 | #ifndef CVPLUGININTERFACE_H
2 | #define CVPLUGININTERFACE_H
3 |
4 | #include
5 | #include
6 | #include "opencv2/opencv.hpp"
7 |
8 | class CvPluginInterface
9 | {
10 | public:
11 | virtual ~CvPluginInterface() {}
12 | virtual QString description() = 0;
13 | virtual void processImage(const cv::Mat &inputImage, const cv::Mat &outputImage) = 0;
14 | };
15 |
16 | #define CVPLUGININTERFACE_IID "com.amin.cvplugin"
17 | Q_DECLARE_INTERFACE(CvPluginInterface, CVPLUGININTERFACE_IID)
18 |
19 | #endif // CVPLUGININTERFACE_H
20 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | #define FILTERS_SUBFOLDER "/filter_plugins/"
5 |
6 | MainWindow::MainWindow(QWidget *parent) :
7 | QMainWindow(parent),
8 | ui(new Ui::MainWindow)
9 | {
10 | ui->setupUi(this);
11 | getPluginsList();
12 | }
13 |
14 | MainWindow::~MainWindow()
15 | {
16 | delete ui;
17 | }
18 |
19 | void MainWindow::getPluginsList()
20 | {
21 | QDir filtersDir(qApp->applicationDirPath() + FILTERS_SUBFOLDER);
22 | QFileInfoList filters = filtersDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files, QDir::Name);
23 | foreach(QFileInfo filter, filters)
24 | {
25 | if(QLibrary::isLibrary(filter.absoluteFilePath()))
26 | {
27 | QPluginLoader pluginLoader(filter.absoluteFilePath(), this);
28 | if(dynamic_cast(pluginLoader.instance()))
29 | {
30 | ui->filtersList->addItem(filter.fileName());
31 | pluginLoader.unload(); // we can unload for now
32 | }
33 | else
34 | {
35 | QMessageBox::warning(this, tr("Warning"),
36 | QString(tr("Make sure %1 is a correct plugin for this application
"
37 | "and it's not in use by some other application!")).arg(filter.fileName()));
38 | }
39 | }
40 | else
41 | {
42 | QMessageBox::warning(this, tr("Warning"),
43 | QString(tr("Make sure only plugins exist in plugins folder.
"
44 | "%1 is not a plugin.")).arg(filter.fileName()));
45 | }
46 | }
47 |
48 | if(ui->filtersList->count() <= 0)
49 | {
50 | QMessageBox::critical(this, tr("No Plugins"), tr("This application cannot work without plugins!"
51 | "
Make sure that filter_plugins folder exists "
52 | "in the same folder as the application
and that "
53 | "there are some filter plugins inside it"));
54 | this->setEnabled(false);
55 | }
56 | }
57 |
58 | void MainWindow::on_inputImgButton_pressed()
59 | {
60 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open Input Image"), QDir::currentPath(), tr("Images") + " (*.jpg *.png *.bmp)");
61 | if(QFile::exists(fileName))
62 | {
63 | ui->inputImgEdit->setText(fileName);
64 | }
65 | }
66 |
67 | void MainWindow::on_helpButton_pressed()
68 | {
69 | if(ui->filtersList->currentRow() >= 0)
70 | {
71 | QPluginLoader pluginLoader(qApp->applicationDirPath() + FILTERS_SUBFOLDER + ui->filtersList->currentItem()->text());
72 | CvPluginInterface *plugin = dynamic_cast(pluginLoader.instance());
73 | if(plugin)
74 | {
75 | QMessageBox::information(this, tr("Plugin Description"), plugin->description());
76 | }
77 | else
78 | {
79 | QMessageBox::warning(this, tr("Warning"), QString(tr("Make sure plugin %1 exists and is usable.")).arg(ui->filtersList->currentItem()->text()));
80 | }
81 | }
82 | else
83 | {
84 | QMessageBox::warning(this, tr("Warning"), QString(tr("First select a filter plugin from the list.")));
85 | }
86 | }
87 |
88 | void MainWindow::on_filterButton_pressed()
89 | {
90 | if(ui->filtersList->currentRow() >= 0 && !ui->inputImgEdit->text().isEmpty())
91 | {
92 | QPluginLoader pluginLoader(qApp->applicationDirPath() + FILTERS_SUBFOLDER + ui->filtersList->currentItem()->text());
93 | CvPluginInterface *plugin = dynamic_cast(pluginLoader.instance());
94 | if(plugin)
95 | {
96 | if(QFile::exists(ui->inputImgEdit->text()))
97 | {
98 | using namespace cv;
99 | Mat inputImage, outputImage;
100 | inputImage = imread(ui->inputImgEdit->text().toStdString());
101 | plugin->processImage(inputImage, outputImage);
102 | imshow(tr("Filtered Image").toStdString(), outputImage);
103 | }
104 | else
105 | {
106 | QMessageBox::warning(this, tr("Warning"), QString(tr("Make sure %1 exists.")).arg(ui->inputImgEdit->text()));
107 | }
108 | }
109 | else
110 | {
111 | QMessageBox::warning(this, tr("Warning"), QString(tr("Make sure plugin %1 exists and is usable.")).arg(ui->filtersList->currentItem()->text()));
112 | }
113 | }
114 | else
115 | {
116 | QMessageBox::warning(this, tr("Warning"), QString(tr("First select a filter plugin from the list.")));
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include "opencv2/opencv.hpp"
11 | #include "cvplugininterface.h"
12 |
13 | namespace Ui {
14 | class MainWindow;
15 | }
16 |
17 | class MainWindow : public QMainWindow
18 | {
19 | Q_OBJECT
20 |
21 | public:
22 | explicit MainWindow(QWidget *parent = 0);
23 | ~MainWindow();
24 |
25 | private slots:
26 | void on_inputImgButton_pressed();
27 |
28 | void on_helpButton_pressed();
29 |
30 | void on_filterButton_pressed();
31 |
32 | private:
33 | Ui::MainWindow *ui;
34 | void getPluginsList();
35 | };
36 |
37 | #endif // MAINWINDOW_H
38 |
--------------------------------------------------------------------------------
/ch03/Plugin_User/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 522
10 | 360
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 | Qt::Horizontal
22 |
23 |
24 |
25 | -
26 |
27 |
-
28 |
29 |
30 | Input Image
31 |
32 |
33 |
34 | -
35 |
36 |
37 | -
38 |
39 |
40 | Browse
41 |
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
-
49 |
50 |
51 | Filter Plugins
52 |
53 |
54 | Qt::AlignCenter
55 |
56 |
57 |
58 | -
59 |
60 |
61 |
62 |
63 | -
64 |
65 |
-
66 |
67 |
68 | Help
69 |
70 |
71 |
72 | -
73 |
74 |
75 | Qt::Horizontal
76 |
77 |
78 |
79 | 40
80 | 20
81 |
82 |
83 |
84 |
85 | -
86 |
87 |
88 | Filter
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/ch03/cvplugininterface.h:
--------------------------------------------------------------------------------
1 | #ifndef CVPLUGININTERFACE_H
2 | #define CVPLUGININTERFACE_H
3 |
4 | #include
5 | #include
6 | #include "opencv2/opencv.hpp"
7 |
8 | class CvPluginInterface
9 | {
10 | public:
11 | virtual ~CvPluginInterface() {}
12 | virtual QString description() = 0;
13 | virtual void processImage(const cv::Mat &inputImage, const cv::Mat &outputImage) = 0;
14 | };
15 |
16 | #define CVPLUGININTERFACE_IID "com.amin.cvplugin"
17 | Q_DECLARE_INTERFACE(CvPluginInterface, CVPLUGININTERFACE_IID)
18 |
19 | #endif // CVPLUGININTERFACE_H
20 |
--------------------------------------------------------------------------------
/ch03/median_filter_plugin/cvplugininterface.h:
--------------------------------------------------------------------------------
1 | #ifndef CVPLUGININTERFACE_H
2 | #define CVPLUGININTERFACE_H
3 |
4 | #include
5 | #include
6 | #include "opencv2/opencv.hpp"
7 |
8 | class CvPluginInterface
9 | {
10 | public:
11 | virtual ~CvPluginInterface() {}
12 | virtual QString description() = 0;
13 | virtual void processImage(const cv::Mat &inputImage, cv::Mat &outputImage) = 0;
14 | };
15 |
16 | #define CVPLUGININTERFACE_IID "com.amin.cvplugininterface"
17 | Q_DECLARE_INTERFACE(CvPluginInterface, CVPLUGININTERFACE_IID)
18 |
19 | #endif // CVPLUGININTERFACE_H
20 |
--------------------------------------------------------------------------------
/ch03/median_filter_plugin/median_filter_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "median_filter_plugin.h"
2 |
3 |
4 | Median_filter_plugin::Median_filter_plugin()
5 | {
6 | }
7 |
8 | Median_filter_plugin::~Median_filter_plugin()
9 | {
10 |
11 | }
12 |
13 | QString Median_filter_plugin::description()
14 | {
15 | return "This plugin applies median blur filters to any image."
16 | " This plugin's goal is to make us more familiar with the"
17 | " concept of plugins in general.";
18 | }
19 |
20 | void Median_filter_plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
21 | {
22 | cv::medianBlur(inputImage, outputImage, 5);
23 | }
24 |
--------------------------------------------------------------------------------
/ch03/median_filter_plugin/median_filter_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef MEDIAN_FILTER_PLUGIN_H
2 | #define MEDIAN_FILTER_PLUGIN_H
3 |
4 | #include "median_filter_plugin_global.h"
5 |
6 | #include "cvplugininterface.h"
7 |
8 | class MEDIAN_FILTER_PLUGINSHARED_EXPORT Median_filter_plugin: public QObject, public CvPluginInterface
9 | {
10 | Q_OBJECT
11 | Q_PLUGIN_METADATA(IID "com.amin.cvplugininterface")
12 | Q_INTERFACES(CvPluginInterface)
13 | public:
14 | Median_filter_plugin();
15 | ~Median_filter_plugin();
16 | QString description();
17 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
18 | };
19 |
20 | #endif // MEDIAN_FILTER_PLUGIN_H
21 |
--------------------------------------------------------------------------------
/ch03/median_filter_plugin/median_filter_plugin.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-09-10T12:23:47
4 | #
5 | #-------------------------------------------------
6 |
7 | QT -= gui
8 |
9 | TARGET = median_filter_plugin
10 | TEMPLATE = lib
11 |
12 | CONFIG += plugin
13 |
14 | DEFINES += MEDIAN_FILTER_PLUGIN_LIBRARY
15 |
16 | # The following define makes your compiler emit warnings if you use
17 | # any feature of Qt which as been marked as deprecated (the exact warnings
18 | # depend on your compiler). Please consult the documentation of the
19 | # deprecated API in order to know how to port your code away from it.
20 | DEFINES += QT_DEPRECATED_WARNINGS
21 |
22 | # You can also make your code fail to compile if you use deprecated APIs.
23 | # In order to do so, uncomment the following line.
24 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
25 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
26 |
27 | SOURCES += \
28 | median_filter_plugin.cpp
29 |
30 | HEADERS += \
31 | median_filter_plugin.h \
32 | median_filter_plugin_global.h \
33 | cvplugininterface.h
34 |
35 | unix {
36 | target.path = /usr/lib
37 | INSTALLS += target
38 | }
39 |
40 | win32: {
41 | include(c:/dev/opencv/opencv.pri)
42 | }
43 |
44 | unix: !macx{
45 | CONFIG += link_pkgconfig
46 | PKGCONFIG += opencv
47 | }
48 |
49 | unix: macx{
50 | INCLUDEPATH += /usr/local/include
51 | LIBS += -L/usr/local/lib \
52 | -lopencv_world
53 | }
54 |
--------------------------------------------------------------------------------
/ch03/median_filter_plugin/median_filter_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef MEDIAN_FILTER_PLUGIN_GLOBAL_H
2 | #define MEDIAN_FILTER_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(MEDIAN_FILTER_PLUGIN_LIBRARY)
7 | # define MEDIAN_FILTER_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define MEDIAN_FILTER_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // MEDIAN_FILTER_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch04/ImageViewer/ImageViewer.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += core gui
3 |
4 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5 |
6 | TARGET = ImageViewer
7 | TEMPLATE = app
8 |
9 | # The following define makes your compiler emit warnings if you use
10 | # any feature of Qt which as been marked as deprecated (the exact warnings
11 | # depend on your compiler). Please consult the documentation of the
12 | # deprecated API in order to know how to port your code away from it.
13 | DEFINES += QT_DEPRECATED_WARNINGS
14 |
15 | # You can also make your code fail to compile if you use deprecated APIs.
16 | # In order to do so, uncomment the following line.
17 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
18 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
19 |
20 |
21 | SOURCES += \
22 | main.cpp \
23 | mainwindow.cpp
24 |
25 | HEADERS += \
26 | mainwindow.h
27 |
28 | FORMS += \
29 | mainwindow.ui
30 |
--------------------------------------------------------------------------------
/ch04/ImageViewer/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch04/ImageViewer/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | setAcceptDrops(true);
10 | }
11 |
12 | MainWindow::~MainWindow()
13 | {
14 | delete ui;
15 | }
16 |
17 | void MainWindow::dragEnterEvent(QDragEnterEvent *event)
18 | {
19 | QStringList acceptedFileTypes;
20 | acceptedFileTypes.append("jpg");
21 | acceptedFileTypes.append("png");
22 | acceptedFileTypes.append("bmp");
23 |
24 | if (event->mimeData()->hasUrls() &&
25 | event->mimeData()->urls().count() == 1)
26 | {
27 |
28 | QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
29 | if(acceptedFileTypes.contains(file.suffix().toLower()))
30 | {
31 | event->acceptProposedAction();
32 | }
33 | }
34 | }
35 |
36 | void MainWindow::dropEvent(QDropEvent *event)
37 | {
38 | QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
39 | if(pixmap.load(file.absoluteFilePath()))
40 | {
41 | ui->label->setPixmap(pixmap.scaled(ui->label->size(),
42 | Qt::KeepAspectRatio,
43 | Qt::SmoothTransformation));
44 | }
45 | else
46 | {
47 | QMessageBox::critical(this,
48 | tr("Error"),
49 | tr("The image file cannot be read!"));
50 | }
51 | }
52 |
53 | void MainWindow::resizeEvent(QResizeEvent *event)
54 | {
55 | Q_UNUSED(event);
56 | if(!pixmap.isNull())
57 | {
58 | ui->label->setPixmap(pixmap.scaled(ui->label->width()-5,
59 | ui->label->height()-5,
60 | Qt::KeepAspectRatio,
61 | Qt::SmoothTransformation));
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/ch04/ImageViewer/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | namespace Ui {
15 | class MainWindow;
16 | }
17 |
18 | class MainWindow : public QMainWindow
19 | {
20 | Q_OBJECT
21 |
22 | public:
23 | explicit MainWindow(QWidget *parent = 0);
24 | ~MainWindow();
25 |
26 | protected:
27 | void dragEnterEvent(QDragEnterEvent *event);
28 | void dropEvent(QDropEvent *event);
29 | void resizeEvent(QResizeEvent *event);
30 |
31 | private:
32 | Ui::MainWindow *ui;
33 | QPixmap pixmap;
34 | };
35 |
36 | #endif // MAINWINDOW_H
37 |
--------------------------------------------------------------------------------
/ch04/ImageViewer/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 749
10 | 544
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 |
22 | 0
23 | 0
24 |
25 |
26 |
27 |
28 |
29 |
30 | Qt::AlignCenter
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/Painter_Test.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-09-18T17:55:16
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = Painter_Test
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp \
29 | qblinkingwidget.cpp
30 |
31 | HEADERS += \
32 | mainwindow.h \
33 | qblinkingwidget.h
34 |
35 | FORMS += \
36 | mainwindow.ui
37 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | }
10 |
11 | MainWindow::~MainWindow()
12 | {
13 | delete ui;
14 | }
15 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class MainWindow;
8 | }
9 |
10 | class MainWindow : public QMainWindow
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit MainWindow(QWidget *parent = 0);
16 | ~MainWindow();
17 |
18 | private:
19 | Ui::MainWindow *ui;
20 | };
21 |
22 | #endif // MAINWINDOW_H
23 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 505
10 | 411
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 |
19 |
20 | 100
21 | 150
22 | 111
23 | 91
24 |
25 |
26 |
27 |
28 |
38 |
39 |
40 | TopToolBarArea
41 |
42 |
43 | false
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | QBlinkingWidget
52 | QWidget
53 |
54 | 1
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/qblinkingwidget.cpp:
--------------------------------------------------------------------------------
1 | #include "qblinkingwidget.h"
2 |
3 | QBlinkingWidget::QBlinkingWidget(QWidget *parent) : QWidget(parent)
4 | {
5 | blink = false;
6 | connect(&blinkTimer,
7 | SIGNAL(timeout()),
8 | this,
9 | SLOT(onBlink()));
10 | blinkTimer.start(500);
11 | }
12 |
13 | void QBlinkingWidget::paintEvent(QPaintEvent *event)
14 | {
15 | Q_UNUSED(event);
16 | QPainter painter(this);
17 | if(blink)
18 | painter.fillRect(this->rect(),
19 | QBrush(Qt::red));
20 | else
21 | painter.fillRect(this->rect(),
22 | QBrush(Qt::white));
23 | }
24 |
25 | void QBlinkingWidget::onBlink()
26 | {
27 | blink = !blink;
28 | this->update();
29 | }
30 |
--------------------------------------------------------------------------------
/ch04/Painter_Test/qblinkingwidget.h:
--------------------------------------------------------------------------------
1 | #ifndef QBLINKINGWIDGET_H
2 | #define QBLINKINGWIDGET_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | class QBlinkingWidget : public QWidget
10 | {
11 | Q_OBJECT
12 | public:
13 | explicit QBlinkingWidget(QWidget *parent = nullptr);
14 |
15 | protected:
16 | void paintEvent(QPaintEvent *event);
17 |
18 | signals:
19 |
20 | private slots:
21 | void onBlink();
22 |
23 | private:
24 | QTimer blinkTimer;
25 | bool blink;
26 |
27 | };
28 |
29 | #endif // QBLINKINGWIDGET_H
30 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/Graphics_Viewer.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += core gui
3 |
4 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5 |
6 | TARGET = Graphics_Viewer
7 | TEMPLATE = app
8 |
9 | # The following define makes your compiler emit warnings if you use
10 | # any feature of Qt which as been marked as deprecated (the exact warnings
11 | # depend on your compiler). Please consult the documentation of the
12 | # deprecated API in order to know how to port your code away from it.
13 | DEFINES += QT_DEPRECATED_WARNINGS
14 |
15 | # You can also make your code fail to compile if you use deprecated APIs.
16 | # In order to do so, uncomment the following line.
17 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
18 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
19 |
20 |
21 | SOURCES += \
22 | main.cpp \
23 | mainwindow.cpp \
24 | qcustomgraphicseffect.cpp \
25 | qenhancedgraphicsview.cpp
26 |
27 | HEADERS += \
28 | mainwindow.h \
29 | qcustomgraphicseffect.h \
30 | qenhancedgraphicsview.h
31 |
32 | FORMS += \
33 | mainwindow.ui
34 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | this->setAcceptDrops(true);
10 | ui->graphicsView->setAcceptDrops(false);
11 | ui->graphicsView->setScene(&scene);
12 |
13 | ui->graphicsView->setInteractive(true);
14 | ui->graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
15 | ui->graphicsView->setRubberBandSelectionMode(Qt::ContainsItemShape);
16 | }
17 |
18 | MainWindow::~MainWindow()
19 | {
20 | delete ui;
21 | }
22 |
23 | void MainWindow::dragEnterEvent(QDragEnterEvent *event)
24 | {
25 | QStringList acceptedFileTypes;
26 | acceptedFileTypes.append("jpg");
27 | acceptedFileTypes.append("png");
28 | acceptedFileTypes.append("bmp");
29 |
30 | if (event->mimeData()->hasUrls() &&
31 | event->mimeData()->urls().count() == 1)
32 | {
33 |
34 | QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
35 | if(acceptedFileTypes.contains(file.suffix().toLower()))
36 | {
37 | event->acceptProposedAction();
38 | }
39 | }
40 | }
41 |
42 | void MainWindow::dropEvent(QDropEvent *event)
43 | {
44 | QPoint viewPos = ui->graphicsView->mapFromParent(event->pos());
45 | QPointF sceneDropPos = ui->graphicsView->mapToScene(viewPos);
46 |
47 | QFileInfo file(event
48 | ->mimeData()
49 | ->urls()
50 | .at(0)
51 | .toLocalFile());
52 | QPixmap pixmap;
53 | if(pixmap.load(file
54 | .absoluteFilePath()))
55 | {
56 | QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
57 | item->setPos(sceneDropPos);
58 | item->setFlag(QGraphicsItem::ItemIsSelectable);
59 | item->setAcceptedMouseButtons(Qt::LeftButton);
60 | scene.addItem(item);
61 | }
62 | else
63 | {
64 | QMessageBox::critical(this,
65 | tr("Error"),
66 | tr("The image file cannot be read!"));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 |
16 | #include "qcustomgraphicseffect.h"
17 |
18 | namespace Ui {
19 | class MainWindow;
20 | }
21 |
22 | class MainWindow : public QMainWindow
23 | {
24 | Q_OBJECT
25 |
26 | public:
27 | explicit MainWindow(QWidget *parent = 0);
28 | ~MainWindow();
29 |
30 | protected:
31 | void dragEnterEvent(QDragEnterEvent *event);
32 | void dropEvent(QDropEvent *event);
33 |
34 | private:
35 | Ui::MainWindow *ui;
36 | QGraphicsScene scene;
37 |
38 | };
39 |
40 | #endif // MAINWINDOW_H
41 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 677
10 | 531
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | QEnhancedGraphicsView
28 | QGraphicsView
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/qcustomgraphicseffect.cpp:
--------------------------------------------------------------------------------
1 | #include "qcustomgraphicseffect.h"
2 |
3 | QCustomGraphicsEffect::QCustomGraphicsEffect(QObject *parent)
4 | : QGraphicsEffect(parent)
5 | {
6 |
7 | }
8 |
9 | void QCustomGraphicsEffect::draw(QPainter *painter)
10 | {
11 | QImage image;
12 | image = sourcePixmap().toImage();
13 | image = image.convertToFormat(
14 | QImage::Format_Grayscale8);
15 | for(int i=0; idrawPixmap(0,0,QPixmap::fromImage(image));
22 | }
23 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/qcustomgraphicseffect.h:
--------------------------------------------------------------------------------
1 | #ifndef QCUSTOMGRAPHICSEFFECT_H
2 | #define QCUSTOMGRAPHICSEFFECT_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | class QCustomGraphicsEffect : public QGraphicsEffect
9 | {
10 | Q_OBJECT
11 | public:
12 | explicit QCustomGraphicsEffect(QObject *parent = nullptr);
13 |
14 | protected:
15 | void draw(QPainter *painter);
16 |
17 | signals:
18 |
19 | public slots:
20 | };
21 |
22 | #endif // QCUSTOMGRAPHICSEFFECT_H
23 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/qenhancedgraphicsview.cpp:
--------------------------------------------------------------------------------
1 | #include "qenhancedgraphicsview.h"
2 |
3 | QEnhancedGraphicsView::QEnhancedGraphicsView(QWidget *parent)
4 | : QGraphicsView(parent)
5 | {
6 |
7 | }
8 |
9 | void QEnhancedGraphicsView::wheelEvent(QWheelEvent *event)
10 | {
11 | if (event->orientation() == Qt::Vertical)
12 | {
13 | double angleDeltaY = event->angleDelta().y();
14 | double zoomFactor = qPow(1.0015, angleDeltaY);
15 | scale(zoomFactor, zoomFactor);
16 | if(angleDeltaY > 0)
17 | {
18 | this->centerOn(sceneMousePos);
19 | sceneMousePos = this->mapToScene(event->pos());
20 | }
21 | this->viewport()->update();
22 | event->accept();
23 | }
24 | else
25 | {
26 | event->ignore();
27 | }
28 | }
29 |
30 | void QEnhancedGraphicsView::mousePressEvent(QMouseEvent *event)
31 | {
32 | if(event->button() == Qt::RightButton)
33 | {
34 | QMenu menu;
35 | QAction *clearAllAction = menu.addAction("Clear All");
36 | connect(clearAllAction,
37 | SIGNAL(triggered(bool)),
38 | this,
39 | SLOT(clearAll(bool)));
40 |
41 | QAction *clearSelectedAction = menu.addAction("Clear Selected");
42 | connect(clearSelectedAction,
43 | SIGNAL(triggered(bool)),
44 | this,
45 | SLOT(clearSelected(bool)));
46 |
47 | QAction *noEffectAction = menu.addAction("No Effect");
48 | connect(noEffectAction,
49 | SIGNAL(triggered(bool)),
50 | this,
51 | SLOT(noEffect(bool)));
52 |
53 | QAction *blurEffectAction = menu.addAction("Blur Effect");
54 | connect(blurEffectAction,
55 | SIGNAL(triggered(bool)),
56 | this,
57 | SLOT(blurEffect(bool)));
58 |
59 | QAction *dropShadEffectAction = menu.addAction("Drop Shadow Effect");
60 | connect(dropShadEffectAction,
61 | SIGNAL(triggered(bool)),
62 | this,
63 | SLOT(dropShadowEffect(bool)));
64 |
65 | QAction *colorizeEffectAction = menu.addAction("Colorize Effect");
66 | connect(colorizeEffectAction,
67 | SIGNAL(triggered(bool)),
68 | this,
69 | SLOT(colorizeEffect(bool)));
70 |
71 | QAction *customEffectAction = menu.addAction("Custom Effect");
72 | connect(customEffectAction,
73 | SIGNAL(triggered(bool)),
74 | this,
75 | SLOT(customEffect(bool)));
76 |
77 | menu.exec(event->globalPos());
78 | event->accept();
79 | }
80 | else
81 | {
82 | QGraphicsView::mousePressEvent(event);
83 | }
84 | }
85 |
86 | void QEnhancedGraphicsView::clearAll(bool)
87 | {
88 | scene()->clear();
89 | }
90 |
91 | void QEnhancedGraphicsView::clearSelected(bool)
92 | {
93 | while(scene()->selectedItems().count() > 0)
94 | {
95 | delete scene()->selectedItems().at(0);
96 | scene()->selectedItems().removeAt(0);
97 | }
98 | }
99 |
100 | void QEnhancedGraphicsView::noEffect(bool)
101 | {
102 | foreach(QGraphicsItem *item, scene()->selectedItems())
103 | {
104 | item->setGraphicsEffect(Q_NULLPTR);
105 | }
106 | }
107 |
108 | void QEnhancedGraphicsView::blurEffect(bool)
109 | {
110 | foreach(QGraphicsItem *item, scene()->selectedItems())
111 | {
112 | item->setGraphicsEffect(new QGraphicsBlurEffect(this));
113 | }
114 | }
115 |
116 | void QEnhancedGraphicsView::dropShadowEffect(bool)
117 | {
118 | foreach(QGraphicsItem *item, scene()->selectedItems())
119 | {
120 | item->setGraphicsEffect(new QGraphicsDropShadowEffect(this));
121 | }
122 | }
123 |
124 | void QEnhancedGraphicsView::colorizeEffect(bool)
125 | {
126 | foreach(QGraphicsItem *item, scene()->selectedItems())
127 | {
128 | item->setGraphicsEffect(new QGraphicsColorizeEffect(this));
129 | }
130 | }
131 |
132 | void QEnhancedGraphicsView::customEffect(bool)
133 | {
134 | foreach(QGraphicsItem *item, scene()->selectedItems())
135 | {
136 | item->setGraphicsEffect(new QCustomGraphicsEffect(this));
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/ch05/Graphics_Viewer/qenhancedgraphicsview.h:
--------------------------------------------------------------------------------
1 | #ifndef QENHANCEDGRAPHICSVIEW_H
2 | #define QENHANCEDGRAPHICSVIEW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include "qcustomgraphicseffect.h"
15 |
16 | class QEnhancedGraphicsView : public QGraphicsView
17 | {
18 | Q_OBJECT
19 | public:
20 | explicit QEnhancedGraphicsView(QWidget *parent = nullptr);
21 |
22 | protected:
23 | void wheelEvent(QWheelEvent *event);
24 | void mousePressEvent(QMouseEvent *event);
25 |
26 | signals:
27 |
28 | public slots:
29 |
30 | private slots:
31 | void clearAll(bool);
32 | void clearSelected(bool);
33 | void noEffect(bool);
34 | void blurEffect(bool);
35 | void dropShadowEffect(bool);
36 | void colorizeEffect(bool);
37 | void customEffect(bool);
38 |
39 | private:
40 | QPointF sceneMousePos;
41 |
42 | };
43 |
44 | #endif // QENHANCEDGRAPHICSVIEW_H
45 |
--------------------------------------------------------------------------------
/ch05/computer_vision/compute_vision.pro:
--------------------------------------------------------------------------------
1 | TEMPLATE = subdirs
2 | SUBDIRS += \
3 | mainapp \
4 | template_plugin
5 |
--------------------------------------------------------------------------------
/ch05/computer_vision/cvplugininterface/cvplugininterface.h:
--------------------------------------------------------------------------------
1 | #ifndef CVPLUGIN_H
2 | #define CVPLUGIN_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include "opencv2/opencv.hpp"
9 |
10 | class CvPluginInterface
11 | {
12 | public:
13 | virtual ~CvPluginInterface() {}
14 | virtual QString title() = 0;
15 | virtual QString version() = 0;
16 | virtual QString description() = 0;
17 | virtual QString help() = 0;
18 | virtual void setupUi(QWidget *parent) = 0;
19 | virtual void processImage(const cv::Mat &inputImage, cv::Mat &outputImage) = 0;
20 | };
21 |
22 | #define CVPLUGININTERFACE_IID "com.computervision.cvplugininterface"
23 | Q_DECLARE_INTERFACE(CvPluginInterface, CVPLUGININTERFACE_IID)
24 |
25 | #endif // CVPLUGIN_H
26 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/language_tr.qm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5/8da7d4414e8347cf9e75018d3f7aa21f619e499f/ch05/computer_vision/mainapp/language_tr.qm
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/language_tr.ts:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | MainWindow
6 |
7 |
8 | Computer Vision
9 | Computer Vision
10 |
11 |
12 |
13 | Plugin UI
14 | Plugin Arayüzü
15 |
16 |
17 |
18 | View
19 | Görüntü
20 |
21 |
22 |
23 | &File
24 | &Dosya
25 |
26 |
27 |
28 | &Plugins
29 | &Pluginler
30 |
31 |
32 |
33 | O&ptions
34 | &Seçenekler
35 |
36 |
37 |
38 | &Help
39 | &Yardım
40 |
41 |
42 |
43 | &Open Image
44 | &Resim Seç
45 |
46 |
47 |
48 | Open &Video
49 | &Video Seç
50 |
51 |
52 |
53 | Open &Camera
54 | &Kamera
55 |
56 |
57 |
58 | Open &RTSP
59 | &RTSP Seç
60 |
61 |
62 |
63 | &Save Image
64 | Resim Ka&ydet
65 |
66 |
67 |
68 | E&xit
69 | &Çıkış
70 |
71 |
72 |
73 | &About Qt
74 | &Qt Hakkında
75 |
76 |
77 |
78 | Language
79 | Dil
80 |
81 |
82 |
83 | Theme
84 | Stil
85 |
86 |
87 |
88 | Exit
89 | Çıkış
90 |
91 |
92 |
93 | Are you sure you want to exit?
94 | Çıkmak istediğinizden emin misiniz?
95 |
96 |
97 |
98 |
99 | Warning
100 | Dikkat
101 |
102 |
103 |
104 | Make sure %1 is a correct plugin for this application<br>and it's not in use by some other application!
105 | %1 doğru bir plugin değil. Lütfen kontrol ediniz.<br>Başka bir program bu plugin'i kullanıyor olabilir!
106 |
107 |
108 |
109 | Make sure only plugins exist in %1 folder.<br>%2 is not a plugin.
110 | Make sure only plugins exist in plugins folder.<br>%1 is not a plugin.
111 | Sadece pluginlerin %1 klasöründe olduğundan emin olunuz.<br>%2 bir plugin değildir.
112 |
113 |
114 |
115 | No Plugins
116 | Plugin bulunamadı
117 |
118 |
119 |
120 | This application cannot work without plugins!<br>Make sure that %1 folder exists in the same folder as the application<br>and that there are some filter plugins inside it
121 | Bu program pluginler olmadan çalışmaz.<br>%1 klasörünün programla aynı klasörde olduğundan emin olunuz<br>ve içinde en az bir plugin olduğunu kontrol ediniz
122 |
123 |
124 | This application cannot work without plugins!<br>Make sure that filter_plugins folder exists in the same folder as the application<br>and that there are some filter plugins inside it
125 | Bu program pluginler olmadan çalışmaz.<br>
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | a.setStyle("fusion");
8 | MainWindow w;
9 | w.showMaximized();
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/mainapp.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += core gui
3 |
4 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5 |
6 | TARGET = Computer_Vision
7 | TEMPLATE = app
8 |
9 | # The following define makes your compiler emit warnings if you use
10 | # any feature of Qt which as been marked as deprecated (the exact warnings
11 | # depend on your compiler). Please consult the documentation of the
12 | # deprecated API in order to know how to port your code away from it.
13 | DEFINES += QT_DEPRECATED_WARNINGS
14 |
15 | # You can also make your code fail to compile if you use deprecated APIs.
16 | # In order to do so, uncomment the following line.
17 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
18 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
19 |
20 | INCLUDEPATH += ../cvplugininterface
21 |
22 | SOURCES += \
23 | main.cpp \
24 | mainwindow.cpp \
25 | qenhancedgraphicsview.cpp
26 |
27 | HEADERS += \
28 | mainwindow.h \
29 | qenhancedgraphicsview.h
30 |
31 | FORMS += \
32 | mainwindow.ui
33 |
34 | win32: {
35 | include("c:/dev/opencv/opencv.pri")
36 | }
37 |
38 | unix: !macx{
39 | CONFIG += link_pkgconfig
40 | PKGCONFIG += opencv
41 | }
42 |
43 | unix: macx{
44 | INCLUDEPATH += /usr/local/include
45 | LIBS += -L"/usr/local/lib" \
46 | -lopencv_world
47 | }
48 |
49 | # Add more language entries here, following the same naming rule
50 | TRANSLATIONS = language_tr.ts
51 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 |
25 | #include "cvplugininterface.h"
26 |
27 | namespace Ui {
28 | class MainWindow;
29 | }
30 |
31 | class MainWindow : public QMainWindow
32 | {
33 | Q_OBJECT
34 |
35 | public:
36 | explicit MainWindow(QWidget *parent = 0);
37 | ~MainWindow();
38 |
39 | protected:
40 | void closeEvent(QCloseEvent *event);
41 | void changeEvent(QEvent *event);
42 |
43 | private slots:
44 | void onPluginActionTriggered(bool);
45 | void onLanguageActionTriggered(bool);
46 | void onThemeActionTriggered(bool);
47 | void onCurrentPluginUpdateNeeded();
48 | void onCurrentPluginErrorMessage(QString msg);
49 | void onCurrentPluginInfoMessage(QString msg);
50 |
51 | void on_actionAboutQt_triggered();
52 |
53 | void on_actionExit_triggered();
54 |
55 | void on_actionOpenImage_triggered();
56 |
57 | void on_viewOriginalCheck_toggled(bool checked);
58 |
59 | void on_actionSaveImage_triggered();
60 |
61 | void on_action_Camera_triggered();
62 |
63 | private:
64 | Ui::MainWindow *ui;
65 |
66 | void loadSettings();
67 | void saveSettings();
68 |
69 | QString currentThemeFile;
70 | QString currentLanguageFile;
71 | QString currentPluginFile;
72 |
73 | void populatePluginsMenu();
74 | void populateLanguagesMenu();
75 | void populateThemesMenu();
76 |
77 | QPointer currentPlugin;
78 | QPointer currentPluginGui;
79 | QGraphicsScene scene;
80 | QTranslator translator;
81 | QGraphicsPixmapItem originalPixmap, processedPixmap;
82 | cv::Mat originalMat, processedMat;
83 | QImage originalImage, processedImage;
84 |
85 | };
86 |
87 | #endif // MAINWINDOW_H
88 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1024
10 | 768
11 |
12 |
13 |
14 |
15 | 1024
16 | 768
17 |
18 |
19 |
20 | Computer Vision
21 |
22 |
23 |
24 | -
25 |
26 |
-
27 |
28 |
29 | Plugin UI
30 |
31 |
32 |
-
33 |
34 |
35 |
36 |
37 |
38 | -
39 |
40 |
41 | View
42 |
43 |
44 |
-
45 |
46 |
47 | -
48 |
49 |
-
50 |
51 |
52 | View Original Image
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
108 |
109 |
110 | &Open Image
111 |
112 |
113 | Ctrl+O
114 |
115 |
116 |
117 |
118 | &Save Image
119 |
120 |
121 | Ctrl+S
122 |
123 |
124 |
125 |
126 | E&xit
127 |
128 |
129 |
130 |
131 | &About Qt
132 |
133 |
134 |
135 |
136 | Language
137 |
138 |
139 |
140 |
141 | Theme
142 |
143 |
144 |
145 |
146 | &Camera
147 |
148 |
149 |
150 |
151 |
152 |
153 | QEnhancedGraphicsView
154 | QGraphicsView
155 |
156 |
157 |
158 |
159 |
160 |
161 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/qenhancedgraphicsview.cpp:
--------------------------------------------------------------------------------
1 | #include "qenhancedgraphicsview.h"
2 |
3 | QEnhancedGraphicsView::QEnhancedGraphicsView(QWidget *parent)
4 | : QGraphicsView(parent)
5 | {
6 |
7 | }
8 |
9 | void QEnhancedGraphicsView::wheelEvent(QWheelEvent *event)
10 | {
11 | if (event->orientation() == Qt::Vertical)
12 | {
13 | double angleDeltaY = event->angleDelta().y();
14 | double zoomFactor = qPow(1.0015, angleDeltaY);
15 | scale(zoomFactor, zoomFactor);
16 | if(angleDeltaY > 0)
17 | {
18 | this->centerOn(sceneMousePos);
19 | sceneMousePos = this->mapToScene(event->pos());
20 | }
21 | this->viewport()->update();
22 | event->accept();
23 | }
24 | else
25 | {
26 | event->ignore();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/ch05/computer_vision/mainapp/qenhancedgraphicsview.h:
--------------------------------------------------------------------------------
1 | #ifndef QENHANCEDGRAPHICSVIEW_H
2 | #define QENHANCEDGRAPHICSVIEW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | class QEnhancedGraphicsView : public QGraphicsView
16 | {
17 | Q_OBJECT
18 | public:
19 | explicit QEnhancedGraphicsView(QWidget *parent = nullptr);
20 |
21 | protected:
22 | void wheelEvent(QWheelEvent *event);
23 |
24 | signals:
25 |
26 | public slots:
27 |
28 | private:
29 | QPointF sceneMousePos;
30 |
31 | };
32 |
33 | #endif // QENHANCEDGRAPHICSVIEW_H
34 |
--------------------------------------------------------------------------------
/ch05/computer_vision/resources/languages/Turkish - TR.qm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5/8da7d4414e8347cf9e75018d3f7aa21f619e499f/ch05/computer_vision/resources/languages/Turkish - TR.qm
--------------------------------------------------------------------------------
/ch05/computer_vision/resources/readme.txt:
--------------------------------------------------------------------------------
1 | These files and folders need to be copied to the exact folder where application executable resides.
2 | On Windows and Linux, this is simply the same folder as the produced executable.
3 | On macOS though, it is inside the package, but again where the executable resides.
--------------------------------------------------------------------------------
/ch05/computer_vision/resources/themes/Dark.thm:
--------------------------------------------------------------------------------
1 | *
2 | {
3 | font: bold 12pt ;
4 | background-color: #423E3E;
5 | color: white;
6 | }
7 |
8 | QGroupBox, QLineEdit, QSpinBox, QDoubleSpinBox
9 | {
10 | border: 2px solid #FFBD00;
11 | border-radius: 4px;
12 | padding: 2px;
13 | }
14 |
15 | QMenu::item::selected
16 | {
17 | background-color: #A89090;
18 | }
19 |
20 | QGraphicsView
21 | {
22 | border: 1px solid #C0FF9F;
23 | border-radius: 15px;
24 | padding: 10px;
25 | }
26 |
27 | QPushButton
28 | {
29 | border: 3px solid rgba(255, 114, 0, 0.91);
30 | border-radius: 10px;
31 | padding: 2px;
32 | min-height: 30px;
33 | min-width: 30px;
34 | }
35 |
36 | QPushButton::hover
37 | {
38 | background-color: rgba(255, 60, 0, 0.95);
39 | }
40 |
41 | QPushButton::pressed
42 | {
43 | background-color: #FF0004;
44 | }
45 |
46 | QLabel
47 | {
48 | min-height: 30px;
49 | }
50 |
51 | QGroupBox::title
52 | {
53 | font: bold 10pt;
54 | }
55 |
56 | QSpinBox, QDoubleSpinBox, QRadioButton, QCheckBox, QSlider, QProgressBar
57 | {
58 | min-width: 30px;
59 | min-height: 30px;
60 | }
61 |
62 | QProgressBar
63 | {
64 | border: 2px solid rgba(0, 163, 64, 0.91);
65 | border-radius: 10px;
66 | text-align: center;
67 | }
68 |
69 | QProgressBar::chunk
70 | {
71 | background-color: #5636D3;
72 | border: 1px solid rgba(0, 255, 216, 0.91);
73 | border-radius: 7px;
74 | width: 15px;
75 | margin: 2px;
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/ch05/computer_vision/resources/themes/Light.thm:
--------------------------------------------------------------------------------
1 | *
2 | {
3 | font: bold 12pt ;
4 | background-color: #EEEEEE;
5 | color: black;
6 | }
7 |
8 | QGroupBox, QLineEdit, QSpinBox, QDoubleSpinBox
9 | {
10 | border: 2px solid #805E09;
11 | border-radius: 4px;
12 | padding: 2px;
13 | }
14 |
15 | QMenu::item::selected
16 | {
17 | background-color: #DFC8C8;
18 | }
19 |
20 | QGraphicsView
21 | {
22 | border: 1px solid #5D7451;
23 | border-radius: 15px;
24 | padding: 10px;
25 | }
26 |
27 | QPushButton
28 | {
29 | border: 3px solid rgba(255, 114, 0, 0.91);
30 | border-radius: 10px;
31 | padding: 2px;
32 | min-height: 30px;
33 | min-width: 30px;
34 | }
35 |
36 | QPushButton::hover
37 | {
38 | background-color: rgba(255, 60, 0, 0.95);
39 | }
40 |
41 | QPushButton::pressed
42 | {
43 | background-color: #FF0004;
44 | }
45 |
46 | QLabel
47 | {
48 | min-height: 30px;
49 | }
50 |
51 | QGroupBox::title
52 | {
53 | font: bold 10pt;
54 | }
55 |
56 | QSpinBox, QDoubleSpinBox, QRadioButton, QCheckBox, QSlider, QProgressBar
57 | {
58 | min-width: 30px;
59 | min-height: 30px;
60 | }
61 |
62 | QProgressBar
63 | {
64 | border: 2px solid rgba(0, 163, 64, 0.91);
65 | border-radius: 10px;
66 | text-align: center;
67 | }
68 |
69 | QProgressBar::chunk
70 | {
71 | background-color: #5636D3;
72 | border: 1px solid rgba(0, 255, 216, 0.91);
73 | border-radius: 7px;
74 | width: 15px;
75 | margin: 2px;
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/ch05/computer_vision/template_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 319
10 | 530
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 | <html><head/><body><p align="center"><span style=" font-size:12pt; font-weight:600;">This is a template plugin.</span></p><p align="center"><span style=" font-size:12pt; font-weight:600;">It can be cloned and used to create new plugins.</span></p></body></html>
21 |
22 |
23 | true
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/ch05/computer_vision/template_plugin/template_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "template_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | Template_Plugin::Template_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | Template_Plugin::~Template_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString Template_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString Template_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString Template_Plugin::description()
26 | {
27 | return "A Template plugin";
28 | }
29 |
30 | QString Template_Plugin::help()
31 | {
32 | return "This is a Template plugin. Clone and use it to create new plugins.";
33 | }
34 |
35 | void Template_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 |
40 | // Connect signals for GUI elemnts manually here since they won't be connected by name in a plugin
41 | // ...
42 | // emit updateNeeded(); should be added whenever parameters on the plugin GUI change
43 | }
44 |
45 | void Template_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
46 | {
47 | // Replace the following line with the actual image processing task
48 | inputImage.copyTo(outputImage);
49 |
50 | // Otherwise, if the process doesn't affect the output image, update plugin GUI here ...
51 | }
52 |
--------------------------------------------------------------------------------
/ch05/computer_vision/template_plugin/template_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef TEMPLATE_PLUGIN_H
2 | #define TEMPLATE_PLUGIN_H
3 |
4 | #include "template_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class TEMPLATE_PLUGINSHARED_EXPORT Template_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Template_Plugin();
18 | ~Template_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private:
33 | Ui::PluginGui *ui;
34 |
35 | };
36 |
37 | #endif // TEMPLATE_PLUGIN_H
38 |
--------------------------------------------------------------------------------
/ch05/computer_vision/template_plugin/template_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Template_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += TEMPLATE_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | template_plugin.cpp
26 |
27 | HEADERS += \
28 | template_plugin.h \
29 | template_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch05/computer_vision/template_plugin/template_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef TEMPLATE_PLUGIN_GLOBAL_H
2 | #define TEMPLATE_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(TEMPLATE_PLUGIN_LIBRARY)
7 | # define TEMPLATE_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define TEMPLATE_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // TEMPLATE_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/color_plugin/color_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "color_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | Color_Plugin::Color_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | Color_Plugin::~Color_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString Color_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString Color_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString Color_Plugin::description()
26 | {
27 | return "";
28 | }
29 |
30 | QString Color_Plugin::help()
31 | {
32 | return "";
33 | }
34 |
35 | void Color_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 |
40 | ui->colorMapCombo->addItems(QStringList()
41 | << "COLORMAP_AUTUMN"
42 | << "COLORMAP_BONE"
43 | << "COLORMAP_JET"
44 | << "COLORMAP_WINTER"
45 | << "COLORMAP_RAINBOW"
46 | << "COLORMAP_OCEAN"
47 | << "COLORMAP_SUMMER"
48 | << "COLORMAP_SPRING"
49 | << "COLORMAP_COOL"
50 | << "COLORMAP_HSV"
51 | << "COLORMAP_PINK"
52 | << "COLORMAP_HOT"
53 | << "COLORMAP_PARULA");
54 |
55 | connect(ui->colorMapCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_colorMapCombo_currentIndexChanged(int)));
56 | }
57 |
58 | void Color_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
59 | {
60 | using namespace cv;
61 | applyColorMap(inputImage, outputImage, ui->colorMapCombo->currentIndex());
62 | }
63 |
64 | void Color_Plugin::on_colorMapCombo_currentIndexChanged(int index)
65 | {
66 | Q_UNUSED(index);
67 | emit updateNeeded();
68 | }
69 |
--------------------------------------------------------------------------------
/ch06/color_plugin/color_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef COLOR_PLUGIN_H
2 | #define COLOR_PLUGIN_H
3 |
4 | #include "color_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class COLOR_PLUGINSHARED_EXPORT Color_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Color_Plugin();
18 | ~Color_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 | void on_colorMapCombo_currentIndexChanged(int index);
34 |
35 | private:
36 | Ui::PluginGui *ui;
37 |
38 | };
39 |
40 | #endif // COLOR_PLUGIN_H
41 |
--------------------------------------------------------------------------------
/ch06/color_plugin/color_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Color_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += COLOR_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | color_plugin.cpp
26 |
27 | HEADERS += \
28 | color_plugin.h \
29 | color_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch06/color_plugin/color_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef COLOR_PLUGIN_GLOBAL_H
2 | #define COLOR_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(COLOR_PLUGIN_LIBRARY)
7 | # define COLOR_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define COLOR_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // COLOR_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/color_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 319
10 | 530
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 0
22 | 0
23 |
24 |
25 |
26 | Color Maps :
27 |
28 |
29 |
30 | -
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/ch06/copymakeborder_plugin/copymakeborder_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "copymakeborder_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | CopyMakeBorder_Plugin::CopyMakeBorder_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | CopyMakeBorder_Plugin::~CopyMakeBorder_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString CopyMakeBorder_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString CopyMakeBorder_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString CopyMakeBorder_Plugin::description()
26 | {
27 | return "";
28 | }
29 |
30 | QString CopyMakeBorder_Plugin::help()
31 | {
32 | return "";
33 | }
34 |
35 | void CopyMakeBorder_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 | QStringList items;
40 | items.append("BORDER_CONSTANT");
41 | items.append("BORDER_REPLICATE");
42 | items.append("BORDER_REFLECT");
43 | items.append("BORDER_WRAP");
44 | items.append("BORDER_REFLECT_101");
45 | ui->borderTypeComboBox->addItems(items);
46 | connect(ui->borderTypeComboBox,
47 | SIGNAL(currentIndexChanged(int)),
48 | this,
49 | SLOT(on_borderTypeComboBox_currentIndexChanged(int)));
50 | }
51 |
52 | void CopyMakeBorder_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
53 | {
54 | int top, bot, left, right;
55 | top = bot = inputImage.rows/2;
56 | left = right = inputImage.cols/2;
57 | cv::copyMakeBorder(inputImage,
58 | outputImage,
59 | top,
60 | bot,
61 | left,
62 | right,
63 | ui->borderTypeComboBox->currentIndex());
64 | }
65 |
66 | void CopyMakeBorder_Plugin::on_borderTypeComboBox_currentIndexChanged(int index)
67 | {
68 | Q_UNUSED(index);
69 | emit updateNeeded();
70 | }
71 |
--------------------------------------------------------------------------------
/ch06/copymakeborder_plugin/copymakeborder_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef COPYMAKEBORDER_PLUGIN_H
2 | #define COPYMAKEBORDER_PLUGIN_H
3 |
4 | #include "copymakeborder_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class COPYMAKEBORDER_PLUGINSHARED_EXPORT CopyMakeBorder_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | CopyMakeBorder_Plugin();
18 | ~CopyMakeBorder_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 | void on_borderTypeComboBox_currentIndexChanged(int index);
34 |
35 | private:
36 | Ui::PluginGui *ui;
37 |
38 | };
39 |
40 | #endif // COPYMAKEBORDER_PLUGIN_H
41 |
--------------------------------------------------------------------------------
/ch06/copymakeborder_plugin/copymakeborder_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = CopyMakeBorder_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += COPYMAKEBORDER_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | copymakeborder_plugin.cpp
26 | HEADERS += \
27 | copymakeborder_plugin.h \
28 | copymakeborder_plugin_global.h
29 |
30 | unix {
31 | target.path = /usr/lib
32 | INSTALLS += target
33 | }
34 |
35 | win32: {
36 | include("c:/dev/opencv/opencv.pri")
37 | }
38 |
39 | unix: !macx{
40 | CONFIG += link_pkgconfig
41 | PKGCONFIG += opencv
42 | }
43 |
44 | unix: macx{
45 | INCLUDEPATH += "/usr/local/include"
46 | LIBS += -L"/usr/local/lib" \
47 | -lopencv_world
48 | }
49 |
50 | FORMS += \
51 | plugin.ui
52 |
--------------------------------------------------------------------------------
/ch06/copymakeborder_plugin/copymakeborder_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef COPYMAKEBORDER_PLUGIN_GLOBAL_H
2 | #define COPYMAKEBORDER_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(COPYMAKEBORDER_PLUGIN_LIBRARY)
7 | # define COPYMAKEBORDER_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define COPYMAKEBORDER_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // COPYMAKEBORDER_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/copymakeborder_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 320
10 | 207
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 0
22 | 0
23 |
24 |
25 |
26 | Border Type :
27 |
28 |
29 |
30 | -
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/ch06/filter_plugin/filter_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef FILTER_PLUGIN_H
2 | #define FILTER_PLUGIN_H
3 |
4 | #include "filter_plugin_global.h"
5 | #include "cvplugininterface.h"
6 | #include
7 | #include
8 |
9 | namespace Ui {
10 | class PluginGui;
11 | }
12 |
13 | class FILTER_PLUGINSHARED_EXPORT Filter_Plugin: public QObject, public CvPluginInterface
14 | {
15 | Q_OBJECT
16 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
17 | Q_INTERFACES(CvPluginInterface)
18 | public:
19 | Filter_Plugin();
20 | ~Filter_Plugin();
21 |
22 | QString title();
23 | QString version();
24 | QString description();
25 | QString help();
26 | void setupUi(QWidget *parent);
27 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
28 |
29 | signals:
30 | void updateNeeded();
31 | void errorMessage(QString msg);
32 | void infoMessage(QString msg);
33 |
34 | private slots:
35 | void on_bilateralDiaSpin_valueChanged(int arg1);
36 |
37 | void on_bilateralSigmaColorSpin_valueChanged(double arg1);
38 |
39 | void on_bilateralSigmaSpaceSpin_valueChanged(double arg1);
40 |
41 | void on_blurKernelSizeSpinBox_valueChanged(int arg1);
42 |
43 | void on_blurAnchoXSpin_valueChanged(int arg1);
44 |
45 | void on_blurAnchoYSpin_valueChanged(int arg1);
46 |
47 | void on_boxKernelSizeSpinBox_valueChanged(int arg1);
48 |
49 | void on_boxDepthSpin_valueChanged(int arg1);
50 |
51 | void on_boxAnchoXSpin_valueChanged(int arg1);
52 |
53 | void on_boxAnchoYSpin_valueChanged(int arg1);
54 |
55 | void on_boxNormalCheck_toggled(bool checked);
56 |
57 | void on_gaussKernelSizeSpinBox_valueChanged(int arg1);
58 |
59 | void on_gaussSigmaXSpin_valueChanged(double arg1);
60 |
61 | void on_gaussSigmaYSpin_valueChanged(double arg1);
62 |
63 | void on_medianApertureSpin_valueChanged(int arg1);
64 |
65 | void on_mainTabs_currentChanged(int index);
66 |
67 | void on_derivSobelRadio_toggled(bool checked);
68 |
69 | void on_derivScharrRadio_toggled(bool checked);
70 |
71 | void on_derivLaplacRadio_toggled(bool checked);
72 |
73 | void on_derivScaleSpin_valueChanged(double arg1);
74 |
75 | void on_derivDeltaSpin_valueChanged(double arg1);
76 |
77 | void on_morphErodeRadio_toggled(bool checked);
78 |
79 | void on_morphDilateRadio_toggled(bool checked);
80 |
81 | void on_morphMorphRadio_toggled(bool checked);
82 |
83 | void on_morphIterSpin_valueChanged(int arg1);
84 |
85 | void on_morphTypesCombo_currentIndexChanged(int index);
86 |
87 | void on_morphShapesCombo_currentIndexChanged(int index);
88 |
89 | private:
90 | Ui::PluginGui *ui;
91 |
92 | };
93 |
94 | #endif // FILTER_PLUGIN_H
95 |
--------------------------------------------------------------------------------
/ch06/filter_plugin/filter_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Filter_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += FILTER_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | filter_plugin.cpp
26 |
27 | HEADERS += \
28 | filter_plugin.h \
29 | filter_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch06/filter_plugin/filter_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef FILTER_PLUGIN_GLOBAL_H
2 | #define FILTER_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(FILTER_PLUGIN_LIBRARY)
7 | # define FILTER_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define FILTER_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // FILTER_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/fourier_plugin/fourier_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "fourier_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | Fourier_Plugin::Fourier_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | Fourier_Plugin::~Fourier_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString Fourier_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString Fourier_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString Fourier_Plugin::description()
26 | {
27 | return "A Template plugin";
28 | }
29 |
30 | QString Fourier_Plugin::help()
31 | {
32 | return "This is a Template plugin. Clone and use it to create new plugins.";
33 | }
34 |
35 | void Fourier_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 | }
40 |
41 | void Fourier_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
42 | {
43 | using namespace cv;
44 |
45 | Mat grayImg;
46 |
47 | cvtColor(inputImage, grayImg, CV_BGR2GRAY);
48 |
49 | int optH = getOptimalDFTSize( grayImg.rows );
50 | int optW = getOptimalDFTSize( grayImg.cols );
51 |
52 | Mat padded;
53 | copyMakeBorder(grayImg,
54 | padded,
55 | 0,
56 | optH - grayImg.rows,
57 | 0,
58 | optW - grayImg.cols,
59 | BORDER_CONSTANT,
60 | Scalar::all(0));
61 |
62 | Mat channels[] = {Mat_(padded),
63 | Mat::zeros(padded.size(),
64 | CV_32F)};
65 | Mat complex;
66 | merge(channels, 2, complex);
67 |
68 | dft(complex, complex);
69 |
70 | split(complex, channels);
71 | Mat mag;
72 | magnitude(channels[0], channels[1], mag);
73 |
74 | mag += Scalar::all(1);
75 | log(mag, mag);
76 |
77 | mag = mag(Rect(0,
78 | 0,
79 | mag.cols & -2,
80 | mag.rows & -2));
81 |
82 | int cx = mag.cols/2;
83 | int cy = mag.rows/2;
84 |
85 | Mat q0(mag, Rect(0, 0, cx, cy));
86 | Mat q1(mag, Rect(cx, 0, cx, cy));
87 | Mat q2(mag, Rect(0, cy, cx, cy));
88 | Mat q3(mag, Rect(cx, cy, cx, cy));
89 |
90 | Mat tmp;
91 | q0.copyTo(tmp);
92 | q3.copyTo(q0);
93 | tmp.copyTo(q3);
94 |
95 | q1.copyTo(tmp);
96 | q2.copyTo(q1);
97 | tmp.copyTo(q2);
98 |
99 | normalize(mag, mag, 0, 255, CV_MINMAX);
100 |
101 | Mat_ magI8bit(mag);
102 | cvtColor(magI8bit, outputImage, CV_GRAY2BGR);
103 | }
104 |
--------------------------------------------------------------------------------
/ch06/fourier_plugin/fourier_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef FOURIER_PLUGIN_H
2 | #define FOURIER_PLUGIN_H
3 |
4 | #include "fourier_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class FOURIER_PLUGINSHARED_EXPORT Fourier_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Fourier_Plugin();
18 | ~Fourier_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 |
34 | private:
35 | Ui::PluginGui *ui;
36 |
37 | };
38 |
39 | #endif // FOURIER_PLUGIN_H
40 |
--------------------------------------------------------------------------------
/ch06/fourier_plugin/fourier_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Fourier_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += FOURIER_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | fourier_plugin.cpp
26 |
27 | HEADERS += \
28 | fourier_plugin.h \
29 | fourier_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch06/fourier_plugin/fourier_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef FOURIER_PLUGIN_GLOBAL_H
2 | #define FOURIER_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(FOURIER_PLUGIN_LIBRARY)
7 | # define FOURIER_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define FOURIER_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // FOURIER_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/fourier_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 319
10 | 530
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch06/segmentation_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 319
10 | 530
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 0
22 | 0
23 |
24 |
25 |
26 | Thresh :
27 |
28 |
29 |
30 | -
31 |
32 |
33 | 255
34 |
35 |
36 | Qt::Horizontal
37 |
38 |
39 |
40 | -
41 |
42 |
43 | -
44 |
45 |
46 | Adaptive
47 |
48 |
49 |
50 | -
51 |
52 |
53 | 255
54 |
55 |
56 | Qt::Horizontal
57 |
58 |
59 |
60 | -
61 |
62 |
63 | Max :
64 |
65 |
66 |
67 | -
68 |
69 |
70 |
71 | 0
72 | 0
73 |
74 |
75 |
76 | Threshold Type :
77 |
78 |
79 |
80 | -
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/ch06/segmentation_plugin/segmentation_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "segmentation_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | Segmentation_Plugin::Segmentation_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | Segmentation_Plugin::~Segmentation_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString Segmentation_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString Segmentation_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString Segmentation_Plugin::description()
26 | {
27 | return "";
28 | }
29 |
30 | QString Segmentation_Plugin::help()
31 | {
32 | return "";
33 | }
34 |
35 | void Segmentation_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 |
40 | ui->threshAdaptiveCombo->addItems(
41 | QStringList()
42 | << "ADAPTIVE_THRESH_MEAN_C"
43 | << "ADAPTIVE_THRESH_GAUSSIAN_C");
44 |
45 | ui->threshTypeCombo->addItems(
46 | QStringList()
47 | << "THRESH_BINARY"
48 | << "THRESH_BINARY_INV"
49 | << "THRESH_TRUNC"
50 | << "THRESH_TOZERO"
51 | << "THRESH_TOZERO_INV");
52 | connect(ui->threshAdaptiveCheck, SIGNAL(toggled(bool)), this, SLOT(on_threshAdaptiveCheck_toggled(bool)));
53 | connect(ui->threshAdaptiveCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_threshAdaptiveCombo_currentIndexChanged(int)));
54 | connect(ui->threshTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_threshTypeCombo_currentIndexChanged(int)));
55 | connect(ui->threshSlider, SIGNAL(valueChanged(int)), this, SLOT(on_threshSlider_valueChanged(int)));
56 | connect(ui->threshMaxSlider, SIGNAL(valueChanged(int)), this, SLOT(on_threshMaxSlider_valueChanged(int)));
57 |
58 | }
59 |
60 | void Segmentation_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
61 | {
62 | using namespace cv;
63 |
64 | Mat grayScale;
65 | cvtColor(inputImage, grayScale, CV_BGR2GRAY);
66 |
67 | if(ui->threshAdaptiveCheck->isChecked())
68 | {
69 | adaptiveThreshold(grayScale,
70 | grayScale,
71 | ui->threshMaxSlider->value(),
72 | ui->threshAdaptiveCombo->currentIndex(),
73 | ui->threshTypeCombo->currentIndex(),
74 | 7,
75 | 0);
76 | }
77 | else
78 | {
79 | threshold(grayScale,
80 | grayScale,
81 | ui->threshSlider->value(),
82 | ui->threshMaxSlider->value(),
83 | ui->threshTypeCombo->currentIndex());
84 | }
85 | cvtColor(grayScale, outputImage, CV_GRAY2BGR);
86 | }
87 |
88 | void Segmentation_Plugin::on_threshAdaptiveCheck_toggled(bool checked)
89 | {
90 | Q_UNUSED(checked);
91 | emit updateNeeded();
92 | }
93 |
94 | void Segmentation_Plugin::on_threshAdaptiveCombo_currentIndexChanged(int index)
95 | {
96 | Q_UNUSED(index);
97 | emit updateNeeded();
98 | }
99 |
100 | void Segmentation_Plugin::on_threshTypeCombo_currentIndexChanged(int index)
101 | {
102 | Q_UNUSED(index);
103 | emit updateNeeded();
104 | }
105 |
106 | void Segmentation_Plugin::on_threshSlider_valueChanged(int value)
107 | {
108 | emit infoMessage(QString::number(value));
109 | emit updateNeeded();
110 | }
111 |
112 | void Segmentation_Plugin::on_threshMaxSlider_valueChanged(int value)
113 | {
114 | emit infoMessage(QString::number(value));
115 | emit updateNeeded();
116 | }
117 |
--------------------------------------------------------------------------------
/ch06/segmentation_plugin/segmentation_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef SEGMENTATION_PLUGIN_H
2 | #define SEGMENTATION_PLUGIN_H
3 |
4 | #include "segmentation_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class SEGMENTATION_PLUGINSHARED_EXPORT Segmentation_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Segmentation_Plugin();
18 | ~Segmentation_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 | void on_threshAdaptiveCheck_toggled(bool checked);
34 |
35 | void on_threshAdaptiveCombo_currentIndexChanged(int index);
36 |
37 | void on_threshTypeCombo_currentIndexChanged(int index);
38 |
39 | void on_threshSlider_valueChanged(int value);
40 |
41 | void on_threshMaxSlider_valueChanged(int value);
42 |
43 | private:
44 | Ui::PluginGui *ui;
45 |
46 | };
47 |
48 | #endif // SEGMENTATION_PLUGIN_H
49 |
--------------------------------------------------------------------------------
/ch06/segmentation_plugin/segmentation_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Segmentation_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += SEGMENTATION_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | segmentation_plugin.cpp
26 |
27 | HEADERS += \
28 | segmentation_plugin.h \
29 | segmentation_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch06/segmentation_plugin/segmentation_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef SEGMENTATION_PLUGIN_GLOBAL_H
2 | #define SEGMENTATION_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(SEGMENTATION_PLUGIN_LIBRARY)
7 | # define SEGMENTATION_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define SEGMENTATION_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // SEGMENTATION_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch06/transform_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 321
10 | 539
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 | Affine
21 |
22 |
23 |
24 | -
25 |
26 |
27 |
28 | 0
29 | 0
30 |
31 |
32 |
33 | Interpolation :
34 |
35 |
36 |
37 | -
38 |
39 |
40 |
41 | 0
42 | 0
43 |
44 |
45 |
46 | Border Type :
47 |
48 |
49 |
50 | -
51 |
52 |
53 | -
54 |
55 |
56 | Resize (1/2 X)
57 |
58 |
59 | true
60 |
61 |
62 |
63 | -
64 |
65 |
66 | Resize (2 X)
67 |
68 |
69 |
70 | -
71 |
72 |
73 | Remap
74 |
75 |
76 |
77 | -
78 |
79 |
80 | -
81 |
82 |
83 | Perspective
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/ch06/transform_plugin/transform_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "transform_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | Transform_Plugin::Transform_Plugin()
6 | {
7 | // Insert initialization codes here ...
8 | }
9 |
10 | Transform_Plugin::~Transform_Plugin()
11 | {
12 | // Insert cleanup codes here ...
13 | }
14 |
15 | QString Transform_Plugin::title()
16 | {
17 | return this->metaObject()->className();
18 | }
19 |
20 | QString Transform_Plugin::version()
21 | {
22 | return "1.0.0";
23 | }
24 |
25 | QString Transform_Plugin::description()
26 | {
27 | return "Performs different transformations available in OpenCV";
28 | }
29 |
30 | QString Transform_Plugin::help()
31 | {
32 | return "This is a plugin that performs different filters available in OpenCV";
33 | }
34 |
35 | void Transform_Plugin::setupUi(QWidget *parent)
36 | {
37 | ui = new Ui::PluginGui;
38 | ui->setupUi(parent);
39 |
40 | ui->borderTypeCombo->addItems(
41 | QStringList()
42 | << "BORDER_CONSTANT"
43 | << "BORDER_REPLICATE"
44 | << "BORDER_REFLECT"
45 | << "BORDER_WRAP"
46 | << "BORDER_REFLECT_101");
47 |
48 | ui->interpolationCombo->addItems(
49 | QStringList()
50 | << "INTER_NEAREST"
51 | << "INTER_CUBIC"
52 | << "INTER_AREA"
53 | << "INTER_LANCZOS4");
54 |
55 | connect(ui->resizeHalfRadio, SIGNAL(toggled(bool)), this, SLOT(on_resizeHalfRadio_toggled(bool)));
56 | connect(ui->resizeDoubleRadio, SIGNAL(toggled(bool)), this, SLOT(on_resizeDoubleRadio_toggled(bool)));
57 | connect(ui->remapRadio, SIGNAL(toggled(bool)), this, SLOT(on_remapRadio_toggled(bool)));
58 | connect(ui->affineRadio, SIGNAL(toggled(bool)), this, SLOT(on_affineRadio_toggled(bool)));
59 | connect(ui->perspectiveRadio, SIGNAL(toggled(bool)), this, SLOT(on_perspectiveRadio_toggled(bool)));
60 | connect(ui->borderTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_borderTypeCombo_currentIndexChanged(int)));
61 | connect(ui->interpolationCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_interpolationCombo_currentIndexChanged(int)));
62 | }
63 |
64 | void Transform_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
65 | {
66 | using namespace cv;
67 | if(ui->resizeHalfRadio->isChecked())
68 | {
69 | resize(inputImage,
70 | outputImage,
71 | Size(),
72 | 0.5,
73 | 0.5,
74 | ui->interpolationCombo->currentIndex());
75 | }
76 | else if(ui->resizeDoubleRadio->isChecked())
77 | {
78 | resize(inputImage,
79 | outputImage,
80 | Size(),
81 | 2.0,
82 | 2.0,
83 | ui->interpolationCombo->currentIndex());
84 | }
85 | else if(ui->affineRadio->isChecked())
86 | {
87 | Point2f triangleA[3];
88 | Point2f triangleB[3];
89 |
90 | triangleA[0] = Point2f(0, 0);
91 | triangleA[1] = Point2f(inputImage.cols - 1, 0);
92 | triangleA[2] = Point2f(0, inputImage.rows - 1);
93 |
94 | triangleB[0] = Point2f(inputImage.cols*0.0, inputImage.rows*0.33);
95 | triangleB[1] = Point2f(inputImage.cols*0.85, inputImage.rows*0.25);
96 | triangleB[2] = Point2f(inputImage.cols*0.15, inputImage.rows*0.7);
97 |
98 | Mat affineMat = getAffineTransform( triangleA, triangleB );
99 |
100 | warpAffine( inputImage,
101 | outputImage,
102 | affineMat,
103 | inputImage.size(),
104 | ui->interpolationCombo->currentIndex(),
105 | ui->borderTypeCombo->currentIndex());
106 | }
107 | else if(ui->perspectiveRadio->isChecked())
108 | {
109 | std::vector cornersA(4);
110 | std::vector cornersB(4);
111 |
112 | cornersA[0] = Point2f(0, 0);
113 | cornersA[1] = Point2f(inputImage.cols, 0);
114 | cornersA[2] = Point2f(inputImage.cols, inputImage.rows);
115 | cornersA[3] = Point2f(0, inputImage.rows);
116 |
117 | cornersB[0] = Point2f(inputImage.cols*0.25, 0);
118 | cornersB[1] = Point2f(inputImage.cols * 0.90, 0);
119 | cornersB[2] = Point2f(inputImage.cols, inputImage.rows);
120 | cornersB[3] = Point2f(0, inputImage.rows * 0.80);
121 |
122 | Mat homo = findHomography(cornersA, cornersB, RANSAC);
123 | warpPerspective(inputImage,
124 | outputImage,
125 | homo,
126 | inputImage.size(),
127 | ui->interpolationCombo->currentIndex(),
128 | ui->borderTypeCombo->currentIndex()); // do perspective transformation
129 | }
130 | else if(ui->remapRadio->isChecked())
131 | {
132 | cvtColor(inputImage, outputImage, CV_32FC(1));;
133 | Mat mapX, mapY;
134 | mapX.create(inputImage.size(), CV_32FC(1));
135 | mapY.create(inputImage.size(), CV_32FC(1));
136 |
137 | Point2f center(inputImage.cols/2,
138 | inputImage.rows/2);
139 |
140 | for(int i=0; i(i,j) = x + center.x;
150 | mapY.at(i,j) = y + center.y;
151 | }
152 |
153 | remap(inputImage,
154 | outputImage,
155 | mapX,
156 | mapY,
157 | INTER_LANCZOS4,
158 | BORDER_CONSTANT);
159 |
160 | }
161 | }
162 |
163 | void Transform_Plugin::on_resizeHalfRadio_toggled(bool checked)
164 | {
165 | Q_UNUSED(checked);
166 | emit updateNeeded();
167 | }
168 |
169 | void Transform_Plugin::on_resizeDoubleRadio_toggled(bool checked)
170 | {
171 | Q_UNUSED(checked);
172 | emit updateNeeded();
173 | }
174 |
175 | void Transform_Plugin::on_remapRadio_toggled(bool checked)
176 | {
177 | Q_UNUSED(checked);
178 | emit updateNeeded();
179 | }
180 |
181 | void Transform_Plugin::on_affineRadio_toggled(bool checked)
182 | {
183 | Q_UNUSED(checked);
184 | emit updateNeeded();
185 | }
186 |
187 | void Transform_Plugin::on_perspectiveRadio_toggled(bool checked)
188 | {
189 | Q_UNUSED(checked);
190 | emit updateNeeded();
191 | }
192 |
193 | void Transform_Plugin::on_borderTypeCombo_currentIndexChanged(int index)
194 | {
195 | Q_UNUSED(index);
196 | emit updateNeeded();
197 | }
198 |
199 | void Transform_Plugin::on_interpolationCombo_currentIndexChanged(int index)
200 | {
201 | Q_UNUSED(index);
202 | emit updateNeeded();
203 | }
204 |
--------------------------------------------------------------------------------
/ch06/transform_plugin/transform_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef TRANSFORM_PLUGIN_H
2 | #define TRANSFORM_PLUGIN_H
3 |
4 | #include "transform_plugin_global.h"
5 | #include "cvplugininterface.h"
6 | #include
7 | #include
8 |
9 | namespace Ui {
10 | class PluginGui;
11 | }
12 |
13 | class TRANSFORM_PLUGINSHARED_EXPORT Transform_Plugin: public QObject, public CvPluginInterface
14 | {
15 | Q_OBJECT
16 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
17 | Q_INTERFACES(CvPluginInterface)
18 | public:
19 | Transform_Plugin();
20 | ~Transform_Plugin();
21 |
22 | QString title();
23 | QString version();
24 | QString description();
25 | QString help();
26 | void setupUi(QWidget *parent);
27 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
28 |
29 | signals:
30 | void updateNeeded();
31 | void errorMessage(QString msg);
32 | void infoMessage(QString msg);
33 |
34 | private slots:
35 |
36 |
37 | void on_resizeHalfRadio_toggled(bool checked);
38 |
39 | void on_resizeDoubleRadio_toggled(bool checked);
40 |
41 | void on_remapRadio_toggled(bool checked);
42 |
43 | void on_affineRadio_toggled(bool checked);
44 |
45 | void on_perspectiveRadio_toggled(bool checked);
46 |
47 | void on_borderTypeCombo_currentIndexChanged(int index);
48 |
49 | void on_interpolationCombo_currentIndexChanged(int index);
50 |
51 | private:
52 | Ui::PluginGui *ui;
53 |
54 | };
55 |
56 | #endif // TRANSFORM_PLUGIN_H
57 |
--------------------------------------------------------------------------------
/ch06/transform_plugin/transform_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Transform_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += TRANSFORM_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | transform_plugin.cpp
26 |
27 | HEADERS += \
28 | transform_plugin.h \
29 | transform_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch06/transform_plugin/transform_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef TRANSFORM_PLUGIN_GLOBAL_H
2 | #define TRANSFORM_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(TRANSFORM_PLUGIN_LIBRARY)
7 | # define TRANSFORM_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define TRANSFORM_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // TRANSFORM_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch07/keypoint_plugin/keypoint_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef KEYPOINT_PLUGIN_H
2 | #define KEYPOINT_PLUGIN_H
3 |
4 | #include "keypoint_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class KEYPOINT_PLUGINSHARED_EXPORT Keypoint_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Keypoint_Plugin();
18 | ~Keypoint_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 | void on_toolBox_currentChanged(int index);
34 |
35 | void on_agastThreshSpin_valueChanged(int arg1);
36 |
37 | void on_agastNonmaxCheck_toggled(bool checked);
38 |
39 | void on_agastTypeCombo_currentIndexChanged(int index);
40 |
41 | void on_kazeAcceleratedCheck_toggled(bool checked);
42 |
43 | void on_kazeExtendCheck_toggled(bool checked);
44 |
45 | void on_kazeUprightCheck_toggled(bool checked);
46 |
47 | void on_akazeDescriptCombo_currentIndexChanged(int index);
48 |
49 | void on_kazeThreshSpin_valueChanged(double arg1);
50 |
51 | void on_kazeOctaveSpin_valueChanged(int arg1);
52 |
53 | void on_kazeLayerSpin_valueChanged(int arg1);
54 |
55 | void on_kazeDiffCombo_currentIndexChanged(int index);
56 |
57 | void on_briskThreshSpin_valueChanged(int arg1);
58 |
59 | void on_briskOctaveSpin_valueChanged(int arg1);
60 |
61 | void on_briskScaleSpin_valueChanged(double arg1);
62 |
63 | void on_fastThreshSpin_valueChanged(int arg1);
64 |
65 | void on_fastNonmaxCheck_toggled(bool checked);
66 |
67 | void on_fastTypeCombo_currentIndexChanged(int index);
68 |
69 | void on_harrisCheck_toggled(bool checked);
70 |
71 | void on_harrisKSpin_valueChanged(double arg1);
72 |
73 | void on_gfttBlockSpin_valueChanged(int arg1);
74 |
75 | void on_gfttMaxSpin_valueChanged(int arg1);
76 |
77 | void on_gfttDistSpin_valueChanged(double arg1);
78 |
79 | void on_gfttQualitySpin_valueChanged(double arg1);
80 |
81 | void on_orbFeaturesSpin_valueChanged(int arg1);
82 |
83 | void on_orbScaleSpin_valueChanged(double arg1);
84 |
85 | void on_orbLevelsSpin_valueChanged(int arg1);
86 |
87 | void on_orbPatchSpin_valueChanged(int arg1);
88 |
89 | void on_orbWtaSpin_valueChanged(int arg1);
90 |
91 | void on_orbFastCheck_toggled(bool checked);
92 |
93 | void on_orbFastSpin_valueChanged(int arg1);
94 |
95 | void on_browseBtn_pressed();
96 |
97 | void on_keypointCombo_currentIndexChanged(int index);
98 |
99 | void on_descriptorCombo_currentIndexChanged(int index);
100 |
101 | void on_matcherCombo_currentIndexChanged(int index);
102 |
103 | private:
104 | Ui::PluginGui *ui;
105 |
106 | cv::Mat secondImage;
107 |
108 | void fillFeature2D(QString algName, cv::Ptr &algorithm);
109 |
110 | };
111 |
112 | #endif // KEYPOINT_PLUGIN_H
113 |
--------------------------------------------------------------------------------
/ch07/keypoint_plugin/keypoint_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Keypoint_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += KEYPOINT_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | keypoint_plugin.cpp
26 |
27 | HEADERS += \
28 | keypoint_plugin.h \
29 | keypoint_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch07/keypoint_plugin/keypoint_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef KEYPOINT_PLUGIN_GLOBAL_H
2 | #define KEYPOINT_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(KEYPOINT_PLUGIN_LIBRARY)
7 | # define KEYPOINT_PLUGINSHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define KEYPOINT_PLUGINSHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // KEYPOINT_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/MultithreadedCV.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-11-01T16:48:38
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = MultithreadedCV
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp \
29 | videoprocessorthread.cpp \
30 | videoprocessor.cpp
31 |
32 | HEADERS += \
33 | mainwindow.h \
34 | videoprocessorthread.h \
35 | videoprocessor.h
36 |
37 | FORMS += \
38 | mainwindow.ui
39 |
40 | win32: {
41 | include("c:/dev/opencv/opencv.pri")
42 | }
43 |
44 | unix: !macx{
45 | CONFIG += link_pkgconfig
46 | PKGCONFIG += opencv
47 | }
48 |
49 | unix: macx{
50 | INCLUDEPATH += /usr/local/include
51 | LIBS += -L"/usr/local/lib" \
52 | -lopencv_world
53 | }
54 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 |
10 | processor = new VideoProcessor();
11 |
12 | processor->moveToThread(new QThread(this));
13 |
14 | connect(processor->thread(),
15 | SIGNAL(started()),
16 | processor,
17 | SLOT(startVideo()));
18 |
19 | connect(processor->thread(),
20 | SIGNAL(finished()),
21 | processor,
22 | SLOT(deleteLater()));
23 |
24 | connect(processor,
25 | SIGNAL(inDisplay(QPixmap)),
26 | ui->inVideo,
27 | SLOT(setPixmap(QPixmap)));
28 |
29 | connect(processor,
30 | SIGNAL(outDisplay(QPixmap)),
31 | ui->outVideo,
32 | SLOT(setPixmap(QPixmap)));
33 |
34 | processor->thread()->start();
35 | }
36 |
37 | MainWindow::~MainWindow()
38 | {
39 | processor->stopVideo();
40 | processor->thread()->quit();
41 | processor->thread()->wait();
42 |
43 | delete ui;
44 | }
45 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 |
6 | #include "videoprocessor.h"
7 | #include
8 |
9 | namespace Ui {
10 | class MainWindow;
11 | }
12 |
13 | class MainWindow : public QMainWindow
14 | {
15 | Q_OBJECT
16 |
17 | public:
18 | explicit MainWindow(QWidget *parent = 0);
19 | ~MainWindow();
20 |
21 | private:
22 | Ui::MainWindow *ui;
23 |
24 | VideoProcessor *processor;
25 | };
26 |
27 | #endif // MAINWINDOW_H
28 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 763
10 | 453
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 |
22 |
23 |
24 | Qt::AlignCenter
25 |
26 |
27 |
28 | -
29 |
30 |
31 |
32 |
33 |
34 | Qt::AlignCenter
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/videoprocessor.cpp:
--------------------------------------------------------------------------------
1 | #include "videoprocessor.h"
2 |
3 | VideoProcessor::VideoProcessor(QObject *parent) : QObject(parent)
4 | {
5 |
6 | }
7 |
8 | void VideoProcessor::startVideo()
9 | {
10 | using namespace cv;
11 | VideoCapture camera(0);
12 | Mat inFrame, outFrame;
13 | stopped = false;
14 | while(camera.isOpened() && !stopped)
15 | {
16 | camera >> inFrame;
17 | if(inFrame.empty())
18 | continue;
19 |
20 | bitwise_not(inFrame, outFrame);
21 |
22 | emit inDisplay(
23 | QPixmap::fromImage(
24 | QImage(
25 | inFrame.data,
26 | inFrame.cols,
27 | inFrame.rows,
28 | inFrame.step,
29 | QImage::Format_RGB888)
30 | .rgbSwapped()));
31 |
32 | emit outDisplay(
33 | QPixmap::fromImage(
34 | QImage(
35 | outFrame.data,
36 | outFrame.cols,
37 | outFrame.rows,
38 | outFrame.step,
39 | QImage::Format_RGB888)
40 | .rgbSwapped()));
41 | }
42 | }
43 |
44 | void VideoProcessor::stopVideo()
45 | {
46 | qDebug() << Q_FUNC_INFO;
47 | stopped = true;
48 | }
49 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/videoprocessor.h:
--------------------------------------------------------------------------------
1 | #ifndef VIDEOPROCESSOR_H
2 | #define VIDEOPROCESSOR_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include "opencv2/opencv.hpp"
12 |
13 | class VideoProcessor : public QObject
14 | {
15 | Q_OBJECT
16 | public:
17 | explicit VideoProcessor(QObject *parent = nullptr);
18 |
19 | signals:
20 | void inDisplay(QPixmap pixmap);
21 | void outDisplay(QPixmap pixmap);
22 |
23 | public slots:
24 | void startVideo();
25 | void stopVideo();
26 |
27 | private:
28 | bool stopped;
29 |
30 | };
31 |
32 | #endif // VIDEOPROCESSOR_H
33 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/videoprocessorthread.cpp:
--------------------------------------------------------------------------------
1 | #include "videoprocessorthread.h"
2 |
3 | VideoProcessorThread::VideoProcessorThread(QObject *parent) : QThread(parent)
4 | {
5 |
6 | }
7 |
8 | void VideoProcessorThread::run()
9 | {
10 | using namespace cv;
11 | VideoCapture camera(0);
12 | Mat inFrame, outFrame;
13 | while(camera.isOpened() && !isInterruptionRequested())
14 | {
15 | camera >> inFrame;
16 | if(inFrame.empty())
17 | continue;
18 |
19 | bitwise_not(inFrame, outFrame);
20 |
21 | emit inDisplay(
22 | QPixmap::fromImage(
23 | QImage(
24 | inFrame.data,
25 | inFrame.cols,
26 | inFrame.rows,
27 | inFrame.step,
28 | QImage::Format_RGB888)
29 | .rgbSwapped()));
30 |
31 | emit outDisplay(
32 | QPixmap::fromImage(
33 | QImage(
34 | outFrame.data,
35 | outFrame.cols,
36 | outFrame.rows,
37 | outFrame.step,
38 | QImage::Format_RGB888)
39 | .rgbSwapped()));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/ch08/MultithreadedCV/videoprocessorthread.h:
--------------------------------------------------------------------------------
1 | #ifndef VIDEOPROCESSORTHREAD_H
2 | #define VIDEOPROCESSORTHREAD_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include "opencv2/opencv.hpp"
8 |
9 | class VideoProcessorThread : public QThread
10 | {
11 | Q_OBJECT
12 | public:
13 | explicit VideoProcessorThread(QObject *parent = nullptr);
14 |
15 | signals:
16 | void inDisplay(QPixmap pixmap);
17 | void outDisplay(QPixmap pixmap);
18 |
19 | public slots:
20 |
21 | private:
22 | void run() override;
23 |
24 | };
25 |
26 | #endif // VIDEOPROCESSORTHREAD_H
27 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/BackgroundDetect.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-11-16T19:56:21
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = BackgroundDetect
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp \
29 | qcvbackgrounddetect.cpp
30 |
31 | HEADERS += \
32 | mainwindow.h \
33 | qcvbackgrounddetect.h
34 |
35 | FORMS += \
36 | mainwindow.ui
37 |
38 | win32: {
39 | include("c:/dev/opencv/opencv.pri")
40 | }
41 |
42 | unix: !macx{
43 | CONFIG += link_pkgconfig
44 | PKGCONFIG += opencv
45 | }
46 |
47 | unix: macx{
48 | INCLUDEPATH += "/usr/local/include"
49 | LIBS += -L"/usr/local/lib" \
50 | -lopencv_world
51 | }
52 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 |
10 | ui->graphicsView->setScene(new QGraphicsScene(this));
11 | backDetect = new QCvBackgroundDetect();
12 | connect(backDetect, SIGNAL(newFrame(QPixmap)), this, SLOT(onNewFrame(QPixmap)));
13 | backDetect->start();
14 |
15 | ui->graphicsView->scene()->addItem(&pixmap);
16 | }
17 |
18 | MainWindow::~MainWindow()
19 | {
20 | backDetect->requestInterruption();
21 | backDetect->wait();
22 | delete backDetect;
23 |
24 | delete ui;
25 | }
26 |
27 | void MainWindow::onNewFrame(QPixmap newFrm)
28 | {
29 | pixmap.setPixmap(newFrm);
30 | }
31 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include "qcvbackgrounddetect.h"
10 |
11 | namespace Ui {
12 | class MainWindow;
13 | }
14 |
15 | class MainWindow : public QMainWindow
16 | {
17 | Q_OBJECT
18 |
19 | public:
20 | explicit MainWindow(QWidget *parent = 0);
21 | ~MainWindow();
22 |
23 | private slots:
24 | void onNewFrame(QPixmap newFrm);
25 |
26 | private:
27 | Ui::MainWindow *ui;
28 |
29 | QCvBackgroundDetect *backDetect;
30 | QGraphicsPixmapItem pixmap;
31 |
32 | };
33 |
34 | #endif // MAINWINDOW_H
35 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 656
10 | 440
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/qcvbackgrounddetect.cpp:
--------------------------------------------------------------------------------
1 | #include "qcvbackgrounddetect.h"
2 |
3 | QCvBackgroundDetect::QCvBackgroundDetect(QObject *parent) : QThread(parent)
4 | {
5 |
6 | }
7 |
8 | void QCvBackgroundDetect::run()
9 | {
10 | using namespace cv;
11 |
12 | Mat foreground;
13 | VideoCapture video;
14 | video.open("d:/back-test.mp4");
15 |
16 | //Ptr subtractor = createBackgroundSubtractorMOG2();
17 | Ptr subtractor = createBackgroundSubtractorKNN();
18 |
19 | while(video.isOpened() && !this->isInterruptionRequested())
20 | {
21 | //this->msleep(100);
22 |
23 | Mat frame;
24 | video >> frame;
25 | if(frame.empty())
26 | break; // or continue if this should be tolerated
27 |
28 | subtractor->apply(frame, foreground);
29 |
30 | Mat foregroundBgr;
31 | //bitwise_and(frame, foreground, foreground);
32 | frame.copyTo(foregroundBgr, foreground);
33 |
34 | emit newFrame(
35 | QPixmap::fromImage(
36 | QImage(
37 | foregroundBgr.data,
38 | foregroundBgr.cols,
39 | foregroundBgr.rows,
40 | foregroundBgr.step,
41 | QImage::Format_RGB888)
42 | .rgbSwapped()));
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/ch09/BackgroundDetect/qcvbackgrounddetect.h:
--------------------------------------------------------------------------------
1 | #ifndef QCVBACKGROUNDDETECT_H
2 | #define QCVBACKGROUNDDETECT_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "opencv2/opencv.hpp"
9 |
10 | class QCvBackgroundDetect : public QThread
11 | {
12 | Q_OBJECT
13 | public:
14 | explicit QCvBackgroundDetect(QObject *parent = nullptr);
15 |
16 | signals:
17 | void newFrame(QPixmap pix);
18 |
19 | public slots:
20 |
21 | private:
22 | void run() override;
23 |
24 | };
25 |
26 | #endif // QCVBACKGROUNDDETECT_H
27 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/MeanShiftTracker.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-11-16T19:56:21
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = MeanShiftTracker
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp \
29 | qcvmeanshiftthread.cpp
30 |
31 | HEADERS += \
32 | mainwindow.h \
33 | qcvmeanshiftthread.h
34 |
35 | FORMS += \
36 | mainwindow.ui
37 |
38 | win32: {
39 | include("c:/dev/opencv/opencv.pri")
40 | }
41 |
42 | unix: !macx{
43 | CONFIG += link_pkgconfig
44 | PKGCONFIG += opencv
45 | }
46 |
47 | unix: macx{
48 | INCLUDEPATH += "/usr/local/include"
49 | LIBS += -L"/usr/local/lib" \
50 | -lopencv_world
51 | }
52 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent) :
5 | QMainWindow(parent),
6 | ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 |
10 | ui->graphicsView->setScene(new QGraphicsScene(this));
11 | ui->graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
12 | connect(ui->graphicsView, SIGNAL(rubberBandChanged(QRect,QPointF,QPointF)),
13 | this, SLOT(onRubberBandChanged(QRect,QPointF,QPointF)));
14 |
15 | meanshift = new QCvMeanShiftThread();
16 | connect(meanshift, SIGNAL(newFrame(QPixmap)), this, SLOT(onNewFrame(QPixmap)));
17 | meanshift->start();
18 |
19 | ui->graphicsView->scene()->addItem(&pixmap);
20 | }
21 |
22 | MainWindow::~MainWindow()
23 | {
24 | meanshift->requestInterruption();
25 | meanshift->wait();
26 | delete meanshift;
27 |
28 | delete ui;
29 | }
30 |
31 | void MainWindow::onRubberBandChanged(QRect rect, QPointF, QPointF)
32 | {
33 | meanshift->setTrackRect(rect);
34 | }
35 |
36 | void MainWindow::onNewFrame(QPixmap newFrm)
37 | {
38 | pixmap.setPixmap(newFrm);
39 | }
40 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include "qcvmeanshiftthread.h"
10 |
11 | namespace Ui {
12 | class MainWindow;
13 | }
14 |
15 | class MainWindow : public QMainWindow
16 | {
17 | Q_OBJECT
18 |
19 | public:
20 | explicit MainWindow(QWidget *parent = 0);
21 | ~MainWindow();
22 |
23 | private slots:
24 | void onRubberBandChanged(QRect rect, QPointF, QPointF);
25 | void onNewFrame(QPixmap newFrm);
26 |
27 | private:
28 | Ui::MainWindow *ui;
29 |
30 | QCvMeanShiftThread *meanshift;
31 | QGraphicsPixmapItem pixmap;
32 |
33 | };
34 |
35 | #endif // MAINWINDOW_H
36 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 656
10 | 440
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 | -
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/qcvmeanshiftthread.cpp:
--------------------------------------------------------------------------------
1 | #include "qcvmeanshiftthread.h"
2 |
3 | QCvMeanShiftThread::QCvMeanShiftThread(QObject *parent) : QThread(parent)
4 | {
5 |
6 | }
7 |
8 | void QCvMeanShiftThread::run()
9 | {
10 | using namespace cv;
11 |
12 | VideoCapture video;
13 | Mat hist;
14 | video.open(0);
15 |
16 | while(video.isOpened() && !this->isInterruptionRequested())
17 | {
18 | //this->msleep(100);
19 |
20 | Mat frame;
21 | video >> frame;
22 | if(frame.empty())
23 | break; // or continue if this should be tolerated
24 |
25 | if(trackRect.size().area() > 0)
26 | {
27 | QMutexLocker locker(&rectMutex);
28 |
29 | Mat hsv, hue;
30 | cvtColor(frame, hsv, CV_BGR2HSV);
31 | hue.create(hsv.size(), hsv.depth());
32 | float hrange[] = {0, 179};
33 | const float* ranges[] = {hrange};
34 | int bins[] = {24};
35 | int fromto[] = {0, 0};
36 | mixChannels(&hsv, 1, &hue, 1, fromto, 1);
37 |
38 | if(updateHistogram)
39 | {
40 | Mat roi(hue, trackRect);
41 | calcHist(&roi, 1, 0, Mat(), hist, 1, bins, ranges);
42 |
43 | normalize(hist,
44 | hist,
45 | 0,
46 | 255,
47 | NORM_MINMAX);
48 |
49 | updateHistogram = false;
50 | }
51 |
52 | Mat backProj;
53 | calcBackProject(&hue,
54 | 1,
55 | 0,
56 | hist,
57 | backProj,
58 | ranges);
59 |
60 | TermCriteria criteria;
61 | criteria.maxCount = 5;
62 | criteria.epsilon = 3;
63 | criteria.type = TermCriteria::EPS;
64 | RotatedRect rotRec = CamShift(backProj, trackRect, criteria);
65 |
66 | rectangle(frame, trackRect, Scalar(0,0,255), 2);
67 | ellipse(frame, rotRec, Scalar(0,255,0), 2);
68 |
69 | // cvtColor(backProj, backProj, CV_GRAY2BGR);
70 | // frame = backProj;
71 | // rectangle(frame, trackRect, Scalar(0,0,255), 2);
72 |
73 | }
74 |
75 | emit newFrame(
76 | QPixmap::fromImage(
77 | QImage(
78 | frame.data,
79 | frame.cols,
80 | frame.rows,
81 | frame.step,
82 | QImage::Format_RGB888)
83 | .rgbSwapped()));
84 |
85 | }
86 | }
87 |
88 | void QCvMeanShiftThread::setTrackRect(QRect rect)
89 | {
90 | QMutexLocker locker(&rectMutex);
91 | if((rect.width()>2) && (rect.height()>2))
92 | {
93 | trackRect.x = rect.left();
94 | trackRect.y = rect.top();
95 | trackRect.width = rect.width();
96 | trackRect.height = rect.height();
97 | updateHistogram = true;
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/ch09/MeanShiftTracker/qcvmeanshiftthread.h:
--------------------------------------------------------------------------------
1 | #ifndef QCVMEANSHIFTTHREAD_H
2 | #define QCVMEANSHIFTTHREAD_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "opencv2/opencv.hpp"
9 |
10 | class QCvMeanShiftThread : public QThread
11 | {
12 | Q_OBJECT
13 | public:
14 | explicit QCvMeanShiftThread(QObject *parent = nullptr);
15 |
16 | void setTrackRect(QRect rect);
17 |
18 | signals:
19 | void newFrame(QPixmap pix);
20 |
21 | public slots:
22 |
23 | private:
24 | void run() override;
25 | cv::Rect trackRect;
26 | QMutex rectMutex;
27 | bool updateHistogram;
28 |
29 | };
30 |
31 | #endif // QCVMEANSHIFTTHREAD_H
32 |
--------------------------------------------------------------------------------
/ch09/histogram_plugin/histogram_plugin.cpp:
--------------------------------------------------------------------------------
1 | #include "histogram_plugin.h"
2 |
3 | #include "ui_plugin.h"
4 |
5 | #include
6 |
7 | Histogram_Plugin::Histogram_Plugin()
8 | {
9 | // Insert initialization codes here ...
10 | }
11 |
12 | Histogram_Plugin::~Histogram_Plugin()
13 | {
14 | // Insert cleanup codes here ...
15 | }
16 |
17 | QString Histogram_Plugin::title()
18 | {
19 | return this->metaObject()->className();
20 | }
21 |
22 | QString Histogram_Plugin::version()
23 | {
24 | return "1.0.0";
25 | }
26 |
27 | QString Histogram_Plugin::description()
28 | {
29 | return "Histogram plugin";
30 | }
31 |
32 | QString Histogram_Plugin::help()
33 | {
34 | return "A plugin for playing around with histograms.";
35 | }
36 |
37 | void Histogram_Plugin::setupUi(QWidget *parent)
38 | {
39 | ui = new Ui::PluginGui;
40 | ui->setupUi(parent);
41 |
42 | connect(ui->grayRadio, SIGNAL(toggled(bool)), this, SLOT(on_grayRadio_toggled(bool)));
43 | connect(ui->rgbRadio, SIGNAL(toggled(bool)), this, SLOT(on_rgbRadio_toggled(bool)));
44 | connect(ui->hsvRadio, SIGNAL(toggled(bool)), this, SLOT(on_hsvRadio_toggled(bool)));
45 | connect(ui->binsSpin, SIGNAL(valueChanged(int)), this, SLOT(on_binsSpin_valueChanged(int)));
46 | connect(ui->widthSpin, SIGNAL(valueChanged(int)), this, SLOT(on_widthSpin_valueChanged(int)));
47 | connect(ui->heightSpin, SIGNAL(valueChanged(int)), this, SLOT(on_heightSpin_valueChanged(int)));
48 | connect(ui->uniformCheck, SIGNAL(toggled(bool)), this, SLOT(on_uniformCheck_toggled(bool)));
49 | }
50 |
51 | void Histogram_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
52 | {
53 | if(ui->grayRadio->isChecked())
54 | {
55 | grayScaleHist(inputImage, outputImage);
56 | }
57 | else if(ui->rgbRadio->isChecked())
58 | {
59 | rgbHist(inputImage, outputImage);
60 | }
61 | else if(ui->hsvRadio->isChecked())
62 | {
63 | hsvHist(inputImage, outputImage);
64 | }
65 | }
66 |
67 | void Histogram_Plugin::on_grayRadio_toggled(bool)
68 | {
69 | emit updateNeeded();
70 | }
71 |
72 | void Histogram_Plugin::on_rgbRadio_toggled(bool)
73 | {
74 | emit updateNeeded();
75 | }
76 |
77 | void Histogram_Plugin::on_hsvRadio_toggled(bool)
78 | {
79 | emit updateNeeded();
80 | }
81 |
82 | void Histogram_Plugin::on_binsSpin_valueChanged(int)
83 | {
84 | emit updateNeeded();
85 | }
86 |
87 | void Histogram_Plugin::grayScaleHist(const cv::Mat &inputImage, cv::Mat &outputImage)
88 | {
89 | using namespace cv;
90 | Mat grayImg;
91 | cvtColor(inputImage, grayImg, CV_BGR2GRAY);
92 |
93 |
94 | int channels[] = {0}; // only the first channel
95 | int histSize[] = {ui->binsSpin->value()}; // number of bins
96 |
97 | float rangeGray[] = {0,255}; // range of grayscale
98 | const float* ranges[] = { rangeGray };
99 |
100 | Mat histogram;
101 |
102 | calcHist(&grayImg,
103 | 1, // number of images
104 | channels,
105 | Mat(), // no masks, an empty Mat
106 | histogram,
107 | 1, // dimensionality
108 | histSize,
109 | ranges);
110 |
111 | /*
112 | for(int i=0; i(i,0) = 255;
116 | else
117 | histogram.at(i,0) = 0;
118 | }
119 |
120 | Mat backprojection;
121 | calcBackProject(&grayImg,
122 | 1,
123 | channels,
124 | histogram,
125 | backprojection,
126 | ranges);
127 | */
128 |
129 | double maxVal = 0;
130 | minMaxLoc(histogram,
131 | Q_NULLPTR, // don't need min
132 | &maxVal,
133 | Q_NULLPTR, // don't need index min
134 | Q_NULLPTR // don't need index max
135 | );
136 |
137 | outputImage.create(ui->heightSpin->value(), // any image width
138 | ui->widthSpin->value(), // any image height
139 | CV_8UC(3));
140 |
141 | outputImage = Scalar::all(128); // empty grayish image
142 |
143 | //imshow("grayImg", grayImg);
144 |
145 | Point p1(0,0), p2(0,0);
146 | for(int i=0; ibinsSpin->value(); i++)
147 | {
148 | float value = histogram.at(i,0);
149 | value = maxVal - value; // invert
150 | value = value / maxVal * outputImage.rows;
151 | line(outputImage,
152 | p1,
153 | Point(p1.x,value),
154 | Scalar(0,0,0));
155 | p1.y = p2.y = value;
156 | p2.x = float(i+1) * float(outputImage.cols) / float(ui->binsSpin->value());
157 | line(outputImage,
158 | p1, p2,
159 | Scalar(0,0,0));
160 | p1.x = p2.x;
161 | }
162 | }
163 |
164 | void Histogram_Plugin::rgbHist(const cv::Mat &inputImage, cv::Mat &outputImage)
165 | {
166 | using namespace cv;
167 | using namespace std;
168 |
169 | int channels[] = {0};
170 | int histSize[] = {ui->binsSpin->value()}; // number of bins
171 |
172 | float range[] = {0,255}; // range of colors
173 | const float* ranges[] = { range };
174 |
175 | Mat histograms[3];
176 |
177 | vector planes;
178 | split(inputImage, planes);
179 |
180 | double maxVal[3] = {0,0,0};
181 |
182 | for(int i=0; i<3; i++)
183 | {
184 | calcHist(&planes[i],
185 | 1, // number of images
186 | channels,
187 | Mat(), // no masks, an empty Mat
188 | histograms[i],
189 | 1, // dimensionality
190 | histSize,
191 | ranges);
192 |
193 | minMaxLoc(histograms[i],
194 | Q_NULLPTR, // don't need min
195 | &maxVal[i],
196 | Q_NULLPTR, // don't need index min
197 | Q_NULLPTR // don't need index max
198 | );
199 | }
200 |
201 | outputImage.create(ui->heightSpin->value(), // any image width
202 | ui->widthSpin->value(), // any image height
203 | CV_8UC(3));
204 |
205 | outputImage = Scalar::all(0); // empty black image
206 |
207 | Point p1[3], p2[3];
208 | for(int i=0; ibinsSpin->value(); i++)
209 | {
210 | for(int j=0; j<3; j++)
211 | {
212 | float value = histograms[j].at(i,0);
213 | value = maxVal[j] - value; // invert
214 | value = value / maxVal[j] * outputImage.rows;
215 | line(outputImage,
216 | p1[j],
217 | Point(p1[j].x,value),
218 | Scalar(j==0 ? 255:0,
219 | j==1 ? 255:0,
220 | j==2 ? 255:0),
221 | 2);
222 | p1[j].y = p2[j].y = value;
223 | p2[j].x = float(i+1) * float(outputImage.cols) / float(ui->binsSpin->value());
224 | line(outputImage,
225 | p1[j], p2[j],
226 | Scalar(j==0 ? 255:0,
227 | j==1 ? 255:0,
228 | j==2 ? 255:0),
229 | 2);
230 | p1[j].x = p2[j].x;
231 | }
232 | }
233 | }
234 |
235 | void Histogram_Plugin::hsvHist(const cv::Mat &inputImage, cv::Mat &outputImage)
236 | {
237 | using namespace cv;
238 | Mat hsvImg;
239 | cvtColor(inputImage, hsvImg, CV_BGR2HSV);
240 |
241 | int channels[] = {0}; // only the first channel
242 | int histSize[] = {ui->binsSpin->value()}; // number of bins
243 |
244 | float rangeHue[] = {0,179}; // range of Hue channel
245 | const float* ranges[] = { rangeHue };
246 |
247 | Mat histogram;
248 |
249 | calcHist(&hsvImg,
250 | 1, // number of images
251 | channels,
252 | Mat(), // no masks, an empty Mat
253 | histogram,
254 | 1, // dimensionality
255 | histSize,
256 | ranges);
257 |
258 | double maxVal = 0;
259 | minMaxLoc(histogram,
260 | Q_NULLPTR, // don't need min
261 | &maxVal,
262 | Q_NULLPTR, // don't need index min
263 | Q_NULLPTR // don't need index max
264 | );
265 |
266 | outputImage.create(ui->heightSpin->value(), // any image width
267 | ui->widthSpin->value(), // any image height
268 | CV_8UC(3));
269 |
270 | outputImage = Scalar::all(0); // empty black image
271 |
272 | Mat colors(1, ui->binsSpin->value(), CV_8UC3);
273 | for(int i=0; ibinsSpin->value(); i++)
274 | {
275 | colors.at(i) = Vec3b(saturate_cast((i+1)*180.0/ui->binsSpin->value()), 255, 255);
276 | }
277 | cvtColor(colors, colors, COLOR_HSV2BGR);
278 |
279 | Point p1(0,0), p2(0,outputImage.rows-1);
280 | for(int i=0; ibinsSpin->value(); i++)
281 | {
282 | float value = histogram.at(i,0);
283 | value = maxVal - value; // invert
284 | value = value / maxVal * outputImage.rows; // scale
285 | p1.y = value;
286 | p2.x = float(i+1) * float(outputImage.cols) / float(ui->binsSpin->value());
287 | rectangle(outputImage,
288 | p1,
289 | p2,
290 | Scalar(colors.at(i)),
291 | CV_FILLED);
292 | p1.x = p2.x;
293 | }
294 |
295 | }
296 |
297 | void Histogram_Plugin::on_widthSpin_valueChanged(int)
298 | {
299 | emit updateNeeded();
300 | }
301 |
302 | void Histogram_Plugin::on_heightSpin_valueChanged(int)
303 | {
304 | emit updateNeeded();
305 | }
306 |
307 | void Histogram_Plugin::on_uniformCheck_toggled(bool)
308 | {
309 | emit updateNeeded();
310 | }
311 |
--------------------------------------------------------------------------------
/ch09/histogram_plugin/histogram_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef HISTOGRAM_PLUGIN_H
2 | #define HISTOGRAM_PLUGIN_H
3 |
4 | #include "histogram_plugin_global.h"
5 | #include "cvplugininterface.h"
6 |
7 | namespace Ui {
8 | class PluginGui;
9 | }
10 |
11 | class HISTOGRAM_PLUGIN_SHARED_EXPORT Histogram_Plugin: public QObject, public CvPluginInterface
12 | {
13 | Q_OBJECT
14 | Q_PLUGIN_METADATA(IID "com.computervision.cvplugininterface")
15 | Q_INTERFACES(CvPluginInterface)
16 | public:
17 | Histogram_Plugin();
18 | ~Histogram_Plugin();
19 |
20 | QString title();
21 | QString version();
22 | QString description();
23 | QString help();
24 | void setupUi(QWidget *parent);
25 | void processImage(const cv::Mat &inputImage, cv::Mat &outputImage);
26 |
27 | signals:
28 | void updateNeeded();
29 | void errorMessage(QString msg);
30 | void infoMessage(QString msg);
31 |
32 | private slots:
33 | void on_grayRadio_toggled(bool);
34 |
35 | void on_rgbRadio_toggled(bool checked);
36 |
37 | void on_hsvRadio_toggled(bool checked);
38 |
39 | void on_binsSpin_valueChanged(int arg1);
40 |
41 | void on_widthSpin_valueChanged(int arg1);
42 |
43 | void on_heightSpin_valueChanged(int arg1);
44 |
45 | void on_uniformCheck_toggled(bool checked);
46 |
47 | private:
48 | Ui::PluginGui *ui;
49 |
50 | void grayScaleHist(const cv::Mat &inputImage, cv::Mat &outputImage);
51 | void rgbHist(const cv::Mat &inputImage, cv::Mat &outputImage);
52 | void hsvHist(const cv::Mat &inputImage, cv::Mat &outputImage);
53 |
54 | };
55 |
56 | #endif // HISTOGRAM_PLUGIN_H
57 |
--------------------------------------------------------------------------------
/ch09/histogram_plugin/histogram_plugin.pro:
--------------------------------------------------------------------------------
1 |
2 | QT += widgets
3 |
4 | TARGET = Histogram_Plugin
5 | TEMPLATE = lib
6 |
7 | CONFIG += plugin
8 |
9 | DEFINES += HISTOGRAM_PLUGIN_LIBRARY
10 |
11 | # The following define makes your compiler emit warnings if you use
12 | # any feature of Qt which as been marked as deprecated (the exact warnings
13 | # depend on your compiler). Please consult the documentation of the
14 | # deprecated API in order to know how to port your code away from it.
15 | DEFINES += QT_DEPRECATED_WARNINGS
16 |
17 | # You can also make your code fail to compile if you use deprecated APIs.
18 | # In order to do so, uncomment the following line.
19 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
20 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
21 |
22 | INCLUDEPATH += ../cvplugininterface
23 |
24 | SOURCES += \
25 | histogram_plugin.cpp
26 |
27 | HEADERS += \
28 | histogram_plugin.h \
29 | histogram_plugin_global.h
30 |
31 | unix {
32 | target.path = /usr/lib
33 | INSTALLS += target
34 | }
35 |
36 | win32: {
37 | include("c:/dev/opencv/opencv.pri")
38 | }
39 |
40 | unix: !macx{
41 | CONFIG += link_pkgconfig
42 | PKGCONFIG += opencv
43 | }
44 |
45 | unix: macx{
46 | INCLUDEPATH += "/usr/local/include"
47 | LIBS += -L"/usr/local/lib" \
48 | -lopencv_world
49 | }
50 |
51 | FORMS += \
52 | plugin.ui
53 |
--------------------------------------------------------------------------------
/ch09/histogram_plugin/histogram_plugin_global.h:
--------------------------------------------------------------------------------
1 | #ifndef HISTOGRAM_PLUGIN_GLOBAL_H
2 | #define HISTOGRAM_PLUGIN_GLOBAL_H
3 |
4 | #include
5 |
6 | #if defined(HISTOGRAM_PLUGIN_LIBRARY)
7 | # define HISTOGRAM_PLUGIN_SHARED_EXPORT Q_DECL_EXPORT
8 | #else
9 | # define HISTOGRAM_PLUGIN_SHARED_EXPORT Q_DECL_IMPORT
10 | #endif
11 |
12 | #endif // HISTOGRAM_PLUGIN_GLOBAL_H
13 |
--------------------------------------------------------------------------------
/ch09/histogram_plugin/plugin.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | PluginGui
4 |
5 |
6 |
7 | 0
8 | 0
9 | 332
10 | 550
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 0
22 | 0
23 |
24 |
25 |
26 | Histogram Type
27 |
28 |
29 |
-
30 |
31 |
32 | Grayscale
33 |
34 |
35 | true
36 |
37 |
38 |
39 | -
40 |
41 |
42 | RGB
43 |
44 |
45 |
46 | -
47 |
48 |
49 | HSV
50 |
51 |
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
-
59 |
60 |
61 |
62 | 0
63 | 0
64 |
65 |
66 |
67 | Bins:
68 |
69 |
70 |
71 | -
72 |
73 |
74 | 999
75 |
76 |
77 | 255
78 |
79 |
80 |
81 |
82 |
83 | -
84 |
85 |
-
86 |
87 |
88 |
89 | 0
90 | 0
91 |
92 |
93 |
94 | Width :
95 |
96 |
97 |
98 | -
99 |
100 |
101 | 9999
102 |
103 |
104 | 640
105 |
106 |
107 |
108 |
109 |
110 | -
111 |
112 |
113 | Qt::Vertical
114 |
115 |
116 |
117 | 20
118 | 40
119 |
120 |
121 |
122 |
123 | -
124 |
125 |
-
126 |
127 |
128 |
129 | 0
130 | 0
131 |
132 |
133 |
134 | Height :
135 |
136 |
137 |
138 | -
139 |
140 |
141 | 9999
142 |
143 |
144 | 360
145 |
146 |
147 |
148 |
149 |
150 | -
151 |
152 |
153 | Uniform
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/ch10/GuiTest/GuiTest.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-11-22T18:40:01
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += widgets testlib
8 |
9 | TARGET = tst_guitesttest
10 | CONFIG += console
11 | CONFIG -= app_bundle
12 |
13 | TEMPLATE = app
14 |
15 | # The following define makes your compiler emit warnings if you use
16 | # any feature of Qt which as been marked as deprecated (the exact warnings
17 | # depend on your compiler). Please consult the documentation of the
18 | # deprecated API in order to know how to port your code away from it.
19 | DEFINES += QT_DEPRECATED_WARNINGS
20 |
21 | # You can also make your code fail to compile if you use deprecated APIs.
22 | # In order to do so, uncomment the following line.
23 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
24 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
25 |
26 |
27 | SOURCES += \
28 | tst_guitesttest.cpp \
29 | testableform.cpp
30 |
31 | DEFINES += SRCDIR=\\\"$$PWD/\\\"
32 |
33 | FORMS += \
34 | testableform.ui
35 |
36 | HEADERS += \
37 | testableform.h
38 |
--------------------------------------------------------------------------------
/ch10/GuiTest/testableform.cpp:
--------------------------------------------------------------------------------
1 | #include "testableform.h"
2 | #include "ui_testableform.h"
3 |
4 | TestableForm::TestableForm(QWidget *parent) :
5 | QWidget(parent),
6 | ui(new Ui::TestableForm)
7 | {
8 | ui->setupUi(this);
9 |
10 | this->nextBtn = ui->nextBtn;
11 | this->infoLabel = ui->infoLabel;
12 | }
13 |
14 | TestableForm::~TestableForm()
15 | {
16 | delete ui;
17 | }
18 |
19 | void TestableForm::on_nextBtn_pressed()
20 | {
21 | if(ui->infoLabel->text().trimmed().isEmpty())
22 | {
23 | ui->infoLabel->setText("1");
24 | }
25 | else
26 | {
27 | int n = ui->infoLabel->text().toInt();
28 | ui->infoLabel->setText(QString::number(n*2));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ch10/GuiTest/testableform.h:
--------------------------------------------------------------------------------
1 | #ifndef TESTABLEFORM_H
2 | #define TESTABLEFORM_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | namespace Ui {
9 | class TestableForm;
10 | }
11 |
12 | class TestableForm : public QWidget
13 | {
14 | Q_OBJECT
15 |
16 | public:
17 | explicit TestableForm(QWidget *parent = 0);
18 | ~TestableForm();
19 |
20 | QPushButton *nextBtn;
21 | QLabel *infoLabel;
22 |
23 | private slots:
24 | void on_nextBtn_pressed();
25 |
26 | private:
27 | Ui::TestableForm *ui;
28 | };
29 |
30 | #endif // TESTABLEFORM_H
31 |
--------------------------------------------------------------------------------
/ch10/GuiTest/testableform.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | TestableForm
4 |
5 |
6 |
7 | 0
8 | 0
9 | 400
10 | 300
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
-
20 |
21 |
22 | Qt::Horizontal
23 |
24 |
25 |
26 | 40
27 | 20
28 |
29 |
30 |
31 |
32 | -
33 |
34 |
35 | Next
36 |
37 |
38 |
39 | -
40 |
41 |
42 | Qt::Horizontal
43 |
44 |
45 |
46 | 40
47 | 20
48 |
49 |
50 |
51 |
52 |
53 |
54 | -
55 |
56 |
-
57 |
58 |
59 |
60 |
61 |
62 | Qt::AlignCenter
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/ch10/GuiTest/tst_guitesttest.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "testableform.h"
6 |
7 | class GuiTestTest : public QObject
8 | {
9 | Q_OBJECT
10 |
11 | public:
12 | GuiTestTest();
13 |
14 | private Q_SLOTS:
15 | void testGui();
16 | void testGui_data();
17 | };
18 |
19 | GuiTestTest::GuiTestTest()
20 | {
21 | }
22 |
23 | void GuiTestTest::testGui_data()
24 | {
25 | QTest::addColumn("events");
26 | QTest::addColumn("result");
27 |
28 | QTestEventList mouseEvents; // three times
29 | mouseEvents.addMouseClick(Qt::LeftButton);
30 | mouseEvents.addMouseClick(Qt::LeftButton);
31 | mouseEvents.addMouseClick(Qt::LeftButton);
32 | QTest::newRow("mouse") << mouseEvents << "4";
33 |
34 | QTestEventList keybEvents; // four times
35 | keybEvents.addKeyClick(Qt::Key_Space);
36 | keybEvents.addDelay(250);
37 | keybEvents.addKeyClick(Qt::Key_Space);
38 | keybEvents.addDelay(250);
39 | keybEvents.addKeyClick(Qt::Key_Space);
40 | keybEvents.addDelay(250);
41 | keybEvents.addKeyClick(Qt::Key_Space);
42 | QTest::newRow("keyboard") << keybEvents << "8";
43 | }
44 |
45 | void GuiTestTest::testGui()
46 | {
47 | TestableForm t;
48 |
49 | QFETCH(QTestEventList, events);
50 | QFETCH(QString, result);
51 |
52 | events.simulate(t.nextBtn);
53 |
54 | QCOMPARE(t.infoLabel->text(), result);
55 | }
56 |
57 | QTEST_MAIN(GuiTestTest)
58 |
59 | #include "tst_guitesttest.moc"
60 |
--------------------------------------------------------------------------------
/ch10/HelloTest/HelloTest.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-11-21T00:45:19
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += testlib
8 |
9 | QT -= gui
10 |
11 | TARGET = tst_hellotesttest
12 | CONFIG += console
13 | CONFIG -= app_bundle
14 |
15 | TEMPLATE = app
16 |
17 | # The following define makes your compiler emit warnings if you use
18 | # any feature of Qt which as been marked as deprecated (the exact warnings
19 | # depend on your compiler). Please consult the documentation of the
20 | # deprecated API in order to know how to port your code away from it.
21 | DEFINES += QT_DEPRECATED_WARNINGS
22 |
23 | # You can also make your code fail to compile if you use deprecated APIs.
24 | # In order to do so, uncomment the following line.
25 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
26 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
27 |
28 |
29 | SOURCES += \
30 | tst_hellotesttest.cpp \
31 | pixelcounter.cpp
32 |
33 | DEFINES += SRCDIR=\\\"$$PWD/\\\"
34 |
35 | HEADERS += \
36 | pixelcounter.h
37 |
38 | win32: {
39 | include("c:/dev/opencv/opencv.pri")
40 | }
41 |
42 | unix: !macx{
43 | CONFIG += link_pkgconfig
44 | PKGCONFIG += opencv
45 | }
46 |
47 | unix: macx{
48 | INCLUDEPATH += "/usr/local/include"
49 | LIBS += -L"/usr/local/lib" \
50 | -lopencv_world
51 | }
52 |
--------------------------------------------------------------------------------
/ch10/HelloTest/pixelcounter.cpp:
--------------------------------------------------------------------------------
1 | #include "pixelcounter.h"
2 |
3 | PixelCounter::PixelCounter(QObject *parent) : QObject(parent)
4 | {
5 |
6 | }
7 |
8 | int PixelCounter::countPixels(QString fname)
9 | {
10 | cv::Mat image = cv::imread(fname.toStdString());
11 | if(image.empty())
12 | {
13 | return 0;
14 | }
15 | else
16 | {
17 | return image.rows * image.cols;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/ch10/HelloTest/pixelcounter.h:
--------------------------------------------------------------------------------
1 | #ifndef PIXELCOUNTER_H
2 | #define PIXELCOUNTER_H
3 |
4 | #include
5 | #include
6 |
7 | #include "opencv2/opencv.hpp"
8 |
9 | class PixelCounter : public QObject
10 | {
11 | Q_OBJECT
12 | public:
13 | explicit PixelCounter(QObject *parent = nullptr);
14 |
15 | int countPixels(QString fname);
16 |
17 | signals:
18 |
19 | public slots:
20 | };
21 |
22 | #endif // PIXELCOUNTER_H
23 |
--------------------------------------------------------------------------------
/ch10/HelloTest/tst_hellotesttest.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "pixelcounter.h"
5 |
6 | class HelloTestTest : public QObject
7 | {
8 | Q_OBJECT
9 |
10 | public:
11 | HelloTestTest();
12 |
13 | private Q_SLOTS:
14 | void testPixelCount();
15 | void testPixelCount_data();
16 | };
17 |
18 | HelloTestTest::HelloTestTest()
19 | {
20 | }
21 |
22 | void HelloTestTest::testPixelCount_data()
23 | {
24 | QTest::addColumn("filename");
25 | QTest::addColumn("pixelcount");
26 |
27 | QTest::newRow("image") << "c:/dev/test.jpg" << 640*427;
28 | }
29 |
30 | void HelloTestTest::testPixelCount()
31 | {
32 | PixelCounter c;
33 | QFETCH(QString, filename);
34 | QFETCH(int, pixelcount);
35 | QCOMPARE(c.countPixels(filename), pixelcount);
36 |
37 | QBENCHMARK
38 | {
39 | c.countPixels(filename);
40 | }
41 | }
42 |
43 | QTEST_APPLESS_MAIN(HelloTestTest)
44 |
45 | #include "tst_hellotesttest.moc"
46 |
--------------------------------------------------------------------------------
/ch12/CvQml/CvQml.pro:
--------------------------------------------------------------------------------
1 | QT += qml quick
2 |
3 | CONFIG += c++11
4 |
5 | SOURCES += main.cpp \
6 | qimageprocessor.cpp \
7 | qimageviewer.cpp
8 |
9 | RESOURCES += qml.qrc
10 |
11 | # Additional import path used to resolve QML modules in Qt Creator's code model
12 | QML_IMPORT_PATH =
13 |
14 | # Additional import path used to resolve QML modules just for Qt Quick Designer
15 | QML_DESIGNER_IMPORT_PATH =
16 |
17 | # The following define makes your compiler emit warnings if you use
18 | # any feature of Qt which as been marked deprecated (the exact warnings
19 | # depend on your compiler). Please consult the documentation of the
20 | # deprecated API in order to know how to port your code away from it.
21 | DEFINES += QT_DEPRECATED_WARNINGS
22 |
23 | # You can also make your code fail to compile if you use deprecated APIs.
24 | # In order to do so, uncomment the following line.
25 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
26 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
27 |
28 | # Default rules for deployment.
29 | qnx: target.path = /tmp/$${TARGET}/bin
30 | else: unix:!android: target.path = /opt/$${TARGET}/bin
31 | !isEmpty(target.path): INSTALLS += target
32 |
33 | win32: {
34 | INCLUDEPATH += "C:/dev/opencv/build/install/include"
35 | Debug: {
36 | LIBS += -L"C:/dev/opencv/build/install/x86/vc14/lib" \
37 | -lopencv_world330d
38 | }
39 | Release: {
40 | LIBS += -L"C:/dev/opencv/build/install/x86/vc14/lib" \
41 | -lopencv_world330
42 | }
43 | }
44 |
45 | HEADERS += \
46 | qimageprocessor.h \
47 | qimageviewer.h
48 |
--------------------------------------------------------------------------------
/ch12/CvQml/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "qimageprocessor.h"
5 | #include "qimageviewer.h"
6 |
7 | int main(int argc, char *argv[])
8 | {
9 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
10 | QGuiApplication app(argc, argv);
11 |
12 | qmlRegisterType("com.amin.classes", 1, 0, "ImageProcessor");
13 | qmlRegisterType("com.amin.classes", 1, 0, "ImageViewer");
14 |
15 | QQmlApplicationEngine engine;
16 | engine.load(QUrl(QLatin1String("qrc:/main.qml")));
17 | if (engine.rootObjects().isEmpty())
18 | return -1;
19 |
20 | return app.exec();
21 | }
22 |
--------------------------------------------------------------------------------
/ch12/CvQml/main.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.7
2 | import QtQuick.Controls 2.0
3 | import QtQuick.Layouts 1.3
4 | import QtMultimedia 5.8
5 | import com.amin.classes 1.0
6 |
7 | ApplicationWindow
8 | {
9 | visible: true
10 | width: 640
11 | height: 480
12 | title: qsTr("CvQml")
13 |
14 | ImageProcessor
15 | {
16 | id: imgProcessor
17 |
18 | onImageProcessed:
19 | {
20 | imgViewer.setImage(image);
21 | imageDrawer.open()
22 | }
23 | }
24 |
25 | Camera
26 | {
27 | id: camera
28 |
29 | imageCapture
30 | {
31 | onImageSaved:
32 | {
33 | imgProcessor.processImage(path)
34 | }
35 | }
36 | }
37 |
38 | VideoOutput
39 | {
40 | source: camera
41 | anchors.fill: parent
42 |
43 | MouseArea
44 | {
45 | anchors.fill: parent
46 | onClicked:
47 | {
48 | camera.imageCapture.capture()
49 | }
50 | }
51 |
52 | Label
53 | {
54 | text: "Touch the screen to take a photo
and process it using OpenCV!"
55 | color: "red"
56 | }
57 | }
58 |
59 | Drawer
60 | {
61 | id: imageDrawer
62 | width: parent.width
63 | height: parent.height
64 |
65 | ImageViewer
66 | {
67 | id: imgViewer
68 | anchors.fill: parent
69 |
70 | Label
71 | {
72 | text: "Swipe from right to left
to return to capture mode!"
73 | color: "red"
74 | }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/ch12/CvQml/qimageprocessor.cpp:
--------------------------------------------------------------------------------
1 | #include "qimageprocessor.h"
2 |
3 | QImageProcessor::QImageProcessor(QObject *parent) : QObject(parent)
4 | {
5 |
6 | }
7 |
8 | void QImageProcessor::processImage(const QString &path)
9 | {
10 | using namespace cv;
11 | Mat imageM = imread(path.toStdString());
12 | if(!imageM.empty())
13 | {
14 | bitwise_not(imageM, imageM); // or any OpenCV code
15 | QImage imageQ(imageM.data,
16 | imageM.cols,
17 | imageM.rows,
18 | imageM.step,
19 | QImage::Format_RGB888);
20 | emit imageProcessed(imageQ.rgbSwapped());
21 | }
22 | else
23 | {
24 | qDebug() << path << "does not exist!";
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/ch12/CvQml/qimageprocessor.h:
--------------------------------------------------------------------------------
1 | #ifndef QIMAGEPROCESSOR_H
2 | #define QIMAGEPROCESSOR_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include "opencv2/opencv.hpp"
8 |
9 | class QImageProcessor : public QObject
10 | {
11 | Q_OBJECT
12 | public:
13 | explicit QImageProcessor(QObject *parent = nullptr);
14 |
15 | Q_INVOKABLE void processImage(const QString &path);
16 |
17 | signals:
18 | void imageProcessed(const QImage &image);
19 |
20 | public slots:
21 |
22 | };
23 |
24 | #endif // QIMAGEPROCESSOR_H
25 |
--------------------------------------------------------------------------------
/ch12/CvQml/qimageviewer.cpp:
--------------------------------------------------------------------------------
1 | #include "qimageviewer.h"
2 |
3 | QImageViewer::QImageViewer(QQuickItem *parent)
4 | : QQuickPaintedItem(parent)
5 | {
6 | }
7 |
8 | void QImageViewer::setImage(const QImage &img)
9 | {
10 | currentImage = img.copy(); // perform a copy
11 | update();
12 | }
13 |
14 | void QImageViewer::paint(QPainter *painter)
15 | {
16 | QSizeF scaled = QSizeF(currentImage.width(),
17 | currentImage.height())
18 | .scaled(boundingRect().size(), Qt::KeepAspectRatio);
19 | QRect centerRect(qAbs(scaled.width() - width()) / 2.0f,
20 | qAbs(scaled.height() - height()) / 2.0f,
21 | scaled.width(),
22 | scaled.height());
23 | painter->drawImage(centerRect,
24 | currentImage);
25 | }
26 |
--------------------------------------------------------------------------------
/ch12/CvQml/qimageviewer.h:
--------------------------------------------------------------------------------
1 | #ifndef QIMAGEVIEWER_H
2 | #define QIMAGEVIEWER_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | class QImageViewer : public QQuickPaintedItem
10 | {
11 | Q_OBJECT
12 | public:
13 | QImageViewer(QQuickItem *parent = Q_NULLPTR);
14 | Q_INVOKABLE void setImage(const QImage &img);
15 |
16 | private:
17 | QImage currentImage;
18 | void paint(QPainter *painter);
19 |
20 | };
21 |
22 | #endif // QIMAGEVIEWER_H
23 |
--------------------------------------------------------------------------------
/ch12/CvQml/qml.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | main.qml
4 | qtquickcontrols2.conf
5 |
6 |
7 |
--------------------------------------------------------------------------------
/ch12/CvQml/qtquickcontrols2.conf:
--------------------------------------------------------------------------------
1 | ; This file can be edited to change the style of the application
2 | ; See Styling Qt Quick Controls 2 in the documentation for details:
3 | ; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
4 |
5 | [Controls]
6 | Style=Default
7 |
8 | [Universal]
9 | Theme=Light
10 | ;Accent=Steel
11 |
12 | [Material]
13 | Theme=Light
14 | ;Accent=BlueGrey
15 | ;Primary=BlueGray
16 |
--------------------------------------------------------------------------------