From aaf97a89331b21d4152fbbf26bcae846db44c3d0 Mon Sep 17 00:00:00 2001 From: Nash Date: Sun, 31 Aug 2014 12:11:34 +0700 Subject: added more advanced opencv-qt integration --- opencv-qt-integration-2/ImageApp.cpp | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 opencv-qt-integration-2/ImageApp.cpp (limited to 'opencv-qt-integration-2/ImageApp.cpp') diff --git a/opencv-qt-integration-2/ImageApp.cpp b/opencv-qt-integration-2/ImageApp.cpp new file mode 100644 index 0000000..5833bd8 --- /dev/null +++ b/opencv-qt-integration-2/ImageApp.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#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!"); + } +} -- cgit v1.2.3