From 424df7fc495c2f5032b2eac1b4842862d9b762ba Mon Sep 17 00:00:00 2001 From: Patrycjusz Mania Date: Thu, 3 Jun 2021 11:49:40 +0200 Subject: [PATCH] zadanie-8 (canny) --- src/core/transformations/convolution.cpp | 3 +- src/core/transformations/edge_canny.cpp | 103 ++++++++++++++++++++++- src/core/transformations/edge_sobel.cpp | 27 +++++- 3 files changed, 126 insertions(+), 7 deletions(-) diff --git a/src/core/transformations/convolution.cpp b/src/core/transformations/convolution.cpp index 30f6683..690f9d2 100644 --- a/src/core/transformations/convolution.cpp +++ b/src/core/transformations/convolution.cpp @@ -50,7 +50,8 @@ PNM* Convolution::convolute(math::matrix mask, Mode mode = RepeatEdge) int r = getOneChannelPixel(mask, x, y, mode, RChannel, weight); int g = getOneChannelPixel(mask, x, y, mode, GChannel, weight); int b = getOneChannelPixel(mask, x, y, mode, BChannel, weight); - newImage->setPixel(x, y, QColor(r,g,b).rgb()); + int l = getOneChannelPixel(mask, x, y, mode, LChannel, weight); + newImage->setPixel(x, y, QColor(r,g,b,l).rgb()); } } diff --git a/src/core/transformations/edge_canny.cpp b/src/core/transformations/edge_canny.cpp index b52129f..0f1fdb9 100644 --- a/src/core/transformations/edge_canny.cpp +++ b/src/core/transformations/edge_canny.cpp @@ -19,12 +19,111 @@ PNM* EdgeCanny::transform() int width = image->width(), height = image->height(); - int upper_thresh = getParameter("upper_threshold").toInt(), + int upper_thresh = getParameter("upper_threshold").toInt() / 3.33, lower_thresh = getParameter("lower_threshold").toInt(); PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8); + ConversionGrayscale gray(image); + newImage = gray.transform(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + BlurGaussian gauss(newImage); + gauss.setParameter("size", 3); + gauss.setParameter("sigma", 1.6); + newImage = gauss.transform(); + + EdgeSobel sobel(newImage); + math::matrix* horizontal = sobel.rawHorizontalDetection(); + math::matrix* vertical = sobel.rawVerticalDetection(); + + math::matrix strenghts(width, height); + math::matrix angles(width, height); + + for (int x=0;x -22.5)) || (thisAngle > 157.5) || (thisAngle < -157.5)) + newAngle = 0; + else if (((thisAngle > 22.5) && (thisAngle < 67.5)) || ((thisAngle < -112.5) && (thisAngle > -157.5))) + newAngle = 45; + else if (((thisAngle > 67.5) && (thisAngle < 112.5)) || ((thisAngle < -67.5) && (thisAngle > -112.5))) + newAngle = 90; + else if (((thisAngle > 112.5) && (thisAngle < 157.5)) || ((thisAngle < -22.5) && (thisAngle > -67.5))) + newAngle = 135; + + angles[x][y] = newAngle; + } + } + + for (int x=1;x window = getWindow(x,y,3, LChannel, RepeatEdge); + int angle = angles[x][y]; + + float neighbour1 = 0; + float neighbour2 = 0; + float center = strenghts[x][y]; + if (angle == 0) { + neighbour1 = strenghts[x][y-1]; + neighbour2 = strenghts[x][y+1]; + } else if (angle == 45) { + neighbour1 = strenghts[x-1][y-1]; + neighbour2 = strenghts[x+1][y+1]; + } else if (angle == 90) { + neighbour1 = strenghts[x-1][y]; + neighbour2 = strenghts[x+1][y]; + } else if (angle == 135) { + neighbour1 = strenghts[x+1][y+1]; + neighbour2 = strenghts[x-1][y-1]; + } + + if (center > neighbour1 && center > neighbour2 && center > upper_thresh){ + center = center > 255 ? 255 : center; + newImage->setPixel(x, y, QColor(center, center, center).rgb()); + }else + newImage->setPixel(x, y, QColor(0, 0, 0).rgb()); + } + } + + int highThresh = upper_thresh * 255 / 100; + int lowThresh = highThresh * lower_thresh / 100; + unsigned int high = QColor(highThresh,highThresh,highThresh).rgb(); + unsigned int low = QColor(lowThresh,lowThresh,lowThresh).rgb(); + unsigned int weak = QColor(128,128,128).rgb(); + unsigned int strong = QColor(255,255,255).rgb(); + for (int x=1;xpixel(x,y) >= high) { + newImage->setPixel(x, y, strong); + } + else if (newImage->pixel(x,y) < high && newImage->pixel(x,y) >= low) { + newImage->setPixel(x, y, weak); + } + else { + newImage->setPixel(x, y, QColor(0, 0, 0).rgb()); + } + + unsigned int white = QColor(255, 255, 255).rgb(); + if (newImage->pixel(x, y) == weak) { + if ((newImage->pixel(x+1, y-1) == white) || + (newImage->pixel(x+1, y) == white) || + (newImage->pixel(x+1, y+1) == white) || + (newImage->pixel(x, y-1) == white) || + (newImage->pixel(x, y+1) == white) || + (newImage->pixel(x+1, y-1) == white) || + (newImage->pixel(x-1, y-1) == white) || + (newImage->pixel(x-1, y) == white) || + (newImage->pixel(x-1, y+1) == white)) + newImage->setPixel(x, y, QColor(255, 255, 255).rgb()); + else + newImage->setPixel(x, y, QColor(0, 0, 0).rgb()); + } + } + } return newImage; } diff --git a/src/core/transformations/edge_sobel.cpp b/src/core/transformations/edge_sobel.cpp index b457b3d..a7b04e0 100644 --- a/src/core/transformations/edge_sobel.cpp +++ b/src/core/transformations/edge_sobel.cpp @@ -28,18 +28,37 @@ void EdgeSobel::prepareMatrices() math::matrix* EdgeSobel::rawHorizontalDetection() { - math::matrix* x_gradient = new math::matrix(this->image->width(), this->image->height()); + int width = image->width(); + int height = image->height(); + math::matrix* x_gradient = new math::matrix(width, height); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + PNM* newImage = convolute(g_x, RepeatEdge); + for (int x=0;xpixel(x,y)); + } + } + + delete newImage; return x_gradient; } math::matrix* EdgeSobel::rawVerticalDetection() { - math::matrix* y_gradient = new math::matrix(this->image->width(), this->image->height()); + int width = image->width(); + int height = image->height(); + math::matrix* y_gradient = new math::matrix(width, height); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + PNM* newImage = convolute(g_y, RepeatEdge); + + for (int x=0;xpixel(x,y)); + } + } + + delete newImage; return y_gradient; }