[TASK] Add an imagelabel, create and display the histogramm.

This commit is contained in:
Jan Philipp Timme 2015-10-30 18:03:47 +01:00
parent 2d7b437758
commit 6001e3d6ce
2 changed files with 32 additions and 11 deletions

View File

@ -160,27 +160,24 @@ void ImageViewer::analyzeImage() {
renewLogging(); renewLogging();
for(int x=0; x<image->width(); x++) { for(int x=0; x<image->width(); x++) {
for(int y=0; y<image->height(); y++) { for(int y=0; y<image->height(); y++) {
logFile << "(" << x << "," << y << "): ";
int r,g,b; int r,g,b;
QColor color = QColor::fromRgb(image->pixel(x, y)); QColor color = QColor::fromRgb(image->pixel(x, y));
color.getRgb(&r,&g,&b); color.getRgb(&r,&g,&b);
logFile << "(" << r << "," << g << "," << b << ")";
int intensity = (int)(0.299*r+0.587*g+0.114*b); int intensity = (int)(0.299*r+0.587*g+0.114*b);
logFile << "(" << intensity << ")";
grayscale_absolute_histogramm[intensity] += 1; grayscale_absolute_histogramm[intensity] += 1;
logFile << std::endl;
} }
} }
int pixels = image->width()*image->height(); int pixels = image->width()*image->height();
for(int i=0; i<256; i++) { for(int i=0; i<256; i++) {
grayscale_relative_histogramm[i] = ((1.0*grayscale_absolute_histogramm[i])/(1.0*pixels)); grayscale_relative_histogramm[i] = (((double) grayscale_absolute_histogramm[i])/((double) pixels));
} }
logFile << "done" << std::endl; logFile << "done" << std::endl;
logFile << "Average intensity: " << getAverageIntensity() << std::endl; logFile << "Average intensity: " << getAverageIntensity() << std::endl;
logFile << "Intensity variance: " << getIntensityVariance() << std::endl; logFile << "Intensity variance: " << getIntensityVariance() << std::endl;
renewLogging(); renewLogging();
updateHistogramm();
} }
} }
@ -220,14 +217,29 @@ int ImageViewer::getIntensityVariance() {
* Builds up the image for the histogramm. * Builds up the image for the histogramm.
* @brief ImageViewer::updateHistogramm * @brief ImageViewer::updateHistogramm
*/ */
void updateHistogramm() { void ImageViewer::updateHistogramm() {
histogramm = new QImage(256, 100); if(histogramm != NULL) {
histogramm.fill(QColor::fromRgb(255,255,255)); delete histogramm;
histogramm = NULL;
}
//Find biggest value in histogramm data
double max = 0;
for(int i=0; i<256; i++) { for(int i=0; i<256; i++) {
if(grayscale_relative_histogramm[i] > max) max = grayscale_relative_histogramm[i];
}
histogramm = new QImage(256, 100, QImage::Format_RGB32);
histogramm->fill(QColor::fromRgb(150,150,150));
int black = QColor::fromRgb(0,0,0).rgba();
for(int x=0; x<256; x++) {
int k_max = (int)((100*grayscale_relative_histogramm[x])/max);
for(int y=0; y<k_max; y++) {
histogramm->setPixel(x, (100-y)-1, black);
}
} }
logFile << "Histogramm done." << std::endl; logFile << "Histogramm done." << std::endl;
renewLogging(); renewLogging();
histogrammLabel->setPixmap(QPixmap::fromImage(*histogramm));
} }
/**************************************************************************************** /****************************************************************************************
@ -281,6 +293,11 @@ void ImageViewer::generateControlPanels() {
task_tab2_button1->setText("Analyze image"); task_tab2_button1->setText("Analyze image");
QObject::connect(task_tab2_button1, SIGNAL(clicked()), this, SLOT (analyzeImage())); QObject::connect(task_tab2_button1, SIGNAL(clicked()), this, SLOT (analyzeImage()));
histogrammLabel = new QLabel();
histogrammLabel->setBackgroundRole(QPalette::Base);
histogrammLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
histogrammLabel->setScaledContents(false);
QLabel* task_tab2_stats = new QLabel(); QLabel* task_tab2_stats = new QLabel();
task_tab_widget2 = new QWidget(); task_tab_widget2 = new QWidget();
@ -289,6 +306,7 @@ void ImageViewer::generateControlPanels() {
task_tab2->addWidget(task_tab2_button1); task_tab2->addWidget(task_tab2_button1);
task_tab2->addWidget(task_tab2_stats); task_tab2->addWidget(task_tab2_stats);
task_tab2->addWidget(histogrammLabel);
tabWidget->addTab(task_tab_widget2,"Task #2"); tabWidget->addTab(task_tab_widget2,"Task #2");
tabWidget->show(); tabWidget->show();
@ -397,7 +415,7 @@ void ImageViewer::open() {
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName)); QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return; return;
} }
scaleFactor = 1.0; scaleFactor = 1.0;
updateImageDisplay(); updateImageDisplay();
printAct->setEnabled(true); printAct->setEnabled(true);

View File

@ -49,6 +49,7 @@
#include <QSlider> #include <QSlider>
#include <QColor> #include <QColor>
#include <cmath>
#include "fstream" #include "fstream"
class QAction; class QAction;
@ -83,6 +84,7 @@ class ImageViewer : public QMainWindow {
QPushButton* task_tab2_button1; QPushButton* task_tab2_button1;
QLabel* task_tab2_stats; QLabel* task_tab2_stats;
QLabel* histogrammLabel;
// "My" space for storing data/results // "My" space for storing data/results
int grayscale_absolute_histogramm[256]; int grayscale_absolute_histogramm[256];
@ -96,7 +98,8 @@ class ImageViewer : public QMainWindow {
void drawDiagonalCross(); void drawDiagonalCross();
void acidTrippin(); void acidTrippin();
void analyzeImage(); void analyzeImage();
void updateHistogramm();
void open(); void open();
void print(); void print();
void zoomIn(); void zoomIn();