zadania 4(done) i 5(in progress)

This commit is contained in:
BohdanBakhlul 2021-05-16 09:22:17 +02:00
parent 9dc5eeed6b
commit 701ce8d44b
10 changed files with 170 additions and 57 deletions

View File

@ -24,16 +24,18 @@ PNM* BlurGaussian::transform()
math::matrix<float> BlurGaussian::getMask(int size, Mode) math::matrix<float> BlurGaussian::getMask(int size, Mode)
{ {
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
for (int x=0;x<size;++x) {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int y=0;y<size;++y) {
mask[x][y] = getGauss(x - int(size / 2), y - int(size / 2), sigma);
}
}
return mask; return mask;
} }
float BlurGaussian::getGauss(int x, int y, float sigma) float BlurGaussian::getGauss(int x, int y, float sigma)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; return exp(-((pow(x, 2.0) + pow(y, 2.0)) / (2 * pow(sigma, 2.0))))
/
return 0; (2 * M_PI * pow(sigma, 2.0));
} }

View File

@ -18,7 +18,22 @@ PNM* BlurLinear::transform()
math::matrix<float> mask(maskSize, maskSize); math::matrix<float> mask(maskSize, maskSize);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; double maskSum = 0.0;
if (normalize) {
for (int i=0;i<tmpMask.size();++i) {
maskSum += tmpMask.at(i).toDouble();
}
}
for (int x=0;x<maskSize;++x) {
for (int y=0;y<maskSize;++y) {
double val = tmpMask.at((y * maskSize) + x).toDouble();
if (normalize && maskSum != 0.0) {
val /= maskSum;
}
mask[x][y] = val;
}
}
return convolute(mask, RepeatEdge); return convolute(mask, RepeatEdge);
} }

View File

@ -14,7 +14,11 @@ math::matrix<float> BlurUniform::getMask(int size, Mode)
{ {
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int x=0; x<size; ++x) {
for (int y=0; y<size; ++y) {
mask[x][y] = 1.0;
}
}
return mask; return mask;
} }

View File

@ -17,17 +17,21 @@ PNM* Convolution::transform()
return convolute(getMask(3, Normalize), RepeatEdge); return convolute(getMask(3, Normalize), RepeatEdge);
} }
/** Returns a sizeXsize matrix with the center point equal 1.0 */ math::matrix<float> Convolution::getMask(int size, Mode mode = Normalize) {
math::matrix<float> Convolution::getMask(int size, Mode mode = Normalize)
{
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
if (i / 2 == size / 2)
mask[i][j] = 1;
else
mask[i][j] = 0;
}
}
return mask; return mask;
} }
/** Does the convolution process for all pixels using the given mask. */
PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge) PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge)
{ {
int width = image->width(), int width = image->width(),
@ -35,43 +39,79 @@ PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge)
PNM* newImage = new PNM(width, height, image->format()); PNM* newImage = new PNM(width, height, image->format());
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; float weight = sum(mask);
for (int x=0; x<width; ++x) {
for (int y=0; y<height; ++y) {
math::matrix<float> window = getWindow(x, y, mask.colsize(), RChannel, mode);
float summ = sum(join(window, mask));
if (weight != 0) summ = summ / weight;
if (summ < 0) summ = 0;
else if (summ > 255) summ = 255;
int r = summ;
window = getWindow(x, y, mask.colsize(), GChannel, mode);
summ = sum(join(window, mask));
if (weight != 0) summ = summ / weight;
if (summ < 0) summ = 0;
else if (summ > 255) summ = 255;
int g = summ;
window = getWindow(x, y, mask.colsize(), BChannel, mode);
summ = sum(join(window, mask));
if (weight != 0) summ = summ / weight;
if (summ < 0) summ = 0;
else if (summ > 255) summ = 255;
int b = summ;
newImage->setPixel(x, y, QColor(r,g,b).rgb());
}
}
return newImage; return newImage;
} }
/** Joins to matrices by multiplying the A[i,j] with B[i,j].
* Warning! Both Matrices must be squares with the same size!
*/
const math::matrix<float> Convolution::join(math::matrix<float> A, math::matrix<float> B) const math::matrix<float> Convolution::join(math::matrix<float> A, math::matrix<float> B)
{ {
int size = A.rowno(); int size = A.rowno();
math::matrix<float> C(size, size); math::matrix<float> C(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
C[i][j] = A[i][j] * B[i][j];
}
}
return C; return C;
} }
/** Sums all of the matrixes elements */
const float Convolution::sum(const math::matrix<float> A) const float Convolution::sum(const math::matrix<float> A)
{ {
float sum = 0.0; float sum = 0.0;
int size = A.rowno();
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
sum += A[i][j];
}
}
return sum; return sum;
} }
/** Returns reflected version of a matrix */
const math::matrix<float> Convolution::reflection(const math::matrix<float> A) const math::matrix<float> Convolution::reflection(const math::matrix<float> A)
{ {
int size = A.rowno(); int size = A.rowno();
math::matrix<float> C(size, size); math::matrix<float> C(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
C[size - i][size - j] = A[i][j];
}
}
return C; return C;
} }

View File

@ -24,8 +24,30 @@ 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!"; PNM* horizontal = horizontalDetection();
PNM* vertical = verticalDetection();
return newImage; for (int x=0;x<image->width();++x) {
for (int y=0;y<image->height();++y) {
int hp = horizontal->pixel(x,y);
int vp = vertical->pixel(x,y);
int r = sqrt(pow(qRed(hp), 2.0) + pow(qRed(vp), 2.0));
int g = sqrt(pow(qGreen(hp), 2.0) + pow(qGreen(vp), 2.0));
int b = sqrt(pow(qBlue(hp), 2.0) + pow(qBlue(vp), 2.0));
if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) r = 0;
if (g > 255) r = 255;
if (b < 0) r = 0;
if (b > 255) r = 255;
newImage->setPixel(x, y, QColor(r,g,b).rgb());
}
}
return newImage;
} }

View File

@ -15,8 +15,14 @@ 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!"; for (int i=0;i<size;++i) {
for (int j=0;j<size;++j) {
if (i == size/2 && j == size/2)
mask[i][j] = (size*size) - 1;
else
mask[i][j] = -1;
}
}
return mask; return mask;
} }

View File

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

View File

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

View File

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

View File

@ -86,7 +86,6 @@ PNM* Transformation::getImage()
return image; return image;
} }
/** Returns a pixel value at given coordinates using different modes */
QRgb Transformation::getPixel(int x, int y, Mode mode) QRgb Transformation::getPixel(int x, int y, Mode mode)
{ {
switch (mode) { switch (mode) {
@ -95,50 +94,61 @@ QRgb Transformation::getPixel(int x, int y, Mode mode)
case RepeatEdge: return getPixelRepeat(x,y); case RepeatEdge: return getPixelRepeat(x,y);
default: return image->pixel(x,y); default: return image->pixel(x,y);
} }
} }
/** Returns a pixel using the Cyclic mode: QRgb Transformation::getPixelCyclic(int x, int y) {
* pixel(x,y) = pixel(x%width, y%width); int width = image->size().width();
*/ int height = image->size().height();
QRgb Transformation::getPixelCyclic(int x, int y) return image->pixel(x % width,y % height);
{
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
return image->pixel(x,y);
} }
/**
* Returns a given pixel
* If the pixel is out of image boundaries Black is returned;
*/
QRgb Transformation::getPixelNull(int x, int y) QRgb Transformation::getPixelNull(int x, int y)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; const QRgb blackPixel = qRgb(0, 0, 0);
int width = image->size().width();
int height = image->size().height();
if (x >= width || x < 0 || y >= height || y < 0)
return blackPixel;
return image->pixel(x,y); return image->pixel(x,y);
} }
/**
* Returns given pixel.
* If the pixel is out of image boundaries
* the nearest edge pixel is given
*/
QRgb Transformation::getPixelRepeat(int x, int y) QRgb Transformation::getPixelRepeat(int x, int y)
{ {
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int width = image->size().width();
int height = image->size().height();
if (x >= width) x = width - 1;
if (x < 0) x = 0;
if (y >= height) y = height - 1;
if (y < 0) y = 0;
return image->pixel(x,y); return image->pixel(x,y);
} }
/** Returns a size x size part of the image centered around (x,y) */
math::matrix<float> Transformation::getWindow(int x, int y, int size, math::matrix<float> Transformation::getWindow(int x, int y, int size,
Channel channel, Channel channel = LChannel,
Mode mode = RepeatEdge) Mode mode = RepeatEdge)
{ {
math::matrix<float> window(size,size); math::matrix<float> window(size,size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
QRgb pixel = getPixel(x - int(size/2) + i, y - int(size/2) + j, mode);
int pixelVal;
if (channel == RChannel) pixelVal = qRed(pixel);
if (channel == GChannel) pixelVal = qGreen(pixel);
if (channel == BChannel) pixelVal = qBlue(pixel);
if (channel == LChannel) pixelVal = qGray(pixel);
window[i][j] = pixelVal;
}
}
return window; return window;
} }