[TASK] Basic implementation of automatic contrast adaption.

This commit is contained in:
Jan Philipp Timme 2015-11-13 13:13:15 +01:00
parent c162f76815
commit 6da80868dc
2 changed files with 79 additions and 8 deletions

View File

@ -85,6 +85,8 @@ int ImageViewer::calcIntensity(int r, int g, int b) {
/** /**
* In case it is needed, convert the loaded image into grayscale. * In case it is needed, convert the loaded image into grayscale.
* This is one of the only methods modifying original_image!
*
* @brief ImageViewer::convertToMonochrome * @brief ImageViewer::convertToMonochrome
*/ */
void ImageViewer::convertToMonochrome() { void ImageViewer::convertToMonochrome() {
@ -98,8 +100,8 @@ void ImageViewer::convertToMonochrome() {
color.getRgb(&r,&g,&b); color.getRgb(&r,&g,&b);
int intensity = calcIntensity(r, g, b); int intensity = calcIntensity(r, g, b);
color = QColor::fromRgb(intensity, intensity, intensity); color = QColor::fromRgb(intensity, intensity, intensity);
image->setPixel(x, y, color.rgba()); image->setPixel(x, y, color.rgb());
original_image->setPixel(x, y, color.rgba()); original_image->setPixel(x, y, color.rgb());
} }
} }
updateImageDisplay(); updateImageDisplay();
@ -129,7 +131,7 @@ void ImageViewer::drawBlackLine() {
*/ */
void ImageViewer::drawDiagonalCross() { void ImageViewer::drawDiagonalCross() {
if(image!=NULL) { if(image!=NULL) {
int color = QColor::fromHsl(120,255,125).rgba(); int color = QColor::fromHsl(120,255,125).rgb();
int width = image->width(); int width = image->width();
int height = image->height(); int height = image->height();
for(int y=0; y<image->height(); y++) { for(int y=0; y<image->height(); y++) {
@ -165,7 +167,7 @@ void ImageViewer::drawRainbowCross(int initialHue=0) {
int range = line_slider->value(); int range = line_slider->value();
if(range > image->width()-2) range = image->width()-2; if(range > image->width()-2) range = image->width()-2;
for(int i=0;i<std::min(image->width(),image->height());i++) { for(int i=0;i<std::min(image->width(),image->height());i++) {
int color = myColor.rgba(); int color = myColor.rgb();
for(int r=0; r<range; r++) { for(int r=0; r<range; r++) {
image->setPixel(i+r,i,color); image->setPixel(i+r,i,color);
image->setPixel((image_width-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 = new QImage(256, 100, QImage::Format_RGB32);
histogramm->fill(QColor::fromRgb(200,200,200)); 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++) { for(int x=0; x<256; x++) {
int k_max = (int) qRound((100*grayscale_relative_histogramm[x])/max); int k_max = (int) qRound((100*grayscale_relative_histogramm[x])/max);
for(int y=0; y<k_max; y++) { for(int y=0; y<k_max; y++) {
@ -312,7 +314,7 @@ void ImageViewer::adjustBrightness(int b) {
if(new_l > 255) new_l = 255; if(new_l > 255) new_l = 255;
if(new_l < 0) new_l = 0; if(new_l < 0) new_l = 0;
color.setHsl(h, s, new_l); color.setHsl(h, s, new_l);
image->setPixel(x, y, color.rgba()); image->setPixel(x, y, color.rgb());
} }
} }
updateImageDisplay(); updateImageDisplay();
@ -337,7 +339,7 @@ void ImageViewer::adjustContrast(int c) {
if(new_l > 255) new_l = 255; if(new_l > 255) new_l = 255;
if(new_l < 0) new_l = 0; if(new_l < 0) new_l = 0;
color.setHsl(h, s, new_l); color.setHsl(h, s, new_l);
image->setPixel(x, y, color.rgba()); image->setPixel(x, y, color.rgb());
} }
} }
updateImageDisplay(); updateImageDisplay();
@ -345,12 +347,67 @@ void ImageViewer::adjustContrast(int c) {
renewLogging(); 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; x<image->width(); x++) {
for(int y=0; y<image->height(); 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. * GUI elements related to the tasks are set up here.
* *
*****************************************************************************************/ **********************contrast_percentile_slider*******************************************************************/
void ImageViewer::generateControlPanels() { void ImageViewer::generateControlPanels() {
// Tab for first task // Tab for first task
@ -426,6 +483,14 @@ void ImageViewer::generateControlPanels() {
contrast_slider->setValue(255); contrast_slider->setValue(255);
QObject::connect(contrast_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustContrast(int))); 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(stats);
task_tab2->addWidget(new QLabel("Histogramm")); task_tab2->addWidget(new QLabel("Histogramm"));
task_tab2->addWidget(histogramm_label); task_tab2->addWidget(histogramm_label);
@ -434,6 +499,8 @@ void ImageViewer::generateControlPanels() {
task_tab2->addWidget(brightness_slider); task_tab2->addWidget(brightness_slider);
task_tab2->addWidget(new QLabel("Contrast")); task_tab2->addWidget(new QLabel("Contrast"));
task_tab2->addWidget(contrast_slider); task_tab2->addWidget(contrast_slider);
task_tab2->addWidget(contrast_percentile_slider);
task_tab2->addWidget(automatic_robust_contrast);
tabWidget->addTab(task_tab_widget2, "Task #2"); tabWidget->addTab(task_tab_widget2, "Task #2");

View File

@ -93,6 +93,9 @@ class ImageViewer : public QMainWindow {
QSlider* brightness_slider; QSlider* brightness_slider;
QSlider* contrast_slider; QSlider* contrast_slider;
QSlider* contrast_percentile_slider;
QPushButton* automatic_robust_contrast;
// Third task tab // Third task tab
QWidget* task_tab_widget3; QWidget* task_tab_widget3;
QVBoxLayout* task_tab3; QVBoxLayout* task_tab3;
@ -114,6 +117,7 @@ class ImageViewer : public QMainWindow {
void adjustBrightness(int b); void adjustBrightness(int b);
void adjustContrast(int c); void adjustContrast(int c);
void convertToMonochrome(); void convertToMonochrome();
void robustAutomaticContrastAdaption();
void open(); void open();
void print(); void print();