implemented clamp and convolute

This commit is contained in:
Damian Kowalski 2024-04-07 14:09:31 +02:00
parent a36a7c115c
commit f7dea21375
2 changed files with 41 additions and 1 deletions

View File

@ -1,5 +1,7 @@
#include "convolution.h"
#include <algorithm>
/** Overloaded constructor */
Convolution::Convolution(PNM* img) :
Transformation(img)
@ -39,6 +41,10 @@ math::matrix<float> Convolution::getMask(int size, Mode mode = Normalize)
return mask;
}
int Convolution::clamp(int value, int min, int max) {
return (value < min) ? min : (value > max) ? max : value;
}
/** Does the convolution process for all pixels using the given mask. */
PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge)
{
@ -47,7 +53,40 @@ PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge)
PNM* newImage = new PNM(width, height, image->format());
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
// Calcularing the mask weight
float weight = Convolution::sum(mask);
// Going through with 'the mask' with all the pixels
for (int x=0; x < width ; x++)
for (int y=0; y < height; y++)
{
math::matrix<float> reflection = Convolution::reflection(mask);
// Calculate accumulator
math::matrix<float> redAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::RChannel, mode), reflection);
math::matrix<float> greenAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::GChannel, mode), reflection);
math::matrix<float> blueAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::BChannel, mode), reflection);
// Calculate accumulator sum
float redAccSum = Convolution::sum(redAcc);
float greenAccSum = Convolution::sum(greenAcc);
float blueAccSum = Convolution::sum(blueAcc);
if (weight != 0)
{
redAccSum = redAccSum / weight;
greenAccSum = greenAccSum / weight;
blueAccSum = blueAccSum / weight;
}
redAccSum = clamp(redAccSum, 0, 255);
greenAccSum = clamp(greenAccSum, 0, 255);
blueAccSum = clamp(blueAccSum, 0, 255);
// Set new pixel value
QColor newPixel = QColor(redAccSum, greenAccSum, blueAccSum);
newImage->setPixel(x,y, newPixel.rgb());
}
return newImage;
}

View File

@ -15,6 +15,7 @@ public:
virtual PNM* transform();
protected:
int clamp(int value, int min, int max);
const math::matrix<float> join(math::matrix<float>, math::matrix<float>);
const float sum(math::matrix<float>);
const math::matrix<float> reflection(math::matrix<float>);