summaryrefslogtreecommitdiff
path: root/opencv-qt-integration-2/ImageApp.cpp
blob: 5833bd8ecdc481982b361cd3d063eeedc983c19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <QtWidgets>
#include "ImageApp.h"

ImageApp::ImageApp()
{
	originalImage = cv::imread("../assets/The_Chapter_House.jpg");
	if (originalImage.data) {
		cv::cvtColor(originalImage, originalImage, cv::COLOR_BGR2RGB);
	}
	setupUi();
	showImage(originalImage);
}

/**
 * Setup the widgets
 */
void ImageApp::setupUi()
{
	imageLabel = new QLabel();

	originalButton = new QPushButton("Original");	
	connect(originalButton, SIGNAL(clicked()), this, SLOT(showOriginalImage()));

	blurButton = new QPushButton("Gaussian Blur");
	connect(blurButton, SIGNAL(clicked()), this, SLOT(doGaussianBlur()));

	cannyButton = new QPushButton("Canny");
	connect(cannyButton, SIGNAL(clicked()), this, SLOT(doCanny()));

	quitButton = new QPushButton("Quit");
	connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

	buttonsLayout = new QVBoxLayout();
	buttonsLayout->addWidget(originalButton);
	buttonsLayout->addWidget(blurButton);
	buttonsLayout->addWidget(cannyButton);
	buttonsLayout->addStretch();
	buttonsLayout->addWidget(quitButton);

	mainLayout = new QHBoxLayout();
	mainLayout->addWidget(imageLabel);

	if (originalImage.data) {
		mainLayout->addLayout(buttonsLayout);
	}
	setLayout(mainLayout);
	setWindowTitle("Image Processing with Qt and OpenCV");
}

/**
 * Redraw original image
 */
void ImageApp::showOriginalImage()
{
	showImage(originalImage);
}

/**
 * Perform Canny edge detection on original image and display the result
 */
void ImageApp::doCanny()
{
	cv::Mat gray;
	cv::cvtColor(originalImage, gray, cv::COLOR_RGB2GRAY);
	cv::Canny(gray, processedImage, 150, 150);
	cv::cvtColor(processedImage, processedImage, cv::COLOR_GRAY2RGB);
	showImage(processedImage);
}

/**
 * Perform Gaussian blurring on original image and display the result
 */
void ImageApp::doGaussianBlur()
{
	cv::GaussianBlur(originalImage, processedImage, cv::Size(15, 15), 0, 0);
	showImage(processedImage);
}

/**
 * Draw OpenCV matrix using QLabel
 */
void ImageApp::showImage(cv::Mat img)
{
	if (img.data) {
		QImage _img(img.data, img.cols, img.rows, QImage::Format_RGB888);
		imageLabel->setPixmap(QPixmap::fromImage(_img));
	} else {
		imageLabel->setText("Cannot load the input image!");
	}
}