diff --git a/12/do_sprawdzenia/cpp/corner_harris.cpp b/12/do_sprawdzenia/cpp/corner_harris.cpp new file mode 100644 index 0000000..ae8d718 --- /dev/null +++ b/12/do_sprawdzenia/cpp/corner_harris.cpp @@ -0,0 +1,158 @@ +#include "corner_harris.h" + +#include "blur_gaussian.h" +#include "conversion_grayscale.h" +#include "edge_sobel.h" + +CornerHarris::CornerHarris(PNM* img) : + Convolution(img) +{ +} + +CornerHarris::CornerHarris(PNM* img, ImageViewer* iv) : + Convolution(img, iv) +{ +} + +PNM* CornerHarris::transform() +{ + int threshold = getParameter("threshold").toInt(); + double sigma = getParameter("sigma").toDouble(); + double sigma_weight = getParameter("sigma_weight").toDouble(); + double k_param = getParameter("k").toDouble(); + + int width = image->width(); + int height = image->height(); + + PNM* newImage = new PNM(width, height, QImage::Format_Mono); + + math::matrix Ixx(width, height); + math::matrix Iyy(width, height); + math::matrix Ixy(width, height); + math::matrix corner_candidates(width, height); + math::matrix corner_nonmax_suppress(width, height); + + ConversionGrayscale* conversion_grayscale = new ConversionGrayscale(image); + PNM* gray_image = conversion_grayscale->transform(); + + BlurGaussian* blur_gaussian = new BlurGaussian(gray_image); + blur_gaussian->setParameter("size", 3); + blur_gaussian->setParameter("sigma", 1.6); + PNM* blur_gauss_image = blur_gaussian->transform(); + + EdgeSobel* edge_sobel = new EdgeSobel(blur_gauss_image); + math::matrix* Gx = edge_sobel->rawHorizontalDetection(); + math::matrix* Gy = edge_sobel->rawVerticalDetection(); + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + Ixx[i][j] = (*Gx)[i][j] * (*Gx)[i][j]; + Ixy[i][j] = (*Gx)[i][j] * (*Gy)[i][j]; + Iyy[i][j] = (*Gy)[i][j] * (*Gy)[i][j]; + + corner_candidates[i][j] = 0; + corner_nonmax_suppress[i][j] = 0; + } + } + + for (int i = 1; i < width - 1; i++) + { + for (int j = 1; j < height - 1; j++) + { + float Sxx = 0; + float Syy = 0; + float Sxy = 0; + + for (int k = -1; k <= 1; k++) + { + for (int l = -1; l <= 1; l++) + { + Sxx = Sxx + Ixx[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + Syy = Syy + Iyy[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + Sxy = Sxy + Ixy[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + } + } + + Sxx = Sxx / sigma_weight; + Sxy = Sxy / sigma_weight; + Syy = Syy / sigma_weight; + + math::matrix H(2,2); + + H(0,0) = Sxx; + H(0,1) = Sxy; + H(1,0) = Sxy; + H(1,1) = Syy; + + float detH = H(0, 0) * H(1, 1) - H(0, 1) * H(1, 0); //determinant + float trH = H(0, 0) + H(1, 1); //trace + float r = detH - k_param * pow(trH, 2); + + if (r > threshold) + { + corner_candidates[i][j] = r; + } + } + } + + bool search = true; + + while(search) + { + search = false; + for (int i = 1; i < width - 1; i++) + { + for (int j = 1; j 0) + { + search = true; + } + + corner_nonmax_suppress[i][j] = 0; + } + + } + } + corner_candidates = corner_nonmax_suppress; + } + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + if (corner_candidates[i][j] == 0) + { + newImage->setPixel(i, j, Qt::color0); + } + else + { + newImage->setPixel(i, j, Qt::color1); + } + } + } + return newImage; +} + + diff --git a/12/do_sprawdzenia/cpp/image1.png b/12/do_sprawdzenia/cpp/image1.png new file mode 100644 index 0000000..80b3ea4 Binary files /dev/null and b/12/do_sprawdzenia/cpp/image1.png differ diff --git a/app/cpp/mysimplegimp/src/core/transformations/corner_harris.cpp b/app/cpp/mysimplegimp/src/core/transformations/corner_harris.cpp index f8979cf..ae8d718 100644 --- a/app/cpp/mysimplegimp/src/core/transformations/corner_harris.cpp +++ b/app/cpp/mysimplegimp/src/core/transformations/corner_harris.cpp @@ -17,16 +17,142 @@ CornerHarris::CornerHarris(PNM* img, ImageViewer* iv) : PNM* CornerHarris::transform() { int threshold = getParameter("threshold").toInt(); - double sigma = getParameter("sigma").toDouble(), - sigma_weight = getParameter("sigma_weight").toDouble(), - k_param = getParameter("k").toDouble(); + double sigma = getParameter("sigma").toDouble(); + double sigma_weight = getParameter("sigma_weight").toDouble(); + double k_param = getParameter("k").toDouble(); - int width = image->width(), - height = image->height(); + int width = image->width(); + int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + math::matrix Ixx(width, height); + math::matrix Iyy(width, height); + math::matrix Ixy(width, height); + math::matrix corner_candidates(width, height); + math::matrix corner_nonmax_suppress(width, height); + ConversionGrayscale* conversion_grayscale = new ConversionGrayscale(image); + PNM* gray_image = conversion_grayscale->transform(); + + BlurGaussian* blur_gaussian = new BlurGaussian(gray_image); + blur_gaussian->setParameter("size", 3); + blur_gaussian->setParameter("sigma", 1.6); + PNM* blur_gauss_image = blur_gaussian->transform(); + + EdgeSobel* edge_sobel = new EdgeSobel(blur_gauss_image); + math::matrix* Gx = edge_sobel->rawHorizontalDetection(); + math::matrix* Gy = edge_sobel->rawVerticalDetection(); + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + Ixx[i][j] = (*Gx)[i][j] * (*Gx)[i][j]; + Ixy[i][j] = (*Gx)[i][j] * (*Gy)[i][j]; + Iyy[i][j] = (*Gy)[i][j] * (*Gy)[i][j]; + + corner_candidates[i][j] = 0; + corner_nonmax_suppress[i][j] = 0; + } + } + + for (int i = 1; i < width - 1; i++) + { + for (int j = 1; j < height - 1; j++) + { + float Sxx = 0; + float Syy = 0; + float Sxy = 0; + + for (int k = -1; k <= 1; k++) + { + for (int l = -1; l <= 1; l++) + { + Sxx = Sxx + Ixx[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + Syy = Syy + Iyy[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + Sxy = Sxy + Ixy[i + k][j + l] * BlurGaussian::getGauss(k, l, sigma); + } + } + + Sxx = Sxx / sigma_weight; + Sxy = Sxy / sigma_weight; + Syy = Syy / sigma_weight; + + math::matrix H(2,2); + + H(0,0) = Sxx; + H(0,1) = Sxy; + H(1,0) = Sxy; + H(1,1) = Syy; + + float detH = H(0, 0) * H(1, 1) - H(0, 1) * H(1, 0); //determinant + float trH = H(0, 0) + H(1, 1); //trace + float r = detH - k_param * pow(trH, 2); + + if (r > threshold) + { + corner_candidates[i][j] = r; + } + } + } + + bool search = true; + + while(search) + { + search = false; + for (int i = 1; i < width - 1; i++) + { + for (int j = 1; j 0) + { + search = true; + } + + corner_nonmax_suppress[i][j] = 0; + } + + } + } + corner_candidates = corner_nonmax_suppress; + } + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + if (corner_candidates[i][j] == 0) + { + newImage->setPixel(i, j, Qt::color0); + } + else + { + newImage->setPixel(i, j, Qt::color1); + } + } + } return newImage; } + +