[TASK] Finish up unsharp masking.

This commit is contained in:
Jan Philipp Timme 2016-01-05 18:59:08 +01:00
parent 3ce2be6e3d
commit 94475095cf
2 changed files with 46 additions and 7 deletions

View File

@ -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; x<original->width(); x++) {
for(int y=0; y<original->height(); y++) {
// Step 1: Blur the image and store seperately
double sum_intensity = 0.0;
for(int fx=0; fx<gauss_width; fx++) {
int dx = fx - (gauss_width/2);
for(int fy=0; fy<gauss_width; fy++) {
int dy = fy - (gauss_width/2);
QColor color = QColor::fromRgb(original->getPixel(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");

View File

@ -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;