1
0

Zadanie 8

This commit is contained in:
Jarosław Wieczorek 2021-04-06 13:34:01 +02:00
parent 094dcbc4a3
commit 90fb90b2a8
23 changed files with 522 additions and 15 deletions

View File

@ -0,0 +1,105 @@
#include "edge_gradient.h"
EdgeGradient::EdgeGradient(PNM* img, ImageViewer* iv) :
Convolution(img, iv)
{
}
EdgeGradient::EdgeGradient(PNM* img) :
Convolution(img)
{
}
PNM* EdgeGradient::verticalDetection()
{
return convolute(g_y, RepeatEdge);
}
PNM* EdgeGradient::horizontalDetection()
{
return convolute(g_x, RepeatEdge);
}
int EdgeGradient::calcValue(int i, int j, Channel channel, PNM* img_x, PNM* img_y)
{
int value_x, value_y;
double power_x, power_y;
int value_xy;
int result;
QRgb pixel_x = img_x->pixel(i, j);
QRgb pixel_y = img_y->pixel(i, j);
switch(channel)
{
case RChannel:
value_x = qRed(pixel_x);
value_y = qRed(pixel_y);
break;
case GChannel:
value_x = qGreen(pixel_x);
value_y = qGreen(pixel_y);
break;
case BChannel:
value_x = qBlue(pixel_x);
value_y = qBlue(pixel_y);
break;
case LChannel:
value_x = qGray(pixel_x);
value_y = qGray(pixel_y);
break;
}
power_x = pow(value_x, 2);
power_y = pow(value_y, 2);
value_xy = (int) sqrt(power_x + power_y);
result = std::max(0, std::min(255, value_xy));
return result;
}
PNM* EdgeGradient::transform()
{
PNM* newImage = new PNM(image->width(), image->height(), image->format());
int width = image->width();
int height = image->height();
// Gradient image by x
PNM* img_x = horizontalDetection();
// Gradient image by y
PNM* img_y = verticalDetection();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (image->format() == QImage::Format_Indexed8)
{
// Calculate gray pixel value
int l = calcValue(i, j, LChannel, img_x, img_y);
newImage->setPixel(i, j, l);
}
else
{
// Calculate RGB values for pixel
int r = calcValue(i, j, RChannel, img_x, img_y);
int g = calcValue(i, j, GChannel, img_x, img_y);
int b = calcValue(i, j, BChannel, img_x, img_y);
QColor color = QColor(r, g, b);
newImage->setPixel(i, j, color.rgb());
}
}
}
return newImage;
}

View File

@ -0,0 +1,24 @@
#ifndef EDGE_GRADIENT_H
#define EDGE_GRADIENT_H
#include "convolution.h"
class EdgeGradient : public Convolution
{
public:
EdgeGradient(PNM*);
EdgeGradient(PNM*, ImageViewer*);
virtual PNM* transform();
PNM* verticalDetection();
PNM* horizontalDetection();
int calcValue(int i, int j, Channel channel, PNM* img_x, PNM* img_y);
protected:
virtual void prepareMatrices() = 0;
math::matrix<float> g_x,
g_y;
};
#endif // EDGE_GRADIENT_H

View File

@ -0,0 +1,33 @@
#include "edge_laplacian.h"
EdgeLaplacian::EdgeLaplacian(PNM* img) :
Convolution(img)
{
}
EdgeLaplacian::EdgeLaplacian(PNM* img, ImageViewer* iv) :
Convolution(img, iv)
{
}
math::matrix<float> EdgeLaplacian::getMask(int, Mode)
{
int size = getParameter("size").toInt();
math::matrix<float> mask(size, size);
int center = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
mask(i, j) = -1;
}
}
// Calculate center element of mask
mask(center, center) = pow(size, 2) - 1;
return mask;
}

View File

@ -0,0 +1,55 @@
#include "edge_laplacian_of_gauss.h"
#include "blur_gaussian.h"
EdgeLaplaceOfGauss::EdgeLaplaceOfGauss(PNM* img) :
Convolution(img)
{
}
EdgeLaplaceOfGauss::EdgeLaplaceOfGauss(PNM* img, ImageViewer* iv) :
Convolution(img, iv)
{
}
math::matrix<float> EdgeLaplaceOfGauss::getMask()
{
size = getParameter("size").toInt();
double sigma = getParameter("sigma").toDouble();
math::matrix<float> mask(size, size);
int center = -size / 2;
for (int x = 0, k = center; x < size; x++, k++)
{
for (int y = 0, l = center; y < size; y++, l++)
{
mask(x, y) = getLoG(k, l, sigma);
}
}
return mask;
}
float EdgeLaplaceOfGauss::getLoG(int x, int y, float s)
{
int px = pow(x, 2);
int py = pow(y, 2);
float ps = pow(s, 2);
int numerator = px + py - 2;
int gauss = BlurGaussian::getGauss(x, y, ps);
int result = (numerator / ps) * gauss;
return result;
}
int EdgeLaplaceOfGauss::getSize()
{
return size;
}

View File

@ -0,0 +1,21 @@
#ifndef EDGE_LAPLACE_GAUSS_H
#define EDGE_LAPLACE_GAUSS_H
#include "convolution.h"
class EdgeLaplaceOfGauss : public Convolution
{
public:
EdgeLaplaceOfGauss(PNM*);
EdgeLaplaceOfGauss(PNM*, ImageViewer*);
virtual math::matrix<float> getMask();
static float getLoG(int, int, float);
int getSize();
private:
int size;
};
#endif // EDGE_LAPLACE_GAUSS_H

View File

@ -0,0 +1,19 @@
#include "edge_prewitt.h"
EdgePrewitt::EdgePrewitt(PNM*img) :
EdgeGradient(img)
{
prepareMatrices();
}
EdgePrewitt::EdgePrewitt(PNM*img, ImageViewer* iv) :
EdgeGradient(img, iv)
{
prepareMatrices();
}
void EdgePrewitt::prepareMatrices()
{
g_x = math::matrix<float>(3, 3, {-1, 0, 1, -1, 0, 1, -1, 0, 1});
g_y = math::matrix<float>(3, 3, {-1, -1, -1, 0, 0, 0, 1, 1, 1});
}

View File

@ -0,0 +1,19 @@
#include "edge_roberts.h"
EdgeRoberts::EdgeRoberts(PNM* img) :
EdgeGradient(img)
{
prepareMatrices();
}
EdgeRoberts::EdgeRoberts(PNM* img, ImageViewer* iv) :
EdgeGradient(img, iv)
{
prepareMatrices();
}
void EdgeRoberts::prepareMatrices()
{
g_x = math::matrix<float>(2, 2, {1, 0, 0, -1});
g_y = math::matrix<float>(2, 2, {0, 1, -1, 0});
}

View File

@ -0,0 +1,37 @@
#include "edge_sobel.h"
EdgeSobel::EdgeSobel(PNM* img, ImageViewer* iv) :
EdgeGradient(img, iv)
{
prepareMatrices();
}
EdgeSobel::EdgeSobel(PNM* img) :
EdgeGradient(img)
{
prepareMatrices();
}
void EdgeSobel::prepareMatrices()
{
g_x = math::matrix<float>(3, 3, {-1, 0, 1, -2, 0, 2, -1, 0, 1});
g_y = math::matrix<float>(3, 3, {-1, -2, -1, 0, 0, 0, 1, 2, 1});
}
math::matrix<float>* EdgeSobel::rawHorizontalDetection()
{
math::matrix<float>* x_gradient = new math::matrix<float>(this->image->width(), this->image->height());
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
return x_gradient;
}
math::matrix<float>* EdgeSobel::rawVerticalDetection()
{
math::matrix<float>* y_gradient = new math::matrix<float>(this->image->width(), this->image->height());
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
return y_gradient;
}

View File

@ -0,0 +1,59 @@
#include "edge_zero.h"
#include "edge_laplacian_of_gauss.h"
EdgeZeroCrossing::EdgeZeroCrossing(PNM* img) :
Convolution(img)
{
}
EdgeZeroCrossing::EdgeZeroCrossing(PNM* img, ImageViewer* iv) :
Convolution(img, iv)
{
}
PNM* EdgeZeroCrossing::transform()
{
int size = getParameter("size").toInt();
double sigma = getParameter("sigma").toDouble();
int t = getParameter("threshold").toInt();
int width = image->width();
int height = image->height();
int t_width;
int t_height;
PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8);
EdgeLaplaceOfGauss *laplace_gauss = new EdgeLaplaceOfGauss(image);
laplace_gauss->setParameter("size", size);
laplace_gauss->setParameter("sigma", sigma);
PNM* transformed_image = laplace_gauss->transform();
t_width = transformed_image->width();
t_height = transformed_image->height();
for (int i = 0; i < t_width; i++)
{
for (int j = 0; j < t_height; j++)
{
math::matrix<float> window = laplace_gauss->getWindow(i, j, size, LChannel, RepeatEdge);
if (window.min() < (128 - t) && window.max() > (128 + t))
{
QRgb pixel = transformed_image->pixel(i, j);
newImage->setPixel(i, j, QColor(pixel).rgb());
}
else
{
newImage->setPixel(i, j, QColor(0,0,0).rgb());
}
}
}
return newImage;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 510 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 576 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 615 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

View File

@ -20,12 +20,86 @@ PNM* EdgeGradient::horizontalDetection()
return convolute(g_x, RepeatEdge); return convolute(g_x, RepeatEdge);
} }
int EdgeGradient::calcValue(int i, int j, Channel channel, PNM* img_x, PNM* img_y)
{
int value_x, value_y;
double power_x, power_y;
int value_xy;
int result;
QRgb pixel_x = img_x->pixel(i, j);
QRgb pixel_y = img_y->pixel(i, j);
switch(channel)
{
case RChannel:
value_x = qRed(pixel_x);
value_y = qRed(pixel_y);
break;
case GChannel:
value_x = qGreen(pixel_x);
value_y = qGreen(pixel_y);
break;
case BChannel:
value_x = qBlue(pixel_x);
value_y = qBlue(pixel_y);
break;
case LChannel:
value_x = qGray(pixel_x);
value_y = qGray(pixel_y);
break;
}
power_x = pow(value_x, 2);
power_y = pow(value_y, 2);
value_xy = (int) sqrt(power_x + power_y);
result = std::max(0, std::min(255, value_xy));
return result;
}
PNM* EdgeGradient::transform() PNM* EdgeGradient::transform()
{ {
PNM* newImage = new PNM(image->width(), image->height(), image->format()); PNM* newImage = new PNM(image->width(), image->height(), image->format());
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int width = image->width();
int height = image->height();
// Gradient image by x
PNM* img_x = horizontalDetection();
// Gradient image by y
PNM* img_y = verticalDetection();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (image->format() == QImage::Format_Indexed8)
{
// Calculate gray pixel value
int l = calcValue(i, j, LChannel, img_x, img_y);
newImage->setPixel(i, j, l);
}
else
{
// Calculate RGB values for pixel
int r = calcValue(i, j, RChannel, img_x, img_y);
int g = calcValue(i, j, GChannel, img_x, img_y);
int b = calcValue(i, j, BChannel, img_x, img_y);
QColor color = QColor(r, g, b);
newImage->setPixel(i, j, color.rgb());
}
}
}
return newImage; return newImage;
} }

View File

@ -13,6 +13,7 @@ public:
PNM* verticalDetection(); PNM* verticalDetection();
PNM* horizontalDetection(); PNM* horizontalDetection();
int calcValue(int i, int j, Channel channel, PNM* img_x, PNM* img_y);
protected: protected:
virtual void prepareMatrices() = 0; virtual void prepareMatrices() = 0;

View File

@ -15,7 +15,18 @@ math::matrix<float> EdgeLaplacian::getMask(int, Mode)
int size = getParameter("size").toInt(); int size = getParameter("size").toInt();
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int center = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
mask(i, j) = -1;
}
}
// Calculate center element of mask
mask(center, center) = pow(size, 2) - 1;
return mask; return mask;
} }

View File

@ -12,25 +12,43 @@ EdgeLaplaceOfGauss::EdgeLaplaceOfGauss(PNM* img, ImageViewer* iv) :
{ {
} }
math::matrix<float> EdgeLaplaceOfGauss::getMask(int, Mode) math::matrix<float> EdgeLaplaceOfGauss::getMask()
{ {
size = getParameter("size").toInt(); size = getParameter("size").toInt();
double sigma = getParameter("sigma").toDouble(); double sigma = getParameter("sigma").toDouble();
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int center = -size / 2;
for (int x = 0, k = center; x < size; x++, k++)
{
for (int y = 0, l = center; y < size; y++, l++)
{
mask(x, y) = getLoG(k, l, sigma);
}
}
return mask; return mask;
} }
float EdgeLaplaceOfGauss::getLoG(int x, int y, float s) float EdgeLaplaceOfGauss::getLoG(int x, int y, float s)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int px = pow(x, 2);
int py = pow(y, 2);
return 0; float ps = pow(s, 2);
int numerator = px + py - 2;
int gauss = BlurGaussian::getGauss(x, y, ps);
int result = (numerator / ps) * gauss;
return result;
} }
int EdgeLaplaceOfGauss::getSize() int EdgeLaplaceOfGauss::getSize()
{ {
return size; return size;

View File

@ -9,7 +9,7 @@ public:
EdgeLaplaceOfGauss(PNM*); EdgeLaplaceOfGauss(PNM*);
EdgeLaplaceOfGauss(PNM*, ImageViewer*); EdgeLaplaceOfGauss(PNM*, ImageViewer*);
virtual math::matrix<float> getMask(int, Mode); virtual math::matrix<float> getMask();
static float getLoG(int, int, float); static float getLoG(int, int, float);
int getSize(); int getSize();

View File

@ -14,6 +14,6 @@ EdgePrewitt::EdgePrewitt(PNM*img, ImageViewer* iv) :
void EdgePrewitt::prepareMatrices() void EdgePrewitt::prepareMatrices()
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; g_x = math::matrix<float>(3, 3, {-1, 0, 1, -1, 0, 1, -1, 0, 1});
g_y = math::matrix<float>(3, 3, {-1, -1, -1, 0, 0, 0, 1, 1, 1});
} }

View File

@ -14,5 +14,6 @@ EdgeRoberts::EdgeRoberts(PNM* img, ImageViewer* iv) :
void EdgeRoberts::prepareMatrices() void EdgeRoberts::prepareMatrices()
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; g_x = math::matrix<float>(2, 2, {1, 0, 0, -1});
g_y = math::matrix<float>(2, 2, {0, 1, -1, 0});
} }

View File

@ -14,7 +14,8 @@ EdgeSobel::EdgeSobel(PNM* img) :
void EdgeSobel::prepareMatrices() void EdgeSobel::prepareMatrices()
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; g_x = math::matrix<float>(3, 3, {-1, 0, 1, -2, 0, 2, -1, 0, 1});
g_y = math::matrix<float>(3, 3, {-1, -2, -1, 0, 0, 0, 1, 2, 1});
} }
math::matrix<float>* EdgeSobel::rawHorizontalDetection() math::matrix<float>* EdgeSobel::rawHorizontalDetection()

View File

@ -14,16 +14,45 @@ EdgeZeroCrossing::EdgeZeroCrossing(PNM* img, ImageViewer* iv) :
PNM* EdgeZeroCrossing::transform() PNM* EdgeZeroCrossing::transform()
{ {
int width = image->width(),
height = image->height();
int size = getParameter("size").toInt(); int size = getParameter("size").toInt();
double sigma = getParameter("sigma").toDouble(); double sigma = getParameter("sigma").toDouble();
int t = getParameter("threshold").toInt(); int t = getParameter("threshold").toInt();
int width = image->width();
int height = image->height();
int t_width;
int t_height;
PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8); PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; EdgeLaplaceOfGauss *laplace_gauss = new EdgeLaplaceOfGauss(image);
laplace_gauss->setParameter("size", size);
laplace_gauss->setParameter("sigma", sigma);
PNM* transformed_image = laplace_gauss->transform();
t_width = transformed_image->width();
t_height = transformed_image->height();
for (int i = 0; i < t_width; i++)
{
for (int j = 0; j < t_height; j++)
{
math::matrix<float> window = laplace_gauss->getWindow(i, j, size, LChannel, RepeatEdge);
if (window.min() < (128 - t) && window.max() > (128 + t))
{
QRgb pixel = transformed_image->pixel(i, j);
newImage->setPixel(i, j, QColor(pixel).rgb());
}
else
{
newImage->setPixel(i, j, QColor(0,0,0).rgb());
}
}
}
return newImage; return newImage;
} }