Video not playing continuously - qt

I am playing video in Qt using opencv. I am having 6 tiled view cameras from which I am playing video. The problem is if one of the videos is not playing i.e finishes then the GUI freezes and exits. The error I get is you must reimplement QApplication::notify() and catch the exceptions there. How to do this?
The code I am using is as follows.
Somewhere in a function
void MainWindow::ActivateWindow()
{
//Some part of code to set Index for stacked widget
if(stackWidget->currentIndex()==9)
{
const int imagePeriod == 1000/25;
imageTimer->setInterval(imagePeriod);
connect(imageTimer,SIGNAL(timeout()),this,SLOT(demoSlot());
imageTimer->start();
}
}
In slot demoSlot
void MainWindow::demoSlot()
{
captureCamera1 cvCaptureFromFile("/root/mp.mp4");
captureCamera2 cvCaptureFromFile("/root/mp.mp4");
captureCamera3 cvCaptureFromFile("/root/mp.mp4");
while(imageTimer->isActive())
{
frameCamera1 = cvQueryFrame(captureCamera1);
frameCamera2 = cvQueryFrame(captureCamera2);
frameCamera3 = cvQueryFrame(captureCamera2);
sourceImageCam1 = frameCamera1;
sourceImageCam2 = frameCamera2;
sourceImageCam3 = frameCamera3;
cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
cv::cvtColor(sourceImageCam1,sourceImageCam2,CV_BGR2RGB);
cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);
cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);
QImage tempImage1 = QImage((const unsigned char* sourceImageCam1.data,sourceImageCam1.cols,sourceImageCam2.rows,QImage::Format_RG888);
QImage tempImage2 = QImage((const unsigned char* sourceImageCam2.data,sourceImageCam2.cols,sourceImageCam2.rows,QImage::Format_RG888);
QImage tempImage3 = QImage((const unsigned char* sourceImageCam3.data,sourceImageCam3.cols,sourceImageCam3.rows,QImage::Format_RG888);
labelCameraCapture1->setPixmap(QPixmap::fromImage(tempImage1)); //label to display video
labelCameraCapture2->setPixmap(QPixmap::fromImage(tempImage2));
labelCameraCapture3->setPixmap(QPixmap::fromImage(tempImage3));
lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
cvWaitkey(20);
qApp->processEvents();
}
if(imageTimer->isActive())
{
imageTimer->stop();
}
else
{
imageTimer->start();
}
}
In header file
cvCapture *captureCamera1;
cvCapture *captureCamera1;
cvCapture *captureCamera1;
IplImage frameCamera1;
IplImage frameCamera2;
IplImage frameCamera3;
cv::Mat sourceImageCam1;
cv::Mat sourceImageCam2;
cv::Mat sourceImageCam3;

This will do the trick changing that to 3 movies is simple.
class MainWindow : public QMainWindow {
Q_OBJECT
explicit QMainWindow(QWidget *parent) ....
// prepare timer and so on
public slots:
void startVideo() {
vid1.close();
vid1.open("/root/mp.mp4");
imageTimer->start();
}
void demoSlot() {
cv::Mat frame;
vid1 >> frame;
cv::cvtColor(frame,frame,CV_BGR2RGB);
QImage img((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
label1->setPixmap(QPixmap::fromImage(img));
}
private:
...
QTimer *imageTimer;
cv::VideoCapture vid1;
};

Check if frame captured from camera is NULL. Then simply skip processing steps for this camera.
And it'll be better to not mix C++ and C interfaces (I mean cv::Mat and IplImage).

Related

How to Zoom / Fit Image to GraphicsView

I read a lot of posts/threads but I can't get it to work.
I'd like to fit every Image to a GraphicsView regardless if it is smaller or bigger then the view.
What's wrong?
void frmMain::on_btLoadImage_clicked()
{
QGraphicsScene *scene;
QPixmap image;
QString imgPath = "O:/IMG_0001.JPG";
QRectF sceneRect = ui->imgMain->sceneRect();
image.load(imgPath);
image.scaled (sceneRect.width (),sceneRect.height (), Qt::KeepAspectRatio, Qt::SmoothTransformation);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(sceneRect); //image.rect());
//ui->imgMain->fitInView (scene->itemsBoundingRect(), Qt::KeepAspectRatio); //ui->imgMain->width (), ui->imgMain->height ());
ui->imgMain->setScene(scene);
}
Here is a basic custom QGraphicsView implementation which displays one image and keeps it sized/scaled to fit the available viewport space. Note that the image needs to be rescaled every time the viewport size changes, which is why it is simplest to reimplement the QGraphicsView itself and change the scaling in resizeEvent(). Although it could be done inside a custom QGraphicsScene instead. (Or, really, a number of other ways depending on the exact needs.)
The same technique could be used to keep a QGraphicsWidget as the root item in the scene to always take up the full space. Then a layout could be used in the widget to keep children aligned/resized/positioned/etc.
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
class GrpahicsImageView : public QGraphicsView
{
Q_OBJECT
public:
using QGraphicsView::QGraphicsView;
public slots:
void setImage(const QString &imageFile)
{
if (m_imageFile != imageFile) {
m_imageFile = imageFile;
loadImage(viewport()->contentsRect().size());
}
}
void setImageScaleMode(int mode)
{
if (m_scaleMode != Qt::AspectRatioMode(mode)) {
m_scaleMode = Qt::AspectRatioMode(mode);
if (m_item)
loadImage(viewport()->contentsRect().size());
}
}
void loadImage(const QSize &size)
{
if (!scene())
return;
if (m_imageFile.isEmpty()) {
// remove existing image, if any
removeItem();
return;
}
// Load image at original size
QPixmap pm(m_imageFile);
if (pm.isNull()) {
// file not found/other error
removeItem();
return;
}
// Resize the image here.
pm = pm.scaled(size, m_scaleMode, Qt::SmoothTransformation);
if (createItem())
m_item->setPixmap(pm);
}
protected:
void resizeEvent(QResizeEvent *e) override
{
QGraphicsView::resizeEvent(e);
if (!scene())
return;
// Set scene size to fill the available viewport size;
const QRect sceneRect(viewport()->contentsRect());
scene()->setSceneRect(sceneRect);
// Keep the root item sized to fill the viewport and scene;
if (m_item)
loadImage(sceneRect.size());
}
private:
bool createItem() {
if (m_item)
return true;
if (!m_item && scene()) {
m_item = new QGraphicsPixmapItem();
scene()->addItem(m_item);
return true;
}
return false;
}
void removeItem()
{
if (m_item) {
if (scene())
scene()->removeItem(m_item);
delete m_item;
m_item = nullptr;
}
}
Qt::AspectRatioMode m_scaleMode = Qt::KeepAspectRatio;
QString m_imageFile;
QGraphicsPixmapItem *m_item = nullptr;
};
Usage example:
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog d;
d.setLayout(new QVBoxLayout);
d.resize(350, 350);
GrpahicsImageView *view = new GrpahicsImageView(new QGraphicsScene, &d);
QComboBox *imgCb = new QComboBox(&d);
imgCb->addItems({
"./so-logo.png",
"./se-logo.png",
"./su-logo.png"
});
QComboBox *scaleCb = new QComboBox(&d);
scaleCb->addItems({
"IgnoreAspectRatio",
"KeepAspectRatio",
"KeepAspectRatioByExpanding"
});
QHBoxLayout *cbLayout = new QHBoxLayout;
cbLayout->setSpacing(9);
cbLayout->addWidget(imgCb);
cbLayout->addWidget(scaleCb);
d.layout()->addItem(cbLayout);
d.layout()->addWidget(view);
QObject::connect(imgCb, QOverload<const QString &>::of(&QComboBox::currentIndexChanged), view, &GrpahicsImageView::setImage);
QObject::connect(scaleCb, QOverload<int>::of(&QComboBox::currentIndexChanged), view, &GrpahicsImageView::setImageScaleMode);
view->setImageScaleMode(scaleCb->currentIndex());
view->setImage(imgCb->currentText());
return d.exec();
}
https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png
https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/se-logo.png
https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/su-logo.png

How to monitor changes to an arbitrary widget?

I am starting a QT5 application with a rather complex design based on Qt Widgets. It runs on Beagleboard with a touchscreen. I will have a rather weird local invention instead of the LCD display. It's a laser painting on acrylic plate. It has no driver yet. To actually update a screen I must create a screenshot of the window as bitmap, turn it to grayscale and feed to a proprietary library, which will handle the laser. It should look cute, when ready. Unfortunately, the laser blinks on update, so I cannot just make screenshots on timer, or it will be jerky like hell.
I need to run a function every time a meaningful update of GUI happens, while preferably ignore things like button being pressed and released. Is there some way to create a hook without subclassing every single Qt Widget I will use? The only way to do this I know is to override paintEvent of everything. I want a simpler solution.
Possible assumptions are: the application will be running under X server with dummy display, will be the only GUI app running. Some updates happen without user input.
The code below does it. It doesn't dig too deeply into the internals of Qt, it merely leverages the fact that backing store devices are usually QImages. It could be modified to accommodate OpenGL-based backing stores as well.
The WidgetMonitor class is used to monitor the widgets for content changes. An entire top-level window is monitored no matter which particular widget is passed to the monitor(QWidget*) method. You only need to call the monitor method for one widget in the window you intend to monitor - any widget will do. The changes are sent out as a QImage of window contents.
The implementation installs itself as an event filter in the target window widget and all of its children, and monitors the repaint events. It attempts to coalesce the repaint notifications by using the zero-length timer. The additions and removals of children are tracked automagically.
When you run the example, it creates two windows: a source window, and a destination window. They may be overlapped so you need to separate them. As you resize the source window, the size of the destination's rendition of it will also change appropriately. Any changes to the source children (time label, button state) propagate automatically to the destination.
In your application, the destination could be an object that takes the QImage contents, converts them to grayscale, resizes appropriately, and passes them to your device.
I do not quite understand how your laser device works if it can't gracefully handle updates. I presume that it is a raster-scanning laser that runs continuously in a loop that looks roughly like this:
while (1) {
for (line = 0; line < nLines; ++line) {
drawLine();
}
}
You need to modify this loop so that it works as follows:
newImage = true;
QImage localImage;
while (1) {
if (newImage) localImage = newImage;
for (line = 0; line < localImage.height(); ++line) {
drawLine(line, localImage);
}
}
You'd be flipping the newImage flag from the notification slot connected to the WidgetMonitor. You may well find out that leveraging QImage, and Qt's functionality in general, in your device driver code, will make it much easier to develop. Qt provides portable timers, threads, collections, etc. I presume that your "driver" is completely userspace, and communicates via a serial port or ethernet to the micro controller that actually controls the laser device.
If you will be writing a kernel driver for the laser device, then the interface would be probably very similar, except that you end up writing the image bitmap to an open device handle.
// https://github.com/KubaO/stackoverflown/tree/master/questions/surface-20737882
#include <QtWidgets>
#include <array>
const char kFiltered[] = "WidgetMonitor_filtered";
class WidgetMonitor : public QObject {
Q_OBJECT
QVector<QPointer<QWidget>> m_awake;
QBasicTimer m_timer;
int m_counter = 0;
void queue(QWidget *window) {
Q_ASSERT(window && window->isWindow());
if (!m_awake.contains(window)) m_awake << window;
if (!m_timer.isActive()) m_timer.start(0, this);
}
void filter(QObject *obj) {
if (obj->isWidgetType() && !obj->property(kFiltered).toBool()) {
obj->installEventFilter(this);
obj->setProperty(kFiltered, true);
}
}
void unfilter(QObject *obj) {
if (obj->isWidgetType() && obj->property(kFiltered).toBool()) {
obj->removeEventFilter(this);
obj->setProperty(kFiltered, false);
}
}
bool eventFilter(QObject *obj, QEvent *ev) override {
switch (ev->type()) {
case QEvent::Paint: {
if (!obj->isWidgetType()) break;
if (auto *window = static_cast<QWidget *>(obj)->window()) queue(window);
break;
}
case QEvent::ChildAdded: {
auto *cev = static_cast<QChildEvent *>(ev);
if (auto *child = qobject_cast<QWidget *>(cev->child())) monitor(child);
break;
}
default:
break;
}
return false;
}
void timerEvent(QTimerEvent *ev) override {
if (ev->timerId() != m_timer.timerId()) return;
qDebug() << "painting: " << m_counter++ << m_awake;
for (auto w : m_awake)
if (auto *img = dynamic_cast<QImage *>(w->backingStore()->paintDevice()))
emit newContents(*img, w);
m_awake.clear();
m_timer.stop();
}
public:
explicit WidgetMonitor(QObject *parent = nullptr) : QObject{parent} {}
explicit WidgetMonitor(QWidget *w, QObject *parent = nullptr) : QObject{parent} {
monitor(w);
}
Q_SLOT void monitor(QWidget *w) {
w = w->window();
if (!w) return;
filter(w);
for (auto *obj : w->findChildren<QWidget *>()) filter(obj);
queue(w);
}
Q_SLOT void unMonitor(QWidget *w) {
w = w->window();
if (!w) return;
unfilter(w);
for (auto *obj : w->findChildren<QWidget *>()) unfilter(obj);
m_awake.removeAll(w);
}
Q_SIGNAL void newContents(const QImage &, QWidget *w);
};
class TestWidget : public QWidget {
QVBoxLayout m_layout{this};
QLabel m_time;
QBasicTimer m_timer;
void timerEvent(QTimerEvent *ev) override {
if (ev->timerId() != m_timer.timerId()) return;
m_time.setText(QTime::currentTime().toString());
}
public:
explicit TestWidget(QWidget *parent = nullptr) : QWidget{parent} {
m_layout.addWidget(&m_time);
m_layout.addWidget(new QLabel{"Static Label"});
m_layout.addWidget(new QPushButton{"A Button"});
m_timer.start(1000, this);
}
};
int main(int argc, char **argv) {
QApplication app{argc, argv};
TestWidget src;
QLabel dst;
dst.setFrameShape(QFrame::Box);
for (auto *w : std::array<QWidget *, 2>{&dst, &src}) {
w->show();
w->raise();
}
QMetaObject::invokeMethod(&dst, [&] { dst.move(src.frameGeometry().topRight()); },
Qt::QueuedConnection);
WidgetMonitor mon(&src);
src.setWindowTitle("Source");
dst.setWindowTitle("Destination");
QObject::connect(&mon, &WidgetMonitor::newContents, [&](const QImage &img) {
dst.resize(img.size());
dst.setPixmap(QPixmap::fromImage(img));
});
return app.exec();
}
#include "main.moc"

Open QDialog and run the QProcess simultaneously

This is my btconnect.h file
#ifndef BTCONNECT_H
#define BTCONNECT_H
#include "scandialog.h"
namespace Ui {
class BTConnect;
}
class BTConnect : public QWidget
{
Q_OBJECT
public:
explicit BTConnect(QWidget *parent = 0);
~BTConnect();
private slots:
void on_ScanButton_clicked();
void ScanBTDevices();
//some slots here
void ScanDialogShow();
void ScanDialogClose();
public slots:
//some slots here
private:
Ui::BTConnect *ui;
QProcess BTscan_Process;
scanDialog *scan;
};
#endif // BTCONNECT_H
btconnect.cpp
BTConnect::BTConnect(QWidget *parent) :
QWidget(parent),
ui(new Ui::BTConnect)
{
//set the userinterface as BTConnect.ui
ui->setupUi(this);
scan = new scanDialog(this);
}
void BTConnect::ScanDialogShow()
{
scan->show();
}
void BTConnect::ScanDialogClose()
{
scan->close();
}
void BTConnect::ScanBTDevices()
{
ScanDialogShow();
//Command to scan nearby bluetooth devices
//"hcitool scan"
QString cmd("hcitool scan");
//start the process
BTscan_Process.start(cmd);
//Wait for the processs to finish with a timeout of 20 seconds
if(BTscan_Process.waitForFinished(20000))
{
//Clear the list widget
this->ui->listWidget->clear();
//Read the command line output and store it in QString out
QString out(BTscan_Process.readAllStandardOutput());
//Split the QString every new line and save theve in a QStringList
QStringList OutSplit = out.split("\n");
//Parse the QStringList in btCellsParser
btCellsParser cp(OutSplit);
for(unsigned int i = 0; i<cp.count(); i++)
{
//writing in listwidget
}
}
ScanDialogClose();
}
void BTConnect::on_ScanButton_clicked()
{
//Scan for available nearby bluetooth devices
ScanBTDevices();
}
if I use the above code, the qdialog scandialog does open when the process begins and closes when the data is loaded in qlistwidget, but the contents of qdialog scandialog are not displayed. If I were to change show() to exec(), the contents will be shown but the QProcess does not run until the dialog is closed.
I want the dialog to open when the Qprocess starts and close when the qlistwidget is loaded with data from the scan. And I want the contents of scandialog to be displayed. It has two labels. One with .GIF file and another with text saying scanning.
Any help is appreciated.
you never return to the event loop when you do show (because of waitForFinished) and you never continue to the processing code when you do exec
instead of the waitForFinished you should connect to the finished signal and handle it there and use a single shot timer that will cancel it:
void BTConnect::on_BTscanFinished()//new slot
{
//Clear the list widget
this->ui->listWidget->clear();
//Read the command line output and store it in QString out
QString out(BTscan_Process.readAllStandardOutput());
//Split the QString every new line and save theve in a QStringList
QStringList OutSplit = out.split("\n");
//Parse the QStringList in btCellsParser
btCellsParser cp(OutSplit);
for(unsigned int i = 0; i<cp.count(); i++)
{
//writing in listwidget
}
ScanDialogClose();
}
void BTConnect::ScanBTDevices()
{
ScanDialogShow();
//Command to scan nearby bluetooth devices
//"hcitool scan"
QString cmd("hcitool scan");
//start the process
connect(BTscan_Process, SIGNAL(finished()), this, SLOT(on_BTscanFinished()));
BTscan_Process.start(cmd);
QTimer::singleShot(20000, scan, SLOT(close()));
}
The problem is that QDialog::exec and QProcess::waitForFinished functions block event loop. Never ever block event loop. So you just need to do things more asynchronously.
QProcess class can be handled asynchronously using signals like readReadStandardOutput. And QDialog can be shown asynchronously using open slot.
The example:
void ScanBTDevices() {
// Open dialog when process is started
connect(process, SIGNAL(started()), dialog, SLOT(open()));
// Read standard output asynchronously
connect(process, SIGNAL(readyReadStandardOutput()), SLOT(onReadyRead()));
// Start process asynchronously (for example I use recursive ls)
process->start("ls -R /");
}
void onReadyRead() {
// Write to list widget
dialog->appendText(QString(process->readAllStandardOutput()));
}
The data will be appended to the dialog during generating by process.
Also using QProcess::finished signal and you can close the dialog.

Video too fast in Qt using OpenCV

I am playing a video on label in Qt. I am using Open CV for the same. The video is playing but it is too fast. How can I decrease the playback speed. I tried using setCaptureProperty but it is not working. My code is as follows
HeaderFile Declarations:
CvCapture *capture;
IplImge *frame;
cv::Mat source_image;
cv::Mat dest_image;
QTimer *imageTimer;
Button click slot:
void MainWindow::onButtonClick()
{
capture = cvCaptureFromFile("/mp.mp4");
while(capture
{
frame = cvQueryFrame((capture);
source_image = frame;
cv::resize(source_image,source_image,cv::Size(420,180),0,0);
cv::cvtColor(source_image,source_image,CV_BGR2RGB);
QImage qimg = QImage((const unsigned char*)source_image.data,source_image.cols,source_imge.rows,QImage::Format_RGB888);
label->setPixmap(QPixmap::fromImage(qimg));
label->resize(label->pixmap()->size());
}
}
Somebody please guide on this...Thank You :)
I use Qtimer in this way,not the while loop,like following:
void on_button_click()
{
cap.open(0);
timer->start(50);
}
void readframe()
{
//display image in label
cap>>frame;
Mat2QImage(); // convert mat to QImage;
...
//setpixmap();
...
}
and in the main window,
connet(timer,timeout(),this,readframe());

Multi-threading with Qt

I am building a simple GUI in Qt to carry out some Image Processing in OpenCV. I want to use multi-threading so that the GUI is responsive even if the processing becomes very intensive. To do this I am referring the chapter below to build my framework:
http://www.informit.com/articles/article.aspx?p=1405551&seqNum=3
To begin with, I have 2 push buttons, one to load the image and the other to process it.
I have 2 Labels, one to display the input image and one to display the processed image.
As of now, I am loading my input image using slot and signal mechanism in the main thread and I am creating a new thread for the image flip.
However, when I build my code, I get an error
Undefined symbols for architecture x86_64:
"FlipTransaction::FlipTransaction(int)", referenced from:
MainWindow::flipHorizontally() in mainwindow.o ld: symbol(s) not found for architecture x86_64
When I comment out the slot flipHorizontally(), my code builds fine and am able to load the image.
Thus my processing is not being carried out.
Below is my code. Any help is appreciated
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QStatusBar>
// OpenCV Headers
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> // for cvtColor
// Multi-Threading Headers
#include <QThread>
#include <QMutex>
#include <QQueue>
#include <QWaitCondition>
namespace Ui {
class MainWindow;
}
class Transaction
{
public:
virtual ~Transaction() {}
virtual QImage apply(const cv::Mat source_image, cv::Mat dest_image) = 0;
virtual QString message() = 0;
};
class TransactionThread : public QThread
{
Q_OBJECT
public:
TransactionThread();
~TransactionThread();
void addTransaction(Transaction *transact);
void setImage(const QImage &image);
QImage image();
signals:
void transactionStarted(const QString &message);
void allTransactionsDone();
protected:
void run();
private:
QImage currentImage;
Transaction *EndTransaction;
QQueue<Transaction *> transactions;
QWaitCondition transactionAdded;
QMutex mutex;
cv::Mat source_image;
cv::Mat dest_image;
};
class FlipTransaction : public Transaction
{
public:
FlipTransaction(int orientation);
QImage apply(const cv::Mat source_image, cv::Mat dest_image);
QString message();
private:
int orientation;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void addTransaction(Transaction *transact);
void drawOut(QImage qimg);
private slots:
void on_pushButton_clicked();
public slots:
void flipHorizontally();
void allTransactionsDone();
private:
Ui::MainWindow *ui;
public:
TransactionThread thread;
cv::Mat source_image; // Input Image Variable
cv::Mat dest_image; // Output Image Variable
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
statusBar()->showMessage(tr("Ready"), 2000);
connect(&thread, SIGNAL(transactionStarted(const QString &)),
statusBar(), SLOT(showMessage(const QString &)));
connect(&thread, SIGNAL(allTransactionsDone()),
this, SLOT(allTransactionsDone()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::drawOut(QImage qimg)
{
// Display on Label
ui->outputLabel->setPixmap(QPixmap::fromImage(qimg));
// Resize the label to fit the image
ui->outputLabel->resize(ui->outputLabel->pixmap()->size());
}
void MainWindow::addTransaction(Transaction *transact)
{
MainWindow::thread.addTransaction(transact);
}
void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Image"), ".",tr("Image Files (*.png *.jpg *.jpeg *.bmp *.gif)"));
// Read Image
source_image = cv::imread(filename.toAscii().data());
// Resize Image
cv::resize(source_image, source_image, cv::Size(128,128) , 0, 0);
// Change to RGB format
cv::cvtColor(source_image,source_image,CV_BGR2RGB);
// Convert to QImage
QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols, source_image.rows, QImage::Format_RGB888); // convert to QImage
// Display on Input Label
ui->inputLabel->setPixmap(QPixmap::fromImage(qimg));
// Resize the label to fit the image
ui->inputLabel->resize(ui->inputLabel->pixmap()->size());
}
void MainWindow::flipHorizontally()
{
MainWindow::thread.addTransaction(new FlipTransaction(int(1)));
}
void MainWindow::allTransactionsDone()
{
statusBar()->showMessage(tr("Ready"), 2000);
}
TransactionThread::TransactionThread()
{
start();
}
TransactionThread::~TransactionThread()
{
{
QMutexLocker locker(&mutex);
while(!transactions.isEmpty())
delete transactions.dequeue();
transactions.enqueue(EndTransaction);
transactionAdded.wakeOne();
}
wait();
}
void TransactionThread::addTransaction(Transaction *transact)
{
QMutexLocker locker(&mutex);
transactions.enqueue(transact);
transactionAdded.wakeOne();
}
void TransactionThread::setImage(const QImage &image)
{
QMutexLocker locker(&mutex);
currentImage = image;
}
QImage TransactionThread::image()
{
QMutexLocker locker(&mutex);
return currentImage;
}
void TransactionThread::run()
{
Transaction *transact = 0;
QImage oldImage;
forever {
{
QMutexLocker locker(&mutex);
if (transactions.isEmpty())
transactionAdded.wait(&mutex);
transact = transactions.dequeue();
if (transact == EndTransaction)
break;
oldImage = currentImage;
}
emit transactionStarted(transact->message());
QImage newImage = transact->apply(source_image, dest_image);
// QImage newImage = transact->apply(oldImage);
delete transact;
{
QMutexLocker locker(&mutex);
currentImage = newImage;
if (transactions.isEmpty())
emit allTransactionsDone();
}
}
}
QImage FlipTransaction::apply(const cv::Mat source_image, cv::Mat dest_image)
{
// Process Image
cv::flip(source_image, dest_image, orientation);
// Change to RGB format
cv::cvtColor(dest_image,dest_image,CV_BGR2RGB);
// Convert to QImage
QImage qimg = QImage((const unsigned char*) dest_image.data, dest_image.cols, dest_image.rows, QImage::Format_RGB888);
return qimg;
}
QString FlipTransaction::message()
{
if (orientation == 1) {
return QObject::tr("Flipping image horizontally...");
} else {
return QObject::tr("Flipping image vertically...");
}
}

Resources