From 94475095cf4dd2aadd849840ea5075f96ac8c4a0 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 5 Jan 2016 18:59:08 +0100 Subject: [PATCH] [TASK] Finish up unsharp masking. --- imageviewer-qt4.cpp | 50 +++++++++++++++++++++++++++++++++++++++------ imageviewer-qt4.h | 3 ++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index 7990bde..007f6ce 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -724,11 +724,44 @@ void ImageViewer::runCannyEdgeThirdStep(void) { void ImageViewer::doUnsharpMasking(void) { double alpha = usm_sharpening->value(); int gauss_width = usm_gauss_width->value(); - logFile << "Doing USM using gauss width of " << gauss_width << " and alpha of " << alpha << std::endl; + double sigma = usm_gauss_sigma->value(); + logFile << "Doing USM using gauss width of " << gauss_width << ", sigma of " << sigma << ", and alpha of " << alpha << std::endl; renewLogging(); - - - + int pixels = original->width() * original->height(); + int* blurred_intensity = (int*) malloc(sizeof(int) * pixels); + int* mask = (int*) malloc(sizeof(int) * pixels); + double sum_weights = 0.0; + double* filter = canny_edge_machine->generateGaussFilter(gauss_width, sigma, sum_weights); + int h, s, l; + for(int x=0; xwidth(); x++) { + for(int y=0; yheight(); y++) { + // Step 1: Blur the image and store seperately + double sum_intensity = 0.0; + for(int fx=0; fxgetPixel(x+dx, y+dy, LazyImage::CONSTANT)); + color.getHsl(&h, &s, &l); + sum_intensity += l; + } + } + double average_intensity = sum_intensity / sum_weights; + // Step 2: Subtract blurred image from original -> store as mask + QColor color = QColor::fromRgb(original->getPixel(x, y, LazyImage::CONSTANT)); + color.getHsl(&h, &s, &l); + double intensity_mask = l - average_intensity; + // Step 3: Add weighted mask to original -> sharpening occurs. + double intensity_result = alpha * intensity_mask + l; + if(intensity_result > 255) intensity_result = 255; + if(intensity_result < 0) intensity_result = 0; + color.setHsl(h, s, qRound(intensity_result)); + working_copy->getImage()->setPixel(x, y, color.rgb()); + } + } + free(filter); + free(mask); + free(blurred_intensity); updateImageDisplay(); } /**************************************************************************************** @@ -1026,8 +1059,12 @@ void ImageViewer::generateControlPanels() { usm_sharpening->setValue(1.0); usm_gauss_width = new QDoubleSpinBox(); - usm_gauss_width->setDecimals(5); + usm_gauss_width->setDecimals(0); usm_gauss_width->setValue(3); + + usm_gauss_sigma = new QDoubleSpinBox(); + usm_gauss_sigma->setDecimals(5); + usm_gauss_sigma->setValue(4); run_usm = new QPushButton("Apply unsharp masking!"); QObject::connect(run_usm, SIGNAL(clicked()), this, SLOT(doUnsharpMasking())); @@ -1047,8 +1084,9 @@ void ImageViewer::generateControlPanels() { task_tab5_scrolllayout->addWidget(new QLabel(" --- Unsharp masking --- ")); task_tab5_scrolllayout->addWidget(new QLabel("Sharpening factor alpha")); task_tab5_scrolllayout->addWidget(usm_sharpening); - task_tab5_scrolllayout->addWidget(new QLabel("Gauss filter width sigma")); + task_tab5_scrolllayout->addWidget(new QLabel("Gauss filter width and sigma")); task_tab5_scrolllayout->addWidget(usm_gauss_width); + task_tab5_scrolllayout->addWidget(usm_gauss_sigma); task_tab5_scrolllayout->addWidget(run_usm); tabWidget->addTab(task_tab5_widget, "Task #5"); diff --git a/imageviewer-qt4.h b/imageviewer-qt4.h index 0ac5226..25c151f 100644 --- a/imageviewer-qt4.h +++ b/imageviewer-qt4.h @@ -156,8 +156,9 @@ class ImageViewer : public QMainWindow { QPushButton* run_canny_edge_second_step; QPushButton* run_canny_edge_third_step; QPushButton* run_usm; - QDoubleSpinBox* usm_sharpening; + QDoubleSpinBox* usm_sharpening; QDoubleSpinBox* usm_gauss_width; + QDoubleSpinBox* usm_gauss_sigma; // "My" space for storing data/results LazyImage* original;