diff --git a/canny_edge_machine.cpp b/canny_edge_machine.cpp index aa4e9de..5af5737 100644 --- a/canny_edge_machine.cpp +++ b/canny_edge_machine.cpp @@ -12,14 +12,108 @@ class CannyEdgeMachine { private: - LazyImage* img; + LazyImage* original; + LazyImage* working_copy; + + int width; + int height; + int pixels; + double* gradient_magnitude; + double* maximum_magnitude; + int* binary_edge_pixels; + int* gradient_x; + int* gradient_y; + + // Params + double t_low; + double t_high; + public: - CannyEdgeMachine(){ - // TODO + CannyEdgeMachine(LazyImage* original, LazyImage* working_copy){ + this->original = original; + this->working_copy = working_copy; + + this->width = this->original->width(); + this->height = this->original->height(); + this->pixels = this->width * this->height; + + this->gradient_magnitude = (double*) malloc(sizeof(double) * this->pixels); + this->maximum_magnitude = (double*) malloc(sizeof(double) * this->pixels); + this->binary_edge_pixels = (int*) malloc(sizeof(int) * this->pixels); + this->gradient_x = (int*) malloc(sizeof(int) * this->pixels); + this->gradient_y = (int*) malloc(sizeof(int) * this->pixels); }; ~CannyEdgeMachine() { - // TODO + free(this->gradient_magnitude); + free(this->maximum_magnitude); + free(this->binary_edge_pixels); + free(this->gradient_x); + free(this->gradient_y); + }; + + void setThresholdValues(double t_low, double t_high) { + this->t_low = t_low; + this->t_high = t_high; + }; + + void reset(void) { + for(int i=0; ipixels; i++) { + this->gradient_magnitude = 0; + this->maximum_magnitude = 0; + this->binary_edge_pixels = 0; + this->gradient_x = 0; + this->gradient_y = 0; + } + }; + + void doGaussBlur(int filter_width=3) { + // build the gauss filter + int* filter = (int*) malloc(sizeof(int) * filter_width* filter_width); + int sum_weights = 0; + for(int i=0; iwidth-filter_offset; x++) { + for(int y=0+filter_offset; ywidth-filter_offset; y++) { + int sum_intensity = 0; + int h, s, l; + for(int fx=0; fxoriginal->getImage()->pixel(x+dx, y+dy)); + color.getHsl(&h, &s, &l); + sum_intensity += (l * filter[fy*filter_width + fx]); + } + } + QColor color = QColor::fromRgb(original->getImage()->pixel(x, y)); + color.getHsl(&h, &s, &l); + l = qRound((1.0*sum_intensity) / (1.0*sum_weights)); + if(l > 255) l = 255; + if(l < 0) l = 0; + color.setHsl(h, s, l); + this->working_copy->getImage()->setPixel(x, y, color.rgb()); + } + } + free(filter); + }; + + void work() { + this->reset(); + + for(int x=0; xwidth; x++) { + for(int y=0; yheight; y++) { + //this->gradient_magnitude = sqrt(...) + } + } + + }; }; diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index d05a10f..b8aef9d 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -650,7 +650,14 @@ void ImageViewer::applyBasicFilterWithOverflowHandling() { */ void ImageViewer::runCannyEdge(void) { //TODO - CannyEdgeMachine cem = CannyEdgeMachine(); + + // Blur with Gauss (1,2,1 2,4,2 1,2,1) (delta=3, otherwise bigger matrix) + + // THEN do x/y gradients with (-0.5, 0, 0.5) + + CannyEdgeMachine cem = CannyEdgeMachine(original, working_copy); + cem.setThresholdValues(0, 0); //TODO + cem.work(); } /**************************************************************************************** @@ -917,6 +924,8 @@ void ImageViewer::generateControlPanels() { run_canny_edge = new QPushButton("Run canny edge algorithm!"); QObject::connect(run_canny_edge, SIGNAL(clicked()), this, SLOT(runCannyEdge())); + task_tab5_scrolllayout->addWidget(run_canny_edge); + tabWidget->addTab(task_tab5_widget, "Task #5"); //Show it diff --git a/lazy_image.cpp b/lazy_image.cpp index 08adc57..701b6fa 100644 --- a/lazy_image.cpp +++ b/lazy_image.cpp @@ -298,8 +298,15 @@ class LazyImage { //std::cout << output_r << "," << output_g << "," << output_b << std::endl << std::endl; QColor result = QColor(output_r, output_g, output_b); return result; - }; - + }; + + int width(void) { + return this->img->width(); + }; + + int height(void) { + return this->img->height(); + }; };