/**************************************************************************** ** ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor ** the names of its contributors may be used to endorse or promote ** products derived from this software without specific prior written ** permission. ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "imageviewer-qt4.h" /** * Constructor * @brief ImageViewer::ImageViewer */ ImageViewer::ImageViewer() { original = NULL; working_copy = NULL; histogramm_reference = NULL; startLogging(); generateMainGui(); renewLogging(); generateControlPanels(); createActions(); createMenus(); resize(1600, 600); } /** * Once an image is loaded, automatically perform these operations. * * analyze the image * allocate brightness map * * @brief ImageViewer::initializeImage */ void ImageViewer::initializeImage() { if(original->getImage() == NULL) { std::cout << "Error! No image provided!" << std::endl; return; } analyzeImage(); } /** * In case it is needed, convert the loaded image into grayscale. * * @brief ImageViewer::convertToMonochrome */ void ImageViewer::convertToMonochrome() { if(original->getImage() != NULL) { logFile << "Converting image to monochrome..."; renewLogging(); working_copy->convertToMonochrome(); updateImageDisplay(); logFile << "done." << std::endl; renewLogging(); } } /** * No big performance, but simple. * Restore the working copy based on the existing original image. * * @brief ImageViewer::resetWorkingCopy */ void ImageViewer::resetWorkingCopy() { logFile << "Resetting working copy ..."; renewLogging(); for(int x=0; xgetImage()->width(); x++) { for(int y=0; ygetImage()->height(); y++) { working_copy->getImage()->setPixel(x, y, original->getImage()->pixel(x, y)); } } updateImageDisplay(); logFile << "done." << std::endl; renewLogging(); } /** * No big performance, but simple. * Save working copy to original image. * * @brief ImageViewer::saveToOriginal */ void ImageViewer::saveToOriginal() { logFile << "Saving working copy to original ..."; renewLogging(); for(int x=0; xgetImage()->width(); x++) { for(int y=0; ygetImage()->height(); y++) { original->getImage()->setPixel(x, y, working_copy->getImage()->pixel(x, y)); } } updateImageDisplay(); logFile << "done." << std::endl; renewLogging(); } /** * Draws a simple, black line. * @brief ImageViewer::drawBlackLine */ void ImageViewer::drawBlackLine() { if(original->getImage() != NULL) { for(int i=0;igetImage()->width(),working_copy->getImage()->height());i++) { working_copy->getImage()->setPixel(i,i,0); } updateImageDisplay(); logFile << "Black line drawn." << std::endl; renewLogging(); } } /** * Draws a diagonal cross from edge to edge. * @brief ImageViewer::drawDiagonalCross */ void ImageViewer::drawDiagonalCross() { if(original->getImage() != NULL) { int color = QColor::fromHsl(120,255,125).rgb(); int width = working_copy->getImage()->width(); int height = working_copy->getImage()->height(); for(int y=0; ygetImage()->height(); y++) { working_copy->getImage()->setPixel((1.0*y/height)*width,y,color); working_copy->getImage()->setPixel((1.0-(1.0*y/height))*width,y,color); } } updateImageDisplay(); logFile << "Diagonal edge-to-edge cross drawn." << std::endl; renewLogging(); } /** * Proxy method to actual method due to signal param issues. * @brief ImageViewer::drawRainbowCross */ void ImageViewer::drawRainbowCross() { drawRainbowCross(0); } /** * Draws a diagonal cross with variable width and * varying color. * Fetches the width from the slider. * @brief ImageViewer::drawRainbowCross * @param initialHue */ void ImageViewer::drawRainbowCross(int initialHue=0) { if(original->getImage() != NULL) { int h = initialHue; QColor myColor = QColor::fromHsl(h, 255, 125); int image_width = working_copy->getImage()->width()-1; int range = line_slider->value(); if(range > working_copy->getImage()->width()-2) range = working_copy->getImage()->width()-2; for(int i=0;igetImage()->width(),working_copy->getImage()->height());i++) { int color = myColor.rgb(); for(int r=0; rgetImage()->setPixel(i+r,i,color); working_copy->getImage()->setPixel((image_width-i)-r,i,color); } h++; if(h > 359) h = 0; myColor.setHsl(h, 255, 125); } updateImageDisplay(); } } /** * Analyze the image, get average luminance * Also fill grayscale_absolute_histogramm * * @brief ImageViewer::analyzeImage */ void ImageViewer::analyzeImage() { if(original->getImage() != NULL) { logFile << "Analyzing image ..."; renewLogging(); original->updateStatistics(); working_copy->updateStatistics(); logFile << "done" << std::endl; renewLogging(); QString result = QString("Intensity: Average: %1, Variance: %2").arg(original->getIntensityAverage()).arg(original->getIntensityVariance()); original_stats->setText(result); result = QString("Intensity: Average: %1, Variance: %2").arg(working_copy->getIntensityAverage()).arg(working_copy->getIntensityVariance()); stats->setText(result); histogramm_label->setPixmap(QPixmap::fromImage(*(working_copy->getHistogrammNormalImage()))); original_histogramm_label->setPixmap(QPixmap::fromImage(*(original->getHistogrammNormalImage()))); histogramm_cumulative_label->setPixmap(QPixmap::fromImage(*(working_copy->getHistogrammCumulativeImage()))); original_histogramm_cumulative_label->setPixmap(QPixmap::fromImage(*(original->getHistogrammCumulativeImage()))); } } /** * Fired by the brightness_slider, this one adjusts the brightness on image. * @brief ImageViewer::adjustBrightness */ void ImageViewer::adjustBrightness(int b) { //int h, s, old_l; //int new_l = 0; int delta = b - 255; 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, &old_l); // new_l = old_l + delta; // if(new_l > 255) new_l = 255; // if(new_l < 0) new_l = 0; // color.setHsl(h, s, new_l); struct yuv yuv = LazyImage::RGBToYUV(color.rgb()); yuv.y = yuv.y + (delta / 255.0); if(yuv.y > 1) yuv.y = 1; if(yuv.y < 0) yuv.y = 0; color = LazyImage::YUVToRGB(yuv); working_copy->getImage()->setPixel(x, y, color.rgb()); } } updateImageDisplay(); //logFile << "Brightness adjusted to: " << old_l << " + " << delta << " -> (" << h << ", " << s << ", " << new_l << ")" << std::endl; //renewLogging(); } /** * Fired by the constrast_slider, this one adjusts the contrast on image. * @brief ImageViewer::adjustContrast */ void ImageViewer::adjustContrast(int c) { int h, s, old_l; int new_l = 0; double alpha = c / 255.0; int average_intensity = original->getIntensityAverage(); 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, &old_l); new_l = average_intensity + ((int) round((old_l - average_intensity) * alpha)); if(new_l > 255) new_l = 255; if(new_l < 0) new_l = 0; color.setHsl(h, s, new_l); working_copy->getImage()->setPixel(x, y, color.rgb()); } } updateImageDisplay(); logFile << "Contrast adjusted to: " << old_l << " * " << alpha << " -> (" << h << ", " << s << ", " << new_l << ")" << std::endl; renewLogging(); } /** * Using the percentile given by the slider, do a robust automatic contrast adaption. * * @brief ImageViewer::robustAutomaticContrastAdaption */ void ImageViewer::robustAutomaticContrastAdaption(int c_param) { double* histogramm = original->getRelativeIntensityHistogramm(); double limit = c_param / 1000.0; logFile << "Doing automatic robust contrast adaption (" << limit << "), hold tight ..." << std::endl; renewLogging(); // Determine upper and lower "borders" double cursor; int lower_border = -1; int upper_border = -1; cursor = 0.0; for(int i=0; i<256; i++) { cursor += histogramm[i]; if(cursor > limit) { lower_border = i-1; // get last value before limit overflow if(lower_border < 0) lower_border = 0; break; } } cursor = 0.0; for(int i=255; i>-1; i--) { cursor += histogramm[i]; if(cursor > limit) { upper_border = i+1; // get last value before limit overflow 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; xgetImage()->width(); x++) { for(int y=0; ygetImage()->height(); y++) { QColor color = QColor::fromRgb(original->getImage()->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); working_copy->getImage()->setPixel(x, y, color.rgb()); } } updateImageDisplay(); logFile << "done." << std::endl; renewLogging(); } /** * Do a very basic, linear histogramm adaption. * * @brief ImageViewer::linearHistogrammAdaption */ void ImageViewer::linearHistogrammAdaption(void) { logFile << "Doing linear histogramm adaption ..." << std::endl; renewLogging(); //int h, s, l; int* ach = original->getAbsoluteCumulativeIntensityHistogramm(); int pixels = working_copy->getImage()->width()*working_copy->getImage()->height(); 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 = (int) qFloor(ach[l] * (255.0/pixels)); //color.setHsl(h, s, l); struct yuv yuv = LazyImage::RGBToYUV(color.rgb()); int l = qRound(yuv.y * 255.0); yuv.y = qFloor(ach[l] * (255.0/pixels)) / 255.0; color = LazyImage::YUVToRGB(yuv); working_copy->getImage()->setPixel(x, y, color.rgb()); } } updateImageDisplay(); logFile << "done." << std::endl; renewLogging(); } /** * Break the reference histogramm into parts, do linear adaption * * @brief ImageViewer::partialLinearHistogrammAdaption */ void ImageViewer::partialLinearHistogrammAdaption() { logFile << "Doing partial linear histogramm adaption ..." << std::endl; renewLogging(); // Get cumulative histogramm from reference image double* reference_histogramm = histogramm_reference->getRelativeCumulativeIntensityHistogramm(); // Split [0,255] into n parts (slider) // This produces a list of brightness indexes to interpolate in between. int number_parts = partial_adaption_pieces_slider->value(); int* parts = (int*) malloc((number_parts+1)*sizeof(int)); for(int i=0; i<=number_parts; i++) { parts[i] = qRound((255.0/number_parts)*i); logFile << "I: " << i << ", intensity: " << parts[i] << std::endl; } // Create histogramm map // This represents the new cumulative histogramm built of linear parts double* histogramm_map = (double*) malloc(256*sizeof(double)); for(int i=0; i<=number_parts; i++) { int current_n = parts[i]; if(current_n == 0) continue; // First part is always at zero! int last_n = parts[i-1]; int delta_n = current_n - last_n; double delta_i = reference_histogramm[current_n] - reference_histogramm[last_n]; double m = delta_i / delta_n; double offset = histogramm_map[last_n]; // Calculate the new cumulative values for(int j=last_n; j<=current_n; j++) { int delta_n = j - last_n; histogramm_map[j] = (offset + delta_n*m); logFile << j << " -> " << histogramm_map[j] << std::endl; } } double* original_histogramm = original->getRelativeCumulativeIntensityHistogramm(); // Build up the brightness map to apply on the image // (basically inversing the histogramm map) int* brightness_map = (int*) malloc(256*sizeof(int)); int j=0; for(int i=0; i<256; i++) { double old_cumulative_brightness = original_histogramm[i]; while(old_cumulative_brightness > histogramm_map[j]) j++; if(j > 255) j = 255; brightness_map[i] = j; } for(int i=0; i<256; i++) { logFile << i << " -> " << brightness_map[i] << std::endl; } renewLogging(); // Apply the brightness map //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 = brightness_map[l]; //color.setHsl(h, s, l); struct yuv yuv = LazyImage::RGBToYUV(color.rgb()); int l = qRound(yuv.y * 255.0); yuv.y = qFloor(brightness_map[l]) / 255.0; color = LazyImage::YUVToRGB(yuv); working_copy->getImage()->setPixel(x, y, color.rgb()); } } // Done. free(brightness_map); free(histogramm_map); free(parts); logFile << "done." << std::endl; renewLogging(); updateImageDisplay(); } /** * Do a succeeding histogramm adaption based on a reference image. * * @brief Imageviewer::succeedingHistogrammAdaption */ void ImageViewer::succeedingHistogrammAdaption() { logFile << "Doing succeeding 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 = original->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++; } // If there is no more space, just put it to the last value used. :/ // Otherwise, we don't know what to do with some pixels. if(source_histogramm[source_cursor] > (free_total_space - used_space)) { histogramm_map[source_cursor] = i; } } // 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); struct yuv yuv = LazyImage::RGBToYUV(color.rgb()); int l = qRound(yuv.y * 255.0); yuv.y = qFloor(histogramm_map[l]) / 255.0; color = LazyImage::YUVToRGB(yuv); working_copy->getImage()->setPixel(x, y, color.rgb()); } } // Done. } logFile << "done." << std::endl; renewLogging(); updateImageDisplay(); } /** * No special logic, avoid the borders and apply the filter. * * @brief ImageViewer::applyBasicFilter */ void ImageViewer::applyBasicFilter() { logFile << "Applying basic filter ..." << std::endl; renewLogging(); int* data = filter_gui->getData(); int filter_size = filter_gui->getSize(); // sum up the filter weights int filter_sum = 0; for(int i=0; i " << current_data << " "; } logFile << std::endl; } logFile << "Beginning calculation ... " << std::endl; renewLogging(); int h, s, l; // for each pixel int filter_offset = ((filter_size+1)/2); for(int x=0+filter_offset; xgetImage()->width()-filter_offset; x++) { for(int y=0+filter_offset; ygetImage()->height()-filter_offset; y++) { int intensity_sum = 0; // sum up weighted intensity for each matrix entry for(int fx=0; fxgetImage()->pixel(x+dx, y+dy)); color.getHsl(&h, &s, &l); //std::cout << "[" << fx << "," << fy <<"] " << l << " * " << data[fx*filter_size + fy] << " = "; intensity_sum += (l * data[fy*filter_size + fx]); //std::cout << intensity_sum << std::endl; } } QColor color = QColor::fromRgb(original->getImage()->pixel(x, y)); color.getHsl(&h, &s, &l); //std::cout << "old lightness: " << l << ", filter_sum: " << filter_sum << ", intensity_sum:" << intensity_sum; // divide by sum of weights l = qRound((intensity_sum*1.0) / filter_sum); //optional clamping for exotic stuff like negative values if(l > 255) l = 255; if(l < 0) l = 0; //std::cout << " --> new l: " << l << std::endl; color.setHsl(h, s, l); // and set it. working_copy->getImage()->setPixel(x, y, color.rgb()); } } logFile << "done." << std::endl; renewLogging(); updateImageDisplay(); } /** * Basic filter PLUS different overflow modes * * @brief ImageViewer::applyBasicFilterWithOverflowHandling */ void ImageViewer::applyBasicFilterWithOverflowHandling() { QString chosen_mode = filter_mode_combobox->currentText(); LazyImage::overflowMode overflow_mode = LazyImage::CONSTANT; // default for compiler if(chosen_mode == "Zero padding") overflow_mode = LazyImage::ZERO_PADDING; else if(chosen_mode == "Constant") overflow_mode = LazyImage::CONSTANT; else if(chosen_mode == "Continuous") overflow_mode = LazyImage::CONTINUOUS; else if(chosen_mode == "Mirrored") overflow_mode = LazyImage::MIRRORED; else { logFile << "Could not determine chosen mode, this is a problem!" << std::endl; } logFile << "Applying basic filter - overflow mode: " << chosen_mode.toStdString() << " -> " << overflow_mode << std::endl; renewLogging(); int* data = filter_gui->getData(); int filter_size = filter_gui->getSize(); // sum up the filter weights int filter_sum = 0; for(int i=0; i " << current_data << " "; } logFile << std::endl; } logFile << "Beginning calculation ... " << std::endl; renewLogging(); int h, s, l; // for each pixel for(int x=0; xgetImage()->width(); x++) { for(int y=0; ygetImage()->height(); y++) { int intensity_sum = 0; // sum up weighted intensity for each matrix entry for(int fx=0; fxgetPixel(x+dx, y+dy, overflow_mode)); color.getHsl(&h, &s, &l); //std::cout << "[" << fx << "," << fy <<"] " << l << " * " << data[fx*filter_size + fy] << " = "; intensity_sum += (l * data[fy*filter_size + fx]); //std::cout << intensity_sum << std::endl; } } QColor color = QColor::fromRgb(original->getImage()->pixel(x, y)); color.getHsl(&h, &s, &l); //std::cout << "old lightness: " << l << ", filter_sum: " << filter_sum << ", intensity_sum:" << intensity_sum; // divide by sum of weights l = qRound((intensity_sum*1.0) / filter_sum); //optional clamping for exotic stuff like negative values if(l > 255) l = 255; if(l < 0) l = 0; //std::cout << " --> new l: " << l << std::endl; color.setHsl(h, s, l); // and set it. working_copy->getImage()->setPixel(x, y, color.rgb()); } } logFile << "done." << std::endl; renewLogging(); updateImageDisplay(); } /** * Does the whole canny edge thing. * * @brief ImageViewer::runCannyEdge */ void ImageViewer::runCannyEdge(void) { //TODO CannyEdgeMachine cem = CannyEdgeMachine(original, working_copy); cem.setThresholdValues(2.5, 3.5); //TODO cem.work(); updateImageDisplay(); } /**************************************************************************************** * * GUI elements related to the tasks are set up here. * *****************************************************************************************/ void ImageViewer::changeFilterSize(int s) { filter_gui->setSize(((s+1)*2)+1); logFile << "done." << std::endl; renewLogging(); } void ImageViewer::generateControlPanels() { // Tab for first task task_tab1_widget = new QWidget(); task_tab1_vboxlayout = new QVBoxLayout(); task_tab1_widget->setLayout(task_tab1_vboxlayout); task_tab1_scrollwidget = new QWidget(); task_tab1_scrollarea = new QScrollArea(); task_tab1_scrollarea->setWidgetResizable(true); task_tab1_scrollarea->setWidget(task_tab1_scrollwidget); task_tab1_vboxlayout->addWidget(task_tab1_scrollarea); task_tab1_scrolllayout = new QVBoxLayout(); task_tab1_scrolllayout->setSizeConstraint(QLayout::SetMaximumSize); task_tab1_scrollwidget->setLayout(task_tab1_scrolllayout); monochrome = new QPushButton(); monochrome->setText("Convert to monochrome"); QObject::connect(monochrome, SIGNAL(clicked()), this, SLOT (convertToMonochrome())); reset_working_copy = new QPushButton(); reset_working_copy->setText("Reset working copy"); QObject::connect(reset_working_copy, SIGNAL(clicked()), this, SLOT (resetWorkingCopy())); save_to_original = new QPushButton(); save_to_original->setText("Save working copy to original"); QObject::connect(save_to_original, SIGNAL(clicked()), this, SLOT (saveToOriginal())); draw_black_line = new QPushButton(); draw_black_line->setText("Draw a black line"); QObject::connect(draw_black_line, SIGNAL(clicked()), this, SLOT (drawBlackLine())); draw_rainbow_cross = new QPushButton(); draw_rainbow_cross->setText("Draw a rainbow cross"); QObject::connect(draw_rainbow_cross, SIGNAL(clicked()), this, SLOT (drawRainbowCross())); diagonal_cross = new QPushButton(); diagonal_cross->setText("Draw a diagonal cross"); QObject::connect(diagonal_cross, SIGNAL(clicked()), this, SLOT (drawDiagonalCross())); line_slider = new QSlider(Qt::Horizontal); line_slider->setMinimum(1); line_slider->setMaximum(150); line_slider->setValue(3); //QObject::connect(lineSlider, SIGNAL(valueChanged(int)), this, SLOT (drawRainbowCross())); task_tab1_scrolllayout->addWidget(reset_working_copy); task_tab1_scrolllayout->addWidget(save_to_original); task_tab1_scrolllayout->addWidget(monochrome); task_tab1_scrolllayout->addWidget(new QLabel("Let's draw something!")); task_tab1_scrolllayout->addWidget(draw_black_line); task_tab1_scrolllayout->addWidget(draw_rainbow_cross); task_tab1_scrolllayout->addWidget(diagonal_cross); task_tab1_scrolllayout->addWidget(line_slider); task_tab1_scrolllayout->addWidget(new QLabel("Sets the width of the cross.")); tabWidget->addTab(task_tab1_widget, "Task #1"); // Tab for second task task_tab2_widget = new QWidget(); task_tab2_vboxlayout = new QVBoxLayout(); task_tab2_widget->setLayout(task_tab2_vboxlayout); task_tab2_scrollwidget = new QWidget(); task_tab2_scrollarea = new QScrollArea(); task_tab2_scrollarea->setWidgetResizable(true); task_tab2_scrollarea->setWidget(task_tab2_scrollwidget); task_tab2_vboxlayout->addWidget(task_tab2_scrollarea); task_tab2_scrolllayout = new QVBoxLayout(); task_tab2_scrolllayout->setSizeConstraint(QLayout::SetMaximumSize); task_tab2_scrollwidget->setLayout(task_tab2_scrolllayout); reference_histogramm_label = new QLabel(); reference_histogramm_label->setBackgroundRole(QPalette::Base); reference_histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); reference_histogramm_label->setScaledContents(false); reference_stats = new QLabel(); reference_stats->setText("Stats for reference image will be shown here."); original_histogramm_label = new QLabel(); original_histogramm_label->setBackgroundRole(QPalette::Base); original_histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); original_histogramm_label->setScaledContents(false); histogramm_label = new QLabel(); histogramm_label->setBackgroundRole(QPalette::Base); histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); histogramm_label->setScaledContents(false); original_stats = new QLabel(); original_stats->setText("Stats for original image will be shown here."); stats = new QLabel(); stats->setText("Stats for working copy will be shown here."); analyze_image = new QPushButton(); analyze_image->setText("Update image statistics, redraw histogramms."); QObject::connect(analyze_image, SIGNAL(clicked()), this, SLOT(analyzeImage())); brightness_slider = new QSlider(Qt::Horizontal); brightness_slider->setMinimum(0); brightness_slider->setMaximum(510); brightness_slider->setValue(255); QObject::connect(brightness_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustBrightness(int))); contrast_slider = new QSlider(Qt::Horizontal); contrast_slider->setMinimum(0); contrast_slider->setMaximum(510); 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(10); QObject::connect(contrast_percentile_slider, SIGNAL(valueChanged(int)), this, SLOT(robustAutomaticContrastAdaption(int))); task_tab2_scrolllayout->addWidget(new QLabel("Reference image")); task_tab2_scrolllayout->addWidget(reference_stats); task_tab2_scrolllayout->addWidget(reference_histogramm_label); task_tab2_scrolllayout->addWidget(new QLabel("Original image")); task_tab2_scrolllayout->addWidget(original_stats); task_tab2_scrolllayout->addWidget(original_histogramm_label); task_tab2_scrolllayout->addWidget(new QLabel("Working copy")); task_tab2_scrolllayout->addWidget(stats); task_tab2_scrolllayout->addWidget(histogramm_label); task_tab2_scrolllayout->addWidget(analyze_image); task_tab2_scrolllayout->addWidget(new QLabel("Brightness")); task_tab2_scrolllayout->addWidget(brightness_slider); task_tab2_scrolllayout->addWidget(new QLabel("Contrast")); task_tab2_scrolllayout->addWidget(contrast_slider); task_tab2_scrolllayout->addWidget(new QLabel("Automatic Contrast Percentile")); task_tab2_scrolllayout->addWidget(contrast_percentile_slider); tabWidget->addTab(task_tab2_widget, "Task #2"); // Tab for third task task_tab3_widget = new QWidget(); task_tab3_vboxlayout = new QVBoxLayout(); task_tab3_widget->setLayout(task_tab3_vboxlayout); task_tab3_scrollwidget = new QWidget(); task_tab3_scrollarea = new QScrollArea(); task_tab3_scrollarea->setWidgetResizable(true); task_tab3_scrollarea->setWidget(task_tab3_scrollwidget); task_tab3_vboxlayout->addWidget(task_tab3_scrollarea); task_tab3_scrolllayout = new QVBoxLayout(); task_tab3_scrolllayout->setSizeConstraint(QLayout::SetMaximumSize); task_tab3_scrollwidget->setLayout(task_tab3_scrolllayout); reference_histogramm_cumulative_label = new QLabel(); reference_histogramm_cumulative_label->setBackgroundRole(QPalette::Base); reference_histogramm_cumulative_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); reference_histogramm_cumulative_label->setScaledContents(false); original_histogramm_cumulative_label = new QLabel(); original_histogramm_cumulative_label->setBackgroundRole(QPalette::Base); original_histogramm_cumulative_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); original_histogramm_cumulative_label->setScaledContents(false); histogramm_cumulative_label = new QLabel(); histogramm_cumulative_label->setBackgroundRole(QPalette::Base); histogramm_cumulative_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); histogramm_cumulative_label->setScaledContents(false); linear_histogramm_adaption = new QPushButton("Do linear histogramm adaption (basic)"); QObject::connect(linear_histogramm_adaption, SIGNAL(clicked()), this, SLOT(linearHistogrammAdaption())); partial_adaption_pieces_slider = new QSlider(Qt::Horizontal); partial_adaption_pieces_slider->setMinimum(2); partial_adaption_pieces_slider->setMaximum(20); partial_adaption_pieces_slider->setValue(5); //QObject::connect(partial_adaption_pieces_slider, SIGNAL(valueChanged(int)), this, SLOT(robustAutomaticContrastAdaption(int))); partial_linear_histogramm_adaption = new QPushButton("Do partial, linear histogramm adaption"); QObject::connect(partial_linear_histogramm_adaption, SIGNAL(clicked()), this, SLOT(partialLinearHistogrammAdaption())); succeeding_histogramm_adaption = new QPushButton("Do succeeding histogramm adaption"); QObject::connect(succeeding_histogramm_adaption, SIGNAL(clicked()), this, SLOT(succeedingHistogrammAdaption())); task_tab3_scrolllayout->addWidget(new QLabel("Reference image")); task_tab3_scrolllayout->addWidget(reference_histogramm_cumulative_label); task_tab3_scrolllayout->addWidget(new QLabel("Original image")); task_tab3_scrolllayout->addWidget(original_histogramm_cumulative_label); task_tab3_scrolllayout->addWidget(new QLabel("Working copy")); task_tab3_scrolllayout->addWidget(histogramm_cumulative_label); task_tab3_scrolllayout->addWidget(linear_histogramm_adaption); task_tab3_scrolllayout->addWidget(partial_adaption_pieces_slider); task_tab3_scrolllayout->addWidget(partial_linear_histogramm_adaption); task_tab3_scrolllayout->addWidget(succeeding_histogramm_adaption); tabWidget->addTab(task_tab3_widget, "Task #3"); // Tab for fourth task task_tab4_widget = new QWidget(); task_tab4_vboxlayout = new QVBoxLayout(); task_tab4_widget->setLayout(task_tab4_vboxlayout); task_tab4_scrollwidget = new QWidget(); task_tab4_scrollarea = new QScrollArea(); task_tab4_scrollarea->setWidgetResizable(true); task_tab4_scrollarea->setWidget(task_tab4_scrollwidget); task_tab4_vboxlayout->addWidget(task_tab4_scrollarea); task_tab4_scrolllayout = new QVBoxLayout(); task_tab4_scrolllayout->setSizeConstraint(QLayout::SetMaximumSize); task_tab4_scrollwidget->setLayout(task_tab4_scrolllayout); filter_size_slider = new QSlider(Qt::Horizontal); filter_size_slider->setMinimum(0); filter_size_slider->setMaximum(4); filter_size_slider->setValue(0); QObject::connect(filter_size_slider, SIGNAL(valueChanged(int)), this, SLOT(changeFilterSize(int))); filter_gui = new FilterGui(); apply_filter = new QPushButton("Apply filter"); QObject::connect(apply_filter, SIGNAL(clicked()), this, SLOT(applyBasicFilter())); filter_mode_combobox = new QComboBox(); filter_mode_combobox->addItem("Zero padding"); filter_mode_combobox->addItem("Constant"); filter_mode_combobox->addItem("Mirrored"); filter_mode_combobox->addItem("Continuous"); apply_border_mode_filter = new QPushButton("Apply filter with overflow handling"); QObject::connect(apply_border_mode_filter, SIGNAL(clicked()), this, SLOT(applyBasicFilterWithOverflowHandling())); task_tab4_scrolllayout->addWidget(filter_size_slider); task_tab4_scrolllayout->addWidget(filter_gui->getWidget()); task_tab4_scrolllayout->addWidget(apply_filter); task_tab4_scrolllayout->addWidget(new QLabel("Choose border overflow mode:")); task_tab4_scrolllayout->addWidget(filter_mode_combobox); task_tab4_scrolllayout->addWidget(apply_border_mode_filter); tabWidget->addTab(task_tab4_widget, "Task #4"); // Tab for fifth task task_tab5_widget = new QWidget(); task_tab5_vboxlayout = new QVBoxLayout(); task_tab5_widget->setLayout(task_tab5_vboxlayout); task_tab5_scrollwidget = new QWidget(); task_tab5_scrollarea = new QScrollArea(); task_tab5_scrollarea->setWidgetResizable(true); task_tab5_scrollarea->setWidget(task_tab5_scrollwidget); task_tab5_vboxlayout->addWidget(task_tab5_scrollarea); task_tab5_scrolllayout = new QVBoxLayout(); task_tab5_scrolllayout->setSizeConstraint(QLayout::SetMaximumSize); task_tab5_scrollwidget->setLayout(task_tab5_scrolllayout); run_canny_edge = new QPushButton("Run canny edge algorithm!"); QObject::connect(run_canny_edge, SIGNAL(clicked()), this, SLOT(runCannyEdge())); task_tab5_scrolllayout->addWidget(run_canny_edge); tabWidget->addTab(task_tab5_widget, "Task #5"); //Show it tabWidget->show(); } /**************************************************************************************** * * The following methods are about the adjustBrightness(int)generic image viewer application and do not contain * any special graphics logic. * *****************************************************************************************/ void ImageViewer::startLogging() { //LogFile logFile.open("log.txt", std::ios::out); logFile << "Logging: \n" << std::endl; } void ImageViewer::renewLogging() { QFile file("log.txt"); // Create a file handle for the file named QString line; file.open(QIODevice::ReadOnly); // Open the file QTextStream stream(&file); // Set the stream to read from myFile logBrowser->clear(); while(!stream.atEnd()) { line = stream.readLine(); // this reads a line (QString) from the file logBrowser->append(line); } } void ImageViewer::updateImageDisplay() { imageLabel->setPixmap(QPixmap::fromImage(*(working_copy->getImage()))); } void ImageViewer::generateMainGui() { /* Tab widget */ tabWidget = new QTabWidget(this); tabWidget->setObjectName("tabWidget"); /* Center widget */ centralwidget = new QWidget(this); centralwidget->setObjectName("centralwidget"); //centralwidget->setFixedSize(200,200); //setCentralWidget(centralwidget); imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); /* Center widget */ scrollArea = new QScrollArea; scrollArea->setBackgroundRole(QPalette::Dark); scrollArea->setWidget(imageLabel); setCentralWidget(scrollArea); /* HBox layout */ QGridLayout* gLayout = new QGridLayout(centralwidget); gLayout->setObjectName("hboxLayout"); gLayout->addWidget(new QLabel(),1,1); gLayout->setVerticalSpacing(50); gLayout->addWidget(tabWidget,2,1); gLayout->addWidget(scrollArea,2,2); logBrowser= new QTextEdit(this); logBrowser->setMinimumHeight(100); logBrowser->setMaximumHeight(200); logBrowser->setMinimumWidth(width()); logBrowser->setMaximumWidth(width()); gLayout->addWidget(logBrowser,3,1,1,2); gLayout->setVerticalSpacing(50); } void ImageViewer::print() { Q_ASSERT(imageLabel->pixmap()); #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG) QPrintDialog dialog(&printer, this); if (dialog.exec()) { QPainter painter(&printer); QRect rect = painter.viewport(); QSize size = imageLabel->pixmap()->size(); size.scale(rect.size(), Qt::KeepAspectRatio); painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); painter.setWindow(imageLabel->pixmap()->rect()); painter.drawPixmap(0, 0, *imageLabel->pixmap()); } #endif } void ImageViewer::open() { if(working_copy != NULL) { delete working_copy; working_copy = NULL; } if(original != NULL) { delete original; original = NULL; } QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); if(!fileName.isEmpty()) { working_copy = new LazyImage(new QImage(fileName)); original = new LazyImage(new QImage(fileName)); if(working_copy->getImage()->isNull()) { QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName)); return; } scaleFactor = 1.0; updateImageDisplay(); printAct->setEnabled(true); fitToWindowAct->setEnabled(true); updateActions(); if(!fitToWindowAct->isChecked()) imageLabel->adjustSize(); setWindowFilePath(fileName); logFile << "geladen: " << fileName.toStdString().c_str() << std::endl; renewLogging(); initializeImage(); //Now hook to prepare certain datastructures after loading. } } /** * Simply load another image to provide reference for some operations. * @brief ImageViewer::openReference */ void ImageViewer::openReference() { if(histogramm_reference != NULL) { delete histogramm_reference; histogramm_reference = NULL; } QString fileName = QFileDialog::getOpenFileName(this, tr("Open Histogramm Reference File"), QDir::currentPath()); if(!fileName.isEmpty()) { histogramm_reference = new LazyImage(new QImage(fileName)); if(histogramm_reference->getImage()->isNull()) { QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName)); return; } if(histogramm_reference->getImage() != NULL) { histogramm_reference->updateStatistics(); QString result = QString("Intensity: Average: %1, Variance: %2").arg(histogramm_reference->getIntensityAverage()).arg(histogramm_reference->getIntensityVariance()); reference_stats->setText(result); reference_histogramm_label->setPixmap(QPixmap::fromImage(*(histogramm_reference->getHistogrammNormalImage()))); reference_histogramm_cumulative_label->setPixmap(QPixmap::fromImage(*(histogramm_reference->getHistogrammCumulativeImage()))); } logFile << "Referenzbild geladen: " << fileName.toStdString().c_str() << std::endl; renewLogging(); } } void ImageViewer::resizeEvent(QResizeEvent* event) { QMainWindow::resizeEvent(event); centralwidget->setMinimumWidth(width()); centralwidget->setMinimumHeight(height()); centralwidget->setMaximumWidth(width()); centralwidget->setMaximumHeight(height()); logBrowser->setMinimumWidth(width()-40); logBrowser->setMaximumWidth(width()-40); } void ImageViewer::zoomIn() { scaleImage(1.25); } void ImageViewer::zoomOut() { scaleImage(0.8); } void ImageViewer::normalSize() { imageLabel->adjustSize(); scaleFactor = 1.0; } void ImageViewer::fitToWindow() { bool fitToWindow = fitToWindowAct->isChecked(); scrollArea->setWidgetResizable(fitToWindow); if(!fitToWindow) { normalSize(); } updateActions(); } void ImageViewer::about() { QMessageBox::about(this, tr("About Image Viewer"), tr("

The Image Viewer example shows how to combine QLabel " "and QScrollArea to display an image. QLabel is typically used " "for displaying a text, but it can also display an image. " "QScrollArea provides a scrolling view around another widget. " "If the child widget exceeds the size of the frame, QScrollArea " "automatically provides scroll bars.

The example " "demonstrates how QLabel's ability to scale its contents " "(QLabel::scaledContents), and QScrollArea's ability to " "automatically resize its contents " "(QScrollArea::widgetResizable), can be used to implement " "zooming and scaling features.

In addition the example " "shows how to use QPainter to print an image.

")); } void ImageViewer::createActions() { openAct = new QAction(tr("&Open..."), this); openAct->setShortcut(tr("Ctrl+O")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); openHistAct = new QAction(tr("Open &Reference..."), this); openHistAct->setShortcut(tr("Ctrl+O")); connect(openHistAct, SIGNAL(triggered()), this, SLOT(openReference())); printAct = new QAction(tr("&Print..."), this); printAct->setShortcut(tr("Ctrl+P")); printAct->setEnabled(false); connect(printAct, SIGNAL(triggered()), this, SLOT(print())); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); zoomInAct = new QAction(tr("Zoom &In (25%)"), this); zoomInAct->setShortcut(tr("Ctrl++")); zoomInAct->setEnabled(false); connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn())); zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this); zoomOutAct->setShortcut(tr("Ctrl+-")); zoomOutAct->setEnabled(false); connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut())); normalSizeAct = new QAction(tr("&Normal Size"), this); normalSizeAct->setShortcut(tr("Ctrl+S")); normalSizeAct->setEnabled(false); connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize())); fitToWindowAct = new QAction(tr("&Fit to Window"), this); fitToWindowAct->setEnabled(false); fitToWindowAct->setCheckable(true); fitToWindowAct->setShortcut(tr("Ctrl+F")); connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow())); aboutAct = new QAction(tr("&About"), this); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); } void ImageViewer::createMenus() { fileMenu = new QMenu(tr("&File"), this); fileMenu->addAction(openAct); fileMenu->addAction(openHistAct); fileMenu->addAction(printAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); viewMenu = new QMenu(tr("&View"), this); viewMenu->addAction(zoomInAct); viewMenu->addAction(zoomOutAct); viewMenu->addAction(normalSizeAct); viewMenu->addSeparator(); viewMenu->addAction(fitToWindowAct); helpMenu = new QMenu(tr("&Help"), this); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct); menuBar()->addMenu(fileMenu); menuBar()->addMenu(viewMenu); menuBar()->addMenu(helpMenu); } void ImageViewer::updateActions() { zoomInAct->setEnabled(!fitToWindowAct->isChecked()); zoomOutAct->setEnabled(!fitToWindowAct->isChecked()); normalSizeAct->setEnabled(!fitToWindowAct->isChecked()); } void ImageViewer::scaleImage(double factor) { Q_ASSERT(imageLabel->pixmap()); scaleFactor *= factor; imageLabel->resize(scaleFactor * imageLabel->pixmap()->size()); adjustScrollBar(scrollArea->horizontalScrollBar(), factor); adjustScrollBar(scrollArea->verticalScrollBar(), factor); zoomInAct->setEnabled(scaleFactor < 3.0); zoomOutAct->setEnabled(scaleFactor > 0.333); } void ImageViewer::adjustScrollBar(QScrollBar* scrollBar, double factor) { scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2))); }