zadanie - 10 (odszumianie)

This commit is contained in:
Patrycjusz Mania 2021-06-16 20:54:14 +02:00
parent cf79d13cc8
commit bee9d1f89c
2 changed files with 59 additions and 12 deletions

View File

@ -17,32 +17,59 @@ PNM* NoiseBilateral::transform()
PNM* newImage = new PNM(width, height, image->format()); PNM* newImage = new PNM(width, height, image->format());
sigma_d = getParameter("sigma_d").toInt(); sigma_d = getParameter("sigma_d").toInt() * 1.8;
sigma_r = getParameter("sigma_r").toInt(); sigma_r = getParameter("sigma_r").toInt();
radius = sigma_d; radius = sigma_d;
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
int r = calcVal(x,y, RChannel);
int g = calcVal(x,y, GChannel);
int b = calcVal(x,y, BChannel);
newImage->setPixel(x, y, QColor(r,g,b).rgb());
}
}
return newImage; return newImage;
} }
int NoiseBilateral::calcVal(int x, int y, Channel channel) int NoiseBilateral::calcVal(int x, int y, Channel channel)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; math::matrix<float> window = getWindow(x, y, radius, channel, RepeatEdge);
return 0; float sumCounter = 0;
float sumDenominator = 0;
int pixel = image->pixel(x,y);
switch (channel) {
case RChannel: pixel = qRed(pixel);
case GChannel: pixel = qGreen(pixel);
case BChannel: pixel = qBlue(pixel);
}
for (int i=0;i<radius;++i) {
for (int j=0;j<radius;++j) {
int pixelVal = window[i][j];
QPoint point((i - radius/2) + x, (j - radius/2) + y);
QPoint point2(x,y);
sumCounter += pixelVal * colorCloseness(pixelVal, pixel) * spatialCloseness(point, point2);
sumDenominator += colorCloseness(pixelVal, pixel) * spatialCloseness(point, point2);
}
}
return sumDenominator != 0 ? sumCounter / sumDenominator : 0;
} }
float NoiseBilateral::colorCloseness(int val1, int val2) float NoiseBilateral::colorCloseness(int val1, int val2)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; float counter = pow(val1 - val2, 2);
float denominator = 2 * pow(sigma_r, 2);
return 0; return exp(-(counter / denominator));
} }
float NoiseBilateral::spatialCloseness(QPoint point1, QPoint point2) float NoiseBilateral::spatialCloseness(QPoint point1, QPoint point2)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; float X = pow(point1.x() - point2.x(), 2);
float Y = pow(point1.y() - point2.y(), 2);
float denominator = 2 * pow(sigma_d, 2);
return 0; return exp(-(X + Y) / denominator);
} }

View File

@ -17,16 +17,36 @@ PNM* NoiseMedian::transform()
PNM* newImage = new PNM(width, height, image->format()); PNM* newImage = new PNM(width, height, image->format());
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
int r = getMedian(x,y, RChannel);
int g = getMedian(x,y, GChannel);
int b = getMedian(x,y, BChannel);
newImage->setPixel(x, y, QColor(r,g,b).rgb());
}
}
return newImage; return newImage;
} }
int NoiseMedian::getMedian(int x, int y, Channel channel) int NoiseMedian::getMedian(int x, int y, Channel channel)
{ {
int radius = getParameter("radius").toInt(); int radius = getParameter("radius").toInt() * 2 + 1;
math::matrix<float> window = getWindow(x, y, radius, channel, RepeatEdge);
int items = radius * radius;
int tmp[items];
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int i=0;
for (int x=0;x<radius;++x) {
for (int y=0;y<radius;++y) {
tmp[i++] = window[x][y];
}
}
return 0; std::sort(tmp, tmp + (items - 1));
if (items % 2 != 0)
return tmp[items / 2];
return (tmp[(items - 1) / 2] + tmp[items / 2]) / 2.0;
} }