From b801fbda96299f719cb3fe4ff7c141e61a606476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Wieczorek?= Date: Thu, 15 Apr 2021 20:32:58 +0200 Subject: [PATCH] Fix bin_gradient.cpp --- .../src/core/transformations/bin_gradient.cpp | 75 +++++++++++-------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/app/cpp/mysimplegimp/src/core/transformations/bin_gradient.cpp b/app/cpp/mysimplegimp/src/core/transformations/bin_gradient.cpp index a495bc7..d07edb2 100644 --- a/app/cpp/mysimplegimp/src/core/transformations/bin_gradient.cpp +++ b/app/cpp/mysimplegimp/src/core/transformations/bin_gradient.cpp @@ -17,51 +17,62 @@ PNM* BinarizationGradient::transform() int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); + int numerator = 0; + int denominator = 0; - // Create variables - // The pixel value at point (i,j) - float I=0; + // Gx + int Gx = 0; - // The first component of the gradient at point (i,j) - float Gx=0; + // Gy + int Gy = 0; - // The second component of the gradient at point (i,j) - float Gy=0; + int G = 0; + int T = 0; + //PNM* newImage = new PNM(width, height, QImage::Format_Mono); - for (int x=0; x < width; x++) + for (int x = 0; x < width; x++) { - for(int y=0; y < height; y++) + for (int y = 0; y < height; y++) { - // Get current pixel value - QRgb pixel = image->pixel(x, y); - I = qGray(pixel); + Mode mode = Transformation::RepeatEdge; + int I = qGray(getPixel(x, y, mode)); - // Calculate Gx - Gx = Gx + (I * qGray(image->pixel(x+1, y)) - I * qGray(image->pixel(x-1, y))); + // Get Gx and Gy + Gx = qGray(getPixel(x + 1, y, mode)) - qGray(getPixel(x - 1, y, mode)); + Gy = qGray(getPixel(x, y + 1, mode)) - qGray(getPixel(x, y - 1, mode)); - //Calculate Gy - Gy = Gy + (I * qGray(image->pixel(x, y+1)) - I * qGray(image->pixel(x, y-1))); - } - } - - // Calculate T - float T = (max(Gx, Gy) * I) / max(Gx, Gy); - - // Iterate over pixels - for (int x=0; x < width; x++) - { - for (int y=0; y < height; y++) - { - // Get current pixel - QRgb pixel = image->pixel(x, y); - if(qGray(pixel) > T) + if (Gx > Gy) { - newImage->setPixel(x, y, Qt::color1); + G = Gx; } else { - newImage->setPixel(x, y, Qt::color0); + G = Gy; + } + numerator += I * G; + denominator += G; + } + } + + T = numerator / denominator; + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + Mode mode = Transformation::RepeatEdge; + QRgb pixel = getPixel(x, y, mode); + + int val = qGray(pixel); + + if (val < T) + { + newImage->setPixel(x, y, 0); + } + else + { + newImage->setPixel(x, y, 1); } } }