From c61aff0e9a984becbee253f8b881561cdc23c9ba Mon Sep 17 00:00:00 2001 From: BohdanBakhlul Date: Fri, 11 Jun 2021 20:38:59 +0200 Subject: [PATCH] zadania 8 i 9 --- src/core/transformations/edge_canny.cpp | 121 +++++++++++++++++++++++- src/core/transformations/edge_sobel.cpp | 27 +++++- src/core/transformations/hough.cpp | 56 ++++++++++- 3 files changed, 197 insertions(+), 7 deletions(-) diff --git a/src/core/transformations/edge_canny.cpp b/src/core/transformations/edge_canny.cpp index b52129f..62fba0b 100644 --- a/src/core/transformations/edge_canny.cpp +++ b/src/core/transformations/edge_canny.cpp @@ -22,9 +22,128 @@ PNM* EdgeCanny::transform() int upper_thresh = getParameter("upper_threshold").toInt(), lower_thresh = getParameter("lower_threshold").toInt(); + PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + //TO GRAY + ConversionGrayscale gray(image); + newImage = gray.transform(); + + //BLUR + BlurGaussian gauss(newImage); + gauss.setParameter("size", 3); + gauss.setParameter("sigma", 1.6); + newImage = gauss.transform(); + + //SOBEL DETECTION + 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)) || (angle > 157.5) || (angle < -157.5)) + a = 0; + else if (((angle > 22.5) && (angle < 67.5)) || ((angle < -112.5) && (angle > -157.5))) + a = 1; + else if (((angle > 67.5) && (angle < 112.5)) || ((angle < -67.5) && (angle > -112.5))) + a = 2; + else if (((angle > 112.5) && (angle < 157.5)) || ((angle < -22.5) && (angle > -67.5))) + a = 3; + + angles[x][y] = a; + } + } + + for (int x=1;x n1 && n > n2 && n > upper_thresh / 3.33){ + n = n > 255 ? 255 : n; + newImage->setPixel(x, y, QColor(n, n, n).rgb()); + } else + newImage->setPixel(x, y, QColor(0, 0, 0).rgb()); + } + } + + int highThresh = (upper_thresh / 3.33) * 255 / 100; + int lowThresh = lower_thresh * 255 / 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 808707e..dc66d15 100644 --- a/src/core/transformations/edge_sobel.cpp +++ b/src/core/transformations/edge_sobel.cpp @@ -23,18 +23,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; } diff --git a/src/core/transformations/hough.cpp b/src/core/transformations/hough.cpp index 7ab2c97..886f25f 100644 --- a/src/core/transformations/hough.cpp +++ b/src/core/transformations/hough.cpp @@ -2,6 +2,7 @@ #include "conversion_grayscale.h" #include "edge_laplacian.h" +#include Hough::Hough(PNM* img) : Transformation(img) @@ -16,8 +17,59 @@ Hough::Hough(PNM* img, ImageViewer* super) : PNM* Hough::transform() { int thetaDensity = getParameter("theta_density").toInt(); + bool skip_edge = getParameter("skip_edge_detection").toBool(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + ConversionGrayscale gray(image); + + PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_Grayscale8); + newImage = gray.transform(); + + if (skip_edge == false) { + EdgeLaplacian laplacian(newImage); + laplacian.setParameter("size", 5); + newImage = laplacian.transform(); + } + + float diag = sqrt( pow(image->width(), 2) + pow(image->height(), 2) ); + float thetaSize = 180 * thetaDensity; + + int width = thetaSize; + int height = (diag * 2) + 1; + + PNM* resultImage = new PNM(width, height, QImage::Format_Grayscale8); + + math::matrix houghtMatrix(width, height, 0.0); + math::matrix theta(width, height); + + for (int x=0;xwidth();++x) { + for (int y=0;yheight();++y) { + if (qGray(newImage->pixel(x,y)) > 0) { + for (int k = 0; k < thetaSize; ++k) { + float theta = (k * M_PI) / (thetaDensity * 180.0); + float p = (x * cos(theta)) + (y * sin(theta)); + int second = diag + p; + houghtMatrix[k][second] += 1; + } + + } + } + } + + delete newImage; + + float maxHough = 255 / houghtMatrix.max(); + for (int x=0;xsetPixel(x, + y, + QColor(houghtMatrix[x][y], + houghtMatrix[x][y], + houghtMatrix[x][y] + ).rgb()); + } + } + + return resultImage; - return 0; }