From 1d09fcdcc76c9489a54992ba369d188d7edca082 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 30 Oct 2015 13:52:23 +0100 Subject: [PATCH] [TASK] Implement Task#2 Sub 1. --- imageviewer-qt4.cpp | 118 ++++++++++++++++++++++++++++++++++++-------- imageviewer-qt4.h | 20 +++++--- 2 files changed, 111 insertions(+), 27 deletions(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index 9fcf079..94fb3f5 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -142,6 +142,78 @@ void ImageViewer::acidTrippin() { } } +/** + * Analyze the image, get average luminance + * Also fill grayscale_absolute_histogramm + * @brief ImageViewer::analyzeImage + */ +void ImageViewer::analyzeImage() { + if(image!=NULL) { + //Zero existing histogramm data first + for(int i=0; i<256; i++) { + grayscale_absolute_histogramm[i] = 0; + grayscale_relative_histogramm[i] = 0; + } + + logFile << "Analyzing image ..."; + renewLogging(); + for(int x=0; xwidth(); x++) { + for(int y=0; yheight(); y++) { + logFile << "(" << x << "," << y << "): "; + int r,g,b; + QColor color = QColor::fromRgb(image->pixel(x, y)); + color.getRgb(&r,&g,&b); + logFile << "(" << r << "," << g << "," << b << ")"; + int intensity = (int)(0.299*r+0.587*g+0.114*b); + logFile << "(" << intensity << ")"; + grayscale_absolute_histogramm[intensity] += 1; + logFile << std::endl; + } + } + + int pixels = image->width()*image->height(); + for(int i=0; i<256; i++) { + grayscale_relative_histogramm[i] = ((1.0*grayscale_absolute_histogramm[i])/(1.0*pixels)); + } + + logFile << "done" << std::endl; + logFile << "Average intensity: " << getAverageIntensity() << std::endl; + logFile << "Intensity variance: " << getIntensityVariance() << std::endl; + renewLogging(); + } +} + +/** + * Use histogramm to retrieve the average intensity. + * @brief ImageViewer::getAverageIntensity + */ +int ImageViewer::getAverageIntensity() { + double sumIntensity = 0; + for(int i=0; i<256;i++) { + sumIntensity += (i*grayscale_relative_histogramm[i]); + } + return (int)sumIntensity; +} + +/** + * Retrieve the intensity variance from the image. + * @brief ImageViewer::getVarianceIntensity + */ +int ImageViewer::getIntensityVariance() { + int average_intensity = getAverageIntensity(); + int sumDifference = 0; + for(int x=0; xwidth(); x++) { + for(int y=0; yheight(); y++) { + QColor color = QColor::fromRgb(image->pixel(x, y)); + int r,g,b; + color.getRgb(&r,&g,&b); + int intensity = (int)(0.299*r+0.587*g+0.114*b); + int diff = std::abs(intensity - average_intensity); + sumDifference += diff; + } + } + return (int) (sumDifference/(image->width()*image->height())); +} /**************************************************************************************** * @@ -155,21 +227,21 @@ void ImageViewer::generateControlPanels() { task_tab1 = new QVBoxLayout(); task_tab_widget1->setLayout(task_tab1); - button1 = new QPushButton(); - button1->setText("Draw a black line"); - QObject::connect(button1, SIGNAL(clicked()), this, SLOT (drawBlackLine())); + task_tab1_button1 = new QPushButton(); + task_tab1_button1->setText("Draw a black line"); + QObject::connect(task_tab1_button1, SIGNAL(clicked()), this, SLOT (drawBlackLine())); - button2 = new QPushButton(); - button2->setText("Draw a rainbow cross"); - QObject::connect(button2, SIGNAL(clicked()), this, SLOT (drawRainbowCross())); + task_tab1_button2 = new QPushButton(); + task_tab1_button2->setText("Draw a rainbow cross"); + QObject::connect(task_tab1_button2, SIGNAL(clicked()), this, SLOT (drawRainbowCross())); - button3 = new QPushButton(); - button3->setText("Start the neverending acid trip!"); - QObject::connect(button3, SIGNAL(clicked()), this, SLOT (acidTrippin())); + task_tab1_button3 = new QPushButton(); + task_tab1_button3->setText("Start the neverending acid trip!"); + QObject::connect(task_tab1_button3, SIGNAL(clicked()), this, SLOT (acidTrippin())); - button4 = new QPushButton(); - button4->setText("Draw a diagonal cross"); - QObject::connect(button4, SIGNAL(clicked()), this, SLOT (drawDiagonalCross())); + task_tab1_button4 = new QPushButton(); + task_tab1_button4->setText("Draw a diagonal cross"); + QObject::connect(task_tab1_button4, SIGNAL(clicked()), this, SLOT (drawDiagonalCross())); lineSlider = new QSlider(Qt::Horizontal); lineSlider->setMinimum(1); @@ -178,10 +250,10 @@ void ImageViewer::generateControlPanels() { //QObject::connect(lineSlider, SIGNAL(valueChanged(int)), this, SLOT (drawRainbowCross())); task_tab1->addWidget(new QLabel("Let's draw something!")); - task_tab1->addWidget(button1); - task_tab1->addWidget(button2); - task_tab1->addWidget(button3); - task_tab1->addWidget(button4); + task_tab1->addWidget(task_tab1_button1); + task_tab1->addWidget(task_tab1_button2); + task_tab1->addWidget(task_tab1_button3); + task_tab1->addWidget(task_tab1_button4); task_tab1->addWidget(new QLabel("This will not stop unless process is killed.")); task_tab1->addWidget(lineSlider); task_tab1->addWidget(new QLabel("Sets the width of the cross.")); @@ -190,17 +262,21 @@ void ImageViewer::generateControlPanels() { //Tab for second task + task_tab2_button1 = new QPushButton(); + task_tab2_button1->setText("Analyze image"); + QObject::connect(task_tab2_button1, SIGNAL(clicked()), this, SLOT (analyzeImage())); + + QLabel* task_tab2_stats = new QLabel(); + task_tab_widget2 = new QWidget(); task_tab2 = new QVBoxLayout(); task_tab_widget2->setLayout(task_tab2); - spinbox1 = new QSpinBox(tabWidget); - - task_tab2->addWidget(new QLabel("Here we put some more controls later.")); - task_tab2->addWidget(spinbox1); + task_tab2->addWidget(task_tab2_button1); + task_tab2->addWidget(task_tab2_stats); tabWidget->addTab(task_tab_widget2,"Task #2"); - //tabWidget->show(); + tabWidget->show(); } diff --git a/imageviewer-qt4.h b/imageviewer-qt4.h index dd44bba..f3d10ad 100644 --- a/imageviewer-qt4.h +++ b/imageviewer-qt4.h @@ -70,10 +70,10 @@ class ImageViewer : public QMainWindow { QWidget* task_tab_widget1; QVBoxLayout* task_tab1; - QPushButton* button1; - QPushButton* button2; - QPushButton* button3; - QPushButton* button4; + QPushButton* task_tab1_button1; + QPushButton* task_tab1_button2; + QPushButton* task_tab1_button3; + QPushButton* task_tab1_button4; QSlider* lineSlider; @@ -81,7 +81,12 @@ class ImageViewer : public QMainWindow { QWidget* task_tab_widget2; QVBoxLayout* task_tab2; - QSpinBox* spinbox1; + QPushButton* task_tab2_button1; + QLabel* task_tab2_stats; + + // "My" space for storing data/results + int grayscale_absolute_histogramm[256]; + double grayscale_relative_histogramm[256]; private slots: @@ -90,7 +95,8 @@ class ImageViewer : public QMainWindow { void drawRainbowCross(); void drawDiagonalCross(); void acidTrippin(); - + void analyzeImage(); + void open(); void print(); void zoomIn(); @@ -110,6 +116,8 @@ class ImageViewer : public QMainWindow { private: // Custom methods void drawRainbowCross(int h); + int getAverageIntensity(); + int getIntensityVariance(); void generateControlPanels(); void startLogging();