From fdf0bb089a02ea5453d6b48a232126d6f0367572 Mon Sep 17 00:00:00 2001 From: Sebastian Spaloniak Date: Sun, 31 May 2020 12:17:02 +0200 Subject: [PATCH] Zadanie 11 --- .../src/core/transformations/hough.cpp | 65 ++++++++++++++++++- .../src/core/transformations/hough_lines.cpp | 57 +++++++++++++++- 2 files changed, 117 insertions(+), 5 deletions(-) diff --git a/lpo-projekt-v2/src/core/transformations/hough.cpp b/lpo-projekt-v2/src/core/transformations/hough.cpp index 7ab2c97..abf708c 100644 --- a/lpo-projekt-v2/src/core/transformations/hough.cpp +++ b/lpo-projekt-v2/src/core/transformations/hough.cpp @@ -14,10 +14,69 @@ Hough::Hough(PNM* img, ImageViewer* super) : } PNM* Hough::transform() -{ +{ int thetaDensity = getParameter("theta_density").toInt(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int width = image->width(), + height = image->height(); - return 0; + PNM* tempImage = new PNM(width, height, QImage::Format_Indexed8); + + tempImage = ConversionGrayscale(image).transform(); + + if (!getParameter("skip_edge_detection").toBool()) + { + tempImage = EdgeLaplacian(tempImage).transform(); + } + + int pmax = (int)sqrt(pow((double)height, 2.0) + pow((double)width, 2.0)); + int thetaSize = 180 * thetaDensity; + + PNM* newImage = new PNM(thetaSize, pmax * 2 + 1, QImage::Format_Indexed8); + + math::matrix hough(thetaSize, pmax * 2 + 1); + + for (int i = 0; i < thetaSize; i++) + { + for (int j = 0, len = pmax * 2 + 1; j < len; j++) + { + hough(i, j) = 0; + } + } + + double t, r; + int max = 0; + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + QRgb pixel = tempImage->pixel(i, j); + if (qGray(pixel) > 0) + { + for (int k = 0; k < thetaSize; k++) + { + t = ((double)k * 3.14159265359) / (180.0 * thetaDensity); + r = i*cos(t) + j*sin(t); + + hough(k, (int)floor(r + pmax))++; + + if (hough(k, (int)floor(r + pmax)) > max) + { + max = hough(k, (int)floor(r + pmax)); + } + } + } + } + } + + for (int i = 0; i < thetaSize; i++) + { + for (int j = 0, len = pmax * 2 + 1; j < len; j++) + { + newImage->setPixel(i, j, (int)(((hough(i, j) * 255)/ max))); + } + } + + return newImage; } diff --git a/lpo-projekt-v2/src/core/transformations/hough_lines.cpp b/lpo-projekt-v2/src/core/transformations/hough_lines.cpp index 02ec3e4..636b3ab 100644 --- a/lpo-projekt-v2/src/core/transformations/hough_lines.cpp +++ b/lpo-projekt-v2/src/core/transformations/hough_lines.cpp @@ -18,13 +18,66 @@ HoughLines::HoughLines(PNM* img, ImageViewer* iv) : PNM* HoughLines::transform() { - // Cut of value from the image; int threshold = getParameter("threshold").toInt(); bool drawWholeLines = getParameter("draw_whole_lines").toBool(); PNM* newImage = new PNM(image->copy()); + PNM* tempImage, *binImage; + EdgeLaplacian* edla = new EdgeLaplacian(image); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + edla->setParameter("size", 3); + tempImage = edla->transform(); + + BinarizationGradient* binar = new BinarizationGradient(tempImage); + + binImage = binar->transform(); + Hough* hough = new Hough(binImage); + + hough->setParameter("theta_density", 3); + hough->setParameter("skip_edge_detection", true); + + tempImage = hough->transform(); + QPainter* qPainter = new QPainter(newImage); + qPainter->setPen(Qt::red); + + int i1 = 0, i2, j1, j2; + + for (int t = 0; t < tempImage->width(); t++) + { + for (int r = 0; r < tempImage->height(); r++) + { + QRgb pixel = tempImage->pixel(t, r); + + int valPixel = qGray(pixel); + + if (valPixel > threshold) + { + int real_rho = r - tempImage->height() / 2; + double rtheta = (t / 3.0)*M_PI / 180.0; + double ctheta = cos(rtheta), stheta = sin(rtheta); + + j1 = round(real_rho / stheta); + i2 = newImage->width() - 1; + j2 = round((real_rho - (newImage->width() - 1)*ctheta) / stheta); + + qPainter->drawLine(i1, j1, i2, j2); + } + } + } + + if (!drawWholeLines == true) + { + for (int i = 0; i < newImage->width(); i++) + { + for (int j = 0; j < newImage->height(); j++) + { + if (qRed(binImage->pixel(i, j)) == 0) + { + newImage->setPixel(i, j, image->pixel(i, j)); + } + } + } + } return newImage; }