Subversion Repositories OpenCV2-Cookbook

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 3 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#include "mainwindow.h"
19
#include "ui_mainwindow.h"
20
 
21
MainWindow::MainWindow(QWidget *parent) :
22
    QMainWindow(parent),
23
    ui(new Ui::MainWindow)
24
{
25
    ui->setupUi(this);
26
 
27
    //select color
28
    connect(ui->pushButton_color, SIGNAL(clicked()), this, SLOT(setColor()));
29
    connect(ui->actionChoose_Color, SIGNAL(triggered()), this, SLOT(setColor()));
30
 
31
    //open image
32
    connect(ui->pushButton_openImage, SIGNAL(clicked()), this, SLOT(setImage()));
33
    connect(ui->actionOpen_Image, SIGNAL(triggered()), this, SLOT(setImage()));
34
 
35
    //process Color Detection
36
    connect(ui->pushButton_process, SIGNAL(clicked()), this, SLOT(processColorDetection()));
37
    connect(ui->actionProcess, SIGNAL(triggered()), this, SLOT(processColorDetection()));
38
 
39
}
40
 
41
MainWindow::~MainWindow()
42
{
43
    delete ui;
44
}
45
 
46
void MainWindow::changeEvent(QEvent *e)
47
{
48
    QMainWindow::changeEvent(e);
49
    switch (e->type()) {
50
    case QEvent::LanguageChange:
51
        ui->retranslateUi(this);
52
        break;
53
    default:
54
        break;
55
    }
56
}
57
 
58
void MainWindow::setImage()
59
{
60
    QFileDialog::Options options;
61
    QString selectedFilter;
62
    QString fileName = QFileDialog::getOpenFileName(this,
63
                                tr("Open Image Files"),
64
                                "",
65
                                tr("Image files (*.jpg *.jpeg *.png *.gif *.bmp)"),
66
                                &selectedFilter,
67
                                options);
68
    if (!fileName.isEmpty()){
69
        cv::Mat img_mat = cv::imread(fileName.toStdString(),1); //0 for grayscale
70
        displayMat(img_mat);
71
    }
72
    //Set Filename
73
    ColorDetectController::getInstance()->setInputImage(fileName.toStdString());
74
}
75
 
76
//Convert cv::Mat to QImage and display
77
void MainWindow::displayMat(const cv::Mat& image){
78
 
79
    //BGR openCV Mat to QImage
80
    QImage img_qt = QImage((const unsigned char*)image.data,image.cols, image.rows, image.step, QImage::Format_RGB888);
81
 
82
    //For Binary Images
83
    if (img_qt.isNull()){
84
        //ColorTable for Binary Images
85
        QVector<QRgb> colorTable;
86
        for (int i = 0; i < 256; i++)
87
            colorTable.push_back(qRgb(i, i, i));
88
 
89
        img_qt = QImage((const unsigned char*)image.data,image.cols, image.rows, QImage::Format_Indexed8);
90
        img_qt.setColorTable(colorTable);
91
        }
92
 
93
    //Display the QImage in the Label
94
    QPixmap img_pix = QPixmap::fromImage(img_qt.rgbSwapped()); //BGR to RGB
95
    this->ui->label->setPixmap(img_pix.scaled(ui->label->size(), Qt::KeepAspectRatio));
96
}
97
 
98
void MainWindow::on_verticalSlider_Threshold_valueChanged(int value)
99
{
100
    QString cdt("Color Distance Threshold: ");
101
    cdt.append(QString::number(value));
102
    this->ui->label_2->setText(cdt);
103
}
104
 
105
void MainWindow::setColor()
106
{
107
    QColor color = QColorDialog::getColor(Qt::green, this);
108
    if (color.isValid()) {
109
       ColorDetectController::getInstance()->setTargetColor(color.red(),color.green(),color.blue());
110
    }
111
}
112
 
113
void MainWindow::processColorDetection()
114
{
115
    ColorDetectController::getInstance()->setColorDistanceThreshold(ui->verticalSlider_Threshold->value());
116
    ColorDetectController::getInstance()->process();
117
 
118
    cv::Mat resulting = ColorDetectController::getInstance()->getLastResult();
119
    if (!resulting.empty())
120
        displayMat(resulting);
121
 
122
}