From 6da80868dc78e781abe0e44a726200d98c66ed1b Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 13 Nov 2015 13:13:15 +0100 Subject: [PATCH] [TASK] Basic implementation of automatic contrast adaption. --- imageviewer-qt4.cpp | 83 ++++++++++++++++++++++++++++++++++++++++----- imageviewer-qt4.h | 4 +++ 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index 16cf53a..1c06e92 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -85,6 +85,8 @@ int ImageViewer::calcIntensity(int r, int g, int b) { /** * In case it is needed, convert the loaded image into grayscale. + * This is one of the only methods modifying original_image! + * * @brief ImageViewer::convertToMonochrome */ void ImageViewer::convertToMonochrome() { @@ -98,8 +100,8 @@ void ImageViewer::convertToMonochrome() { color.getRgb(&r,&g,&b); int intensity = calcIntensity(r, g, b); color = QColor::fromRgb(intensity, intensity, intensity); - image->setPixel(x, y, color.rgba()); - original_image->setPixel(x, y, color.rgba()); + image->setPixel(x, y, color.rgb()); + original_image->setPixel(x, y, color.rgb()); } } updateImageDisplay(); @@ -129,7 +131,7 @@ void ImageViewer::drawBlackLine() { */ void ImageViewer::drawDiagonalCross() { if(image!=NULL) { - int color = QColor::fromHsl(120,255,125).rgba(); + int color = QColor::fromHsl(120,255,125).rgb(); int width = image->width(); int height = image->height(); for(int y=0; yheight(); y++) { @@ -165,7 +167,7 @@ void ImageViewer::drawRainbowCross(int initialHue=0) { int range = line_slider->value(); if(range > image->width()-2) range = image->width()-2; for(int i=0;iwidth(),image->height());i++) { - int color = myColor.rgba(); + int color = myColor.rgb(); for(int r=0; rsetPixel(i+r,i,color); image->setPixel((image_width-i)-r,i,color); @@ -284,7 +286,7 @@ void ImageViewer::updateHistogramm() { histogramm = new QImage(256, 100, QImage::Format_RGB32); histogramm->fill(QColor::fromRgb(200,200,200)); - int black = QColor::fromRgb(0,0,0).rgba(); + int black = QColor::fromRgb(0,0,0).rgb(); for(int x=0; x<256; x++) { int k_max = (int) qRound((100*grayscale_relative_histogramm[x])/max); for(int y=0; y 255) new_l = 255; if(new_l < 0) new_l = 0; color.setHsl(h, s, new_l); - image->setPixel(x, y, color.rgba()); + image->setPixel(x, y, color.rgb()); } } updateImageDisplay(); @@ -337,7 +339,7 @@ void ImageViewer::adjustContrast(int c) { if(new_l > 255) new_l = 255; if(new_l < 0) new_l = 0; color.setHsl(h, s, new_l); - image->setPixel(x, y, color.rgba()); + image->setPixel(x, y, color.rgb()); } } updateImageDisplay(); @@ -345,12 +347,67 @@ void ImageViewer::adjustContrast(int c) { renewLogging(); } +/** + * Using the percentile given by the slider, do a robust automatic contrast adaption. + * + * @brief ImageViewer::robustAutomaticContrastAdaption + */ +void ImageViewer::robustAutomaticContrastAdaption(void) { + int percentile = contrast_percentile_slider->value(); + logFile << "Doing automatic robust contrast adaption (" << percentile << "%), hold tight... " << std::endl; + renewLogging(); + + // Determine upper and lower "borders" + int lower_border = -1; + int upper_border = -1; + double limit = percentile / 100.0; + double cursor = 0.0; + for(int i=0; i<256; i++) { + cursor += grayscale_relative_histogramm[i]; + if(cursor >= limit && lower_border == -1) { + lower_border = i-1; + if(lower_border < 0) lower_border = 0; + } + if(cursor >= 1-limit && upper_border == -1) { + upper_border = i; + if(upper_border > 255) upper_border = 255; + break; + } + } + + logFile << "lower border: " << lower_border << ", upper border: " << upper_border << "." << std::endl; + renewLogging(); + + int h, s, l; + for(int x=0; xwidth(); x++) { + for(int y=0; yheight(); y++) { + QColor color = QColor::fromRgb(original_image->pixel(x, y)); + color.getHsl(&h, &s, &l); + + if(l < lower_border) { + l = 0; + } else if(l > upper_border) { + l = 255; + } else { + l = qRound((l - lower_border) * (255.0/(upper_border-lower_border))); + } + + color.setHsl(h, s, l); + image->setPixel(x, y, color.rgb()); + } + } + updateImageDisplay(); + + logFile << "done." << std::endl; + renewLogging(); +} + /**************************************************************************************** * * GUI elements related to the tasks are set up here. * -*****************************************************************************************/ +**********************contrast_percentile_slider*******************************************************************/ void ImageViewer::generateControlPanels() { // Tab for first task @@ -426,6 +483,14 @@ void ImageViewer::generateControlPanels() { contrast_slider->setValue(255); QObject::connect(contrast_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustContrast(int))); + contrast_percentile_slider = new QSlider(Qt::Horizontal); + contrast_percentile_slider->setMinimum(0); + contrast_percentile_slider->setMaximum(100); + contrast_percentile_slider->setValue(5); + + automatic_robust_contrast = new QPushButton("Automatic robust contrast adaption"); + QObject::connect(automatic_robust_contrast, SIGNAL(clicked()), this, SLOT(robustAutomaticContrastAdaption())); + task_tab2->addWidget(stats); task_tab2->addWidget(new QLabel("Histogramm")); task_tab2->addWidget(histogramm_label); @@ -434,6 +499,8 @@ void ImageViewer::generateControlPanels() { task_tab2->addWidget(brightness_slider); task_tab2->addWidget(new QLabel("Contrast")); task_tab2->addWidget(contrast_slider); + task_tab2->addWidget(contrast_percentile_slider); + task_tab2->addWidget(automatic_robust_contrast); tabWidget->addTab(task_tab_widget2, "Task #2"); diff --git a/imageviewer-qt4.h b/imageviewer-qt4.h index 1cf60e9..dd2a069 100644 --- a/imageviewer-qt4.h +++ b/imageviewer-qt4.h @@ -93,6 +93,9 @@ class ImageViewer : public QMainWindow { QSlider* brightness_slider; QSlider* contrast_slider; + QSlider* contrast_percentile_slider; + QPushButton* automatic_robust_contrast; + // Third task tab QWidget* task_tab_widget3; QVBoxLayout* task_tab3; @@ -114,6 +117,7 @@ class ImageViewer : public QMainWindow { void adjustBrightness(int b); void adjustContrast(int c); void convertToMonochrome(); + void robustAutomaticContrastAdaption(); void open(); void print();