Fix bin_gradient.cpp
This commit is contained in:
parent
e657afd5ae
commit
b801fbda96
@ -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 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++)
|
||||
if (Gx > Gy)
|
||||
{
|
||||
for (int y=0; y < height; y++)
|
||||
{
|
||||
// Get current pixel
|
||||
QRgb pixel = image->pixel(x, y);
|
||||
if(qGray(pixel) > T)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user