From c0ed0711905b36ea63c907d4664fb133c128aba6 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 20 Nov 2015 13:31:27 +0100 Subject: [PATCH] [TASK] Almost finish piecebased histogramm adaption. --- imageviewer-qt4.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++- imageviewer-qt4.h | 2 ++ lazy_image.cpp | 8 ++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index ab854ad..176b0c4 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -363,6 +363,59 @@ void ImageViewer::linearHistogrammAdaption(void) { renewLogging(); } +/** + * Do a piece based linear histogramm adaption using a reference image. + * + * @brief Imageviewer::pieceLinearHistogrammAdaption + */ +void ImageViewer::pieceLinearHistogrammAdaption() { + logFile << "Doing linear histogramm adaption ..." << std::endl; + renewLogging(); + if(histogramm_reference == NULL) { + logFile << "No reference image loaded! Aborting." << std::endl; + renewLogging(); + return; + } else { + // Create an intensity map + int histogramm_map[256]; + for(int i=0; i<256; i++) histogramm_map[i] = -1; + int source_cursor = 0; + double used_space = 0.0; + double* source_histogramm = working_copy->getRelativeIntensityHistogramm(); + double* target_cummulative_histogramm = histogramm_reference->getRelativeCumulativeIntensityHistogramm(); + for(int i=0; i<256; i++) { + double free_total_space = target_cummulative_histogramm[i]; + while(source_histogramm[source_cursor] <= (free_total_space - used_space)) { + histogramm_map[source_cursor] = i; + used_space += source_histogramm[source_cursor]; + source_cursor++; + } + } + + for(int i=0; i<256; i++) { + logFile << i << ": " << histogramm_map[i] << std::endl; + } + renewLogging(); + + // Apply the map for each pixel. + int h, s, l; + for(int x=0; xgetImage()->width(); x++) { + for(int y=0; ygetImage()->height(); y++) { + QColor color = QColor::fromRgb(original->getImage()->pixel(x, y)); + color.getHsl(&h, &s, &l); + l = histogramm_map[l]; + color.setHsl(h, s, l); + working_copy->getImage()->setPixel(x, y, color.rgb()); + } + } + // Done. + } + logFile << "done." << std::endl; + renewLogging(); + updateImageDisplay(); +} + + /**************************************************************************************** * @@ -511,7 +564,10 @@ void ImageViewer::generateControlPanels() { linear_histogramm_adaption = new QPushButton("Do linear histogramm adaption (basic)"); QObject::connect(linear_histogramm_adaption, SIGNAL(clicked()), this, SLOT(linearHistogrammAdaption())); - + + piece_linear_histogramm_adaption = new QPushButton("Do linear histogramm adaption (pieces)"); + QObject::connect(piece_linear_histogramm_adaption, SIGNAL(clicked()), this, SLOT(pieceLinearHistogrammAdaption())); + task_tab3->addWidget(new QLabel("Reference image")); task_tab3->addWidget(reference_histogramm_cumulative_label); task_tab3->addWidget(new QLabel("Original image")); @@ -519,6 +575,7 @@ void ImageViewer::generateControlPanels() { task_tab3->addWidget(new QLabel("Working copy")); task_tab3->addWidget(histogramm_cumulative_label); task_tab3->addWidget(linear_histogramm_adaption); + task_tab3->addWidget(piece_linear_histogramm_adaption); tabWidget->addTab(task_tab_widget3, "Task #3"); diff --git a/imageviewer-qt4.h b/imageviewer-qt4.h index 3c49e95..c658703 100644 --- a/imageviewer-qt4.h +++ b/imageviewer-qt4.h @@ -110,6 +110,7 @@ class ImageViewer : public QMainWindow { QLabel* histogramm_cumulative_label; QPushButton* linear_histogramm_adaption; + QPushButton* piece_linear_histogramm_adaption; // "My" space for storing data/results LazyImage* original; @@ -129,6 +130,7 @@ class ImageViewer : public QMainWindow { void saveToOriginal(); void robustAutomaticContrastAdaption(int c_param); void linearHistogrammAdaption(); + void pieceLinearHistogrammAdaption(); void open(); void openReference(); diff --git a/lazy_image.cpp b/lazy_image.cpp index 503b208..8e9a195 100644 --- a/lazy_image.cpp +++ b/lazy_image.cpp @@ -16,6 +16,7 @@ class LazyImage { QImage* histogramm_normal_image; QImage* histogramm_cumulative_image; double histogramm_relative_intensity[256]; + double histogramm_relative_cumulative_intensity[256]; int histogramm_absolute_intensity[256]; int histogramm_absolute_cumulative_intensity[256]; int intensity_average; @@ -42,10 +43,13 @@ class LazyImage { // Calculate relative histogramm and absolute cumulative histogramm int pixels = this->img->width()*this->img->height(); int sum = 0; + double relative_sum = 0.0; for(int i=0; i<256; i++) { histogramm_relative_intensity[i] = (((double) histogramm_absolute_intensity[i])/((double) pixels)); sum += histogramm_absolute_intensity[i]; + relative_sum += histogramm_relative_intensity[i]; histogramm_absolute_cumulative_intensity[i] = sum; + histogramm_relative_cumulative_intensity[i] = relative_sum; } }; @@ -181,6 +185,10 @@ class LazyImage { return this->histogramm_relative_intensity; }; + double* getRelativeCumulativeIntensityHistogramm(void) { + return this->histogramm_relative_cumulative_intensity; + }; + void convertToMonochrome(void) { for(int x=0; ximg->width(); x++) { for(int y=0; yimg->height(); y++) {