diff --git a/11/do_sprawdzenia/cpp/bin_gradient.cpp b/11/do_sprawdzenia/cpp/bin_gradient.cpp new file mode 100644 index 0000000..d07edb2 --- /dev/null +++ b/11/do_sprawdzenia/cpp/bin_gradient.cpp @@ -0,0 +1,83 @@ +#include "bin_gradient.h" +using namespace std; + +BinarizationGradient::BinarizationGradient(PNM* img) : + Transformation(img) +{ +} + +BinarizationGradient::BinarizationGradient(PNM* img, ImageViewer* iv) : + Transformation(img, iv) +{ +} + +PNM* BinarizationGradient::transform() +{ + int width = image->width(); + int height = image->height(); + + PNM* newImage = new PNM(width, height, QImage::Format_Mono); + int numerator = 0; + int denominator = 0; + + // Gx + int Gx = 0; + + // Gy + int Gy = 0; + + int G = 0; + int T = 0; + + //PNM* newImage = new PNM(width, height, QImage::Format_Mono); + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + Mode mode = Transformation::RepeatEdge; + int I = qGray(getPixel(x, y, mode)); + + // Get Gx and Gy + Gx = qGray(getPixel(x + 1, y, mode)) - qGray(getPixel(x - 1, y, mode)); + Gy = qGray(getPixel(x, y + 1, mode)) - qGray(getPixel(x, y - 1, mode)); + + if (Gx > Gy) + { + G = Gx; + } + else + { + G = Gy; + } + numerator += I * G; + denominator += G; + } + } + + T = numerator / denominator; + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + Mode mode = Transformation::RepeatEdge; + QRgb pixel = getPixel(x, y, mode); + + int val = qGray(pixel); + + if (val < T) + { + newImage->setPixel(x, y, 0); + } + else + { + newImage->setPixel(x, y, 1); + } + } + } + + return newImage; +} + + diff --git a/11/do_sprawdzenia/cpp/hough.cpp b/11/do_sprawdzenia/cpp/hough.cpp new file mode 100644 index 0000000..e70518d --- /dev/null +++ b/11/do_sprawdzenia/cpp/hough.cpp @@ -0,0 +1,93 @@ +#include "hough.h" +#include "iostream" +#include "conversion_grayscale.h" +#include "edge_laplacian.h" + +Hough::Hough(PNM* img) : + Transformation(img) +{ +} + +Hough::Hough(PNM* img, ImageViewer* super) : + Transformation(img, super) +{ +} + +PNM* Hough::transform() +{ + int width = image->width(); + int height = image->height(); + + int theta_density = getParameter("theta_density").toInt(); + bool skip_edge = getParameter("skip_edge_detection").toBool(); + + std::cout << "skip_edge: " << skip_edge << std::endl; + int theta_size = 180 * theta_density; + double max_ro = sqrt(pow(width, 2) + pow(height, 2)); + + ConversionGrayscale* gray_scale = new ConversionGrayscale(image); + image = gray_scale->transform(); + + PNM* newImage = new PNM(theta_size, max_ro * 2 + 1, QImage::Format_Grayscale8); + + int new_image_width = newImage->width(); + int new_image_height = newImage->height(); + + math::matrix hough(new_image_width, new_image_height); + + + + if (skip_edge == false) + { + EdgeLaplacian *el = new EdgeLaplacian(image); + el->setParameter("size", 7); + image = el->transform(); + } + + for (int x = 0; x < new_image_width; x++) + { + for (int y = 0; y < new_image_height; y++) + { + hough[x][y] = 0; + } + } + + + double theta = 0.0; + double ro = 0; + float max = 0; + double max_z = 0; + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + if (qGray(image->pixel(i, j)) > 0) + { + for (int k = 0; k < theta_size; k++) + { + theta = (k * M_PI) / (theta_density * 180); + ro = i * cos(theta) + j * sin(theta); + hough[k][ro + max_ro]++; + } + } + } + } + + max = hough.max(); + + int z; + + for (int x = 0; x < new_image_width; x++) + { + for (int y = 0; y < new_image_height; y++) + { + z = hough[x][y] * (max / 255); + + newImage->setPixel(x, y, QColor(z, z, z).rgb()); + } + } + std::cout << "max_z: " << max_z << std::endl; + + return newImage; +} diff --git a/11/do_sprawdzenia/cpp/hough_lines.cpp b/11/do_sprawdzenia/cpp/hough_lines.cpp new file mode 100644 index 0000000..41c2db3 --- /dev/null +++ b/11/do_sprawdzenia/cpp/hough_lines.cpp @@ -0,0 +1,91 @@ +#include "hough_lines.h" +#include "bin_gradient.h" +#include "edge_laplacian.h" + +#include "hough.h" +#include +#include +#include + +HoughLines::HoughLines(PNM* img) : + Transformation(img) +{ +} + +HoughLines::HoughLines(PNM* img, ImageViewer* iv) : + Transformation(img, iv) +{ +} + + + + + + +PNM* HoughLines::transform() +{ + + // Cut of value from the image; + int threshold = getParameter("threshold").toInt(); + bool drawWholeLines = getParameter("draw_whole_lines").toBool(); + int theta_density = 3; + + PNM* newImage = new PNM(image->copy()); + + EdgeLaplacian el(image); + el.setParameter("size", 3); + PNM* image_edge = el.transform(); + + PNM* image_bin = BinarizationGradient(image_edge).transform(); + + Hough h(image_edge); + + h.setParameter("theta_density", theta_density); + h.setParameter("skip_edge_detection", true); + PNM* image_hough = h.transform(); + + int hough_width = image_hough->width(); + int hough_height = image_hough->height(); + + int width = image_bin->width(); + int height = image_bin->height(); + + QPainter qPainter (newImage); + qPainter.setPen(Qt::red); + + for (int theta = 0; theta < hough_width; theta++) + { + for (int rho = 0; rho < hough_height; rho++) + { + int val = (int) qGray(image_hough->pixel(theta, rho)); + if (val > threshold) + { + double rtheta = ((double)theta / 3.0) * M_PI / 180.0; + int rrho = rho - hough_height / 2; + qPainter.drawLine(0, round(rrho / sin(rtheta)), width - 1, round((rrho - (width - 1) * cos(rtheta)) / sin(rtheta))); + + } + } + } + + int counter_pixels = 0; + if (!drawWholeLines) + { + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + QRgb pixel = image_bin->pixel(x, y); + + if (qGray(pixel) == 0) + { + counter_pixels++; + newImage->setPixel(x, y, image->pixel(x, y)); + } + } + } + std::cout << "counter " << counter_pixels << std::endl; + } + + return newImage; +} diff --git a/11/do_sprawdzenia/cpp/image1.png b/11/do_sprawdzenia/cpp/image1.png new file mode 100644 index 0000000..81e70f3 Binary files /dev/null and b/11/do_sprawdzenia/cpp/image1.png differ diff --git a/11/do_sprawdzenia/cpp/image2.png b/11/do_sprawdzenia/cpp/image2.png new file mode 100644 index 0000000..c0729ef Binary files /dev/null and b/11/do_sprawdzenia/cpp/image2.png differ diff --git a/app/cpp/mysimplegimp/src/core/transformations/hough.cpp b/app/cpp/mysimplegimp/src/core/transformations/hough.cpp index 7ab2c97..e70518d 100644 --- a/app/cpp/mysimplegimp/src/core/transformations/hough.cpp +++ b/app/cpp/mysimplegimp/src/core/transformations/hough.cpp @@ -1,5 +1,5 @@ #include "hough.h" - +#include "iostream" #include "conversion_grayscale.h" #include "edge_laplacian.h" @@ -15,9 +15,79 @@ Hough::Hough(PNM* img, ImageViewer* super) : PNM* Hough::transform() { - int thetaDensity = getParameter("theta_density").toInt(); + int width = image->width(); + int height = image->height(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int theta_density = getParameter("theta_density").toInt(); + bool skip_edge = getParameter("skip_edge_detection").toBool(); - return 0; + std::cout << "skip_edge: " << skip_edge << std::endl; + int theta_size = 180 * theta_density; + double max_ro = sqrt(pow(width, 2) + pow(height, 2)); + + ConversionGrayscale* gray_scale = new ConversionGrayscale(image); + image = gray_scale->transform(); + + PNM* newImage = new PNM(theta_size, max_ro * 2 + 1, QImage::Format_Grayscale8); + + int new_image_width = newImage->width(); + int new_image_height = newImage->height(); + + math::matrix hough(new_image_width, new_image_height); + + + + if (skip_edge == false) + { + EdgeLaplacian *el = new EdgeLaplacian(image); + el->setParameter("size", 7); + image = el->transform(); + } + + for (int x = 0; x < new_image_width; x++) + { + for (int y = 0; y < new_image_height; y++) + { + hough[x][y] = 0; + } + } + + + double theta = 0.0; + double ro = 0; + float max = 0; + double max_z = 0; + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + if (qGray(image->pixel(i, j)) > 0) + { + for (int k = 0; k < theta_size; k++) + { + theta = (k * M_PI) / (theta_density * 180); + ro = i * cos(theta) + j * sin(theta); + hough[k][ro + max_ro]++; + } + } + } + } + + max = hough.max(); + + int z; + + for (int x = 0; x < new_image_width; x++) + { + for (int y = 0; y < new_image_height; y++) + { + z = hough[x][y] * (max / 255); + + newImage->setPixel(x, y, QColor(z, z, z).rgb()); + } + } + std::cout << "max_z: " << max_z << std::endl; + + return newImage; } diff --git a/app/cpp/mysimplegimp/src/core/transformations/hough_lines.cpp b/app/cpp/mysimplegimp/src/core/transformations/hough_lines.cpp index 02ec3e4..41c2db3 100644 --- a/app/cpp/mysimplegimp/src/core/transformations/hough_lines.cpp +++ b/app/cpp/mysimplegimp/src/core/transformations/hough_lines.cpp @@ -1,9 +1,10 @@ #include "hough_lines.h" - #include "bin_gradient.h" #include "edge_laplacian.h" -#include "hough.h" +#include "hough.h" +#include +#include #include HoughLines::HoughLines(PNM* img) : @@ -16,15 +17,75 @@ 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(); + int theta_density = 3; PNM* newImage = new PNM(image->copy()); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + EdgeLaplacian el(image); + el.setParameter("size", 3); + PNM* image_edge = el.transform(); + + PNM* image_bin = BinarizationGradient(image_edge).transform(); + + Hough h(image_edge); + + h.setParameter("theta_density", theta_density); + h.setParameter("skip_edge_detection", true); + PNM* image_hough = h.transform(); + + int hough_width = image_hough->width(); + int hough_height = image_hough->height(); + + int width = image_bin->width(); + int height = image_bin->height(); + + QPainter qPainter (newImage); + qPainter.setPen(Qt::red); + + for (int theta = 0; theta < hough_width; theta++) + { + for (int rho = 0; rho < hough_height; rho++) + { + int val = (int) qGray(image_hough->pixel(theta, rho)); + if (val > threshold) + { + double rtheta = ((double)theta / 3.0) * M_PI / 180.0; + int rrho = rho - hough_height / 2; + qPainter.drawLine(0, round(rrho / sin(rtheta)), width - 1, round((rrho - (width - 1) * cos(rtheta)) / sin(rtheta))); + + } + } + } + + int counter_pixels = 0; + if (!drawWholeLines) + { + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + QRgb pixel = image_bin->pixel(x, y); + + if (qGray(pixel) == 0) + { + counter_pixels++; + newImage->setPixel(x, y, image->pixel(x, y)); + } + } + } + std::cout << "counter " << counter_pixels << std::endl; + } return newImage; }