From d80914966880b859b56ea7eef9ce192716c604fd Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 6 Jan 2016 18:30:00 +0100 Subject: [PATCH] [TASK] Kind of begin hough transformation. --- hough_machine.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++--- imageviewer-qt4.cpp | 3 ++ 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/hough_machine.cpp b/hough_machine.cpp index dd6852b..d12a86e 100644 --- a/hough_machine.cpp +++ b/hough_machine.cpp @@ -9,22 +9,110 @@ #include +#include "lazy_image.cpp" + class HoughMachine { private: + LazyImage* original; + LazyImage* working_copy; + int x_center; + int y_center; + int n_ang; + double d_ang; + int n_rad; + double d_rad; + int* hough_arr; public: - HoughMachine() { - + HoughMachine(LazyImage* original, LazyImage* working_copy) { + this->original = original; + this->working_copy = working_copy; + this->hough_arr = NULL; }; ~HoughMachine() { - + this->reset(); + }; + + void reset(void) { + this->x_center = 0; + this->y_center = 0; + this->n_ang = 0; + this->d_ang = 0; + this->n_rad = 0; + this->d_rad = 0; + if(this->hough_arr == NULL) { + free(this->hough_arr); + this->hough_arr = NULL; + } }; - void convertToMonochrome(void) { - + void run(int a_steps, int r_steps) { + this->reset(); + this->x_center = this->original->width()/2; + this->y_center = this->original->height()/2; + this->n_ang = a_steps; + this->d_ang = M_PI / this->n_ang; + this->n_rad = r_steps; + double r_max = sqrt(pow(this->x_center, 2) + pow(this->y_center, 2)); + this->d_rad = (2*r_max)/this->n_rad; + this->hough_arr = (int*) malloc(sizeof(int) * this->n_ang * this->n_rad); + for(int i=0; i< this->n_ang*this->n_rad; i++) this->hough_arr[i] = 0; + this->fillHoughAccumulator(); + this->debugShow(); + }; + + void fillHoughAccumulator() { + for(int v=0; voriginal->height(); v++) { + for(int u=0; uoriginal->width(); u++) { + QColor pixel = QColor::fromRgb(this->original->getPixel(u, v, LazyImage::DEFAULT)); + int h, s, l; + pixel.getHsl(&h, &s, &l); + if(l > 0) { + this->doPixel(u, v); + } + } + } + }; + + void doPixel(int u, int v) { + int x = u - this->x_center; + int y = v - this->y_center; + for(int a=0; an_ang; a++) { + double theta = this->d_ang * a; + int r = (int) qRound( (x*cos(theta) + y*sin(theta)) / this->d_rad ) + this->n_rad / 2; + if(r >= 0 && r < this->n_rad) { + this->hough_arr[a*this->n_ang+r] += 1; + } + } + }; + + void debugShow() { + std::cout << "Dimensions: " << this->n_ang << " x " << this->n_rad << " | " << std::endl; + + int max_value = 0; + for(int i=0; in_ang*this->n_rad; i++) { + int current_value = this->hough_arr[i]; + if(current_value > max_value) max_value = current_value; + } + + for(int x=0; xn_ang; x++) { + for(int y=0; yn_rad; y++) { + int value = qRound(255.0*this->hough_arr[x*this->n_ang+y] / max_value); + if(value < 0) { + std::cout << value << std::endl; + value = 0; + } + if(value > 255) { + std::cout << value << std::endl; + value = 255; + } + int color = QColor::fromRgb(value, value, value).rgb(); + this->working_copy->getImage()->setPixel(x,y,color); + } + } }; }; diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index 3215623..b2f5955 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -773,6 +773,9 @@ void ImageViewer::doUnsharpMasking(void) { void ImageViewer::runHoughTransformation(void) { logFile << "Let's run a hough transformation!" << std::endl; renewLogging(); + HoughMachine* hm = new HoughMachine(original, working_copy); + hm->run(250, 250); + delete hm; updateImageDisplay(); } /****************************************************************************************