2015-12-18 13:16:13 +01:00
|
|
|
#ifndef CANNY_EDGE_MACHINE_C
|
|
|
|
#define CANNY_EDGE_MACHINE_C
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QMainWindow>
|
|
|
|
#include <QColor>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <math.h>
|
2015-12-18 14:18:36 +01:00
|
|
|
#include "lazy_image.cpp"
|
2015-12-18 13:16:13 +01:00
|
|
|
|
|
|
|
class CannyEdgeMachine {
|
|
|
|
|
|
|
|
private:
|
2015-12-18 14:06:09 +01:00
|
|
|
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;
|
|
|
|
|
2015-12-18 13:16:13 +01:00
|
|
|
public:
|
2015-12-18 14:06:09 +01:00
|
|
|
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);
|
2015-12-18 13:16:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
~CannyEdgeMachine() {
|
2015-12-18 14:06:09 +01:00
|
|
|
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; i<this->pixels; 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; i<filter_width; i++) {
|
|
|
|
for(int j=0; j<filter_width; j++) {
|
|
|
|
filter[i*filter_width + j] = pow(2, i) * pow(2, j);
|
|
|
|
sum_weights += filter[i*filter_width + j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// apply gauss filter
|
|
|
|
int filter_offset = (filter_width+1)/2;
|
2015-12-18 14:18:36 +01:00
|
|
|
for(int x=(0+filter_offset); x<(this->width-filter_offset); x++) {
|
|
|
|
for(int y=(0+filter_offset); y<(this->height-filter_offset); y++) {
|
2015-12-18 14:06:09 +01:00
|
|
|
int sum_intensity = 0;
|
|
|
|
int h, s, l;
|
|
|
|
for(int fx=0; fx<filter_width; fx++) {
|
|
|
|
int dx = fx - (filter_width / 2);
|
|
|
|
for(int fy=0; fy<filter_width; fy++) {
|
|
|
|
int dy = fy - (filter_width / 2);
|
2015-12-18 14:18:36 +01:00
|
|
|
QColor color = QColor::fromRgb(this->original->getPixel(x+dx, y+dy, LazyImage::DEFAULT));
|
2015-12-18 14:06:09 +01:00
|
|
|
color.getHsl(&h, &s, &l);
|
|
|
|
sum_intensity += (l * filter[fy*filter_width + fx]);
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 14:18:36 +01:00
|
|
|
QColor color = QColor::fromRgb(this->original->getPixel(x, y, LazyImage::DEFAULT));
|
2015-12-18 14:06:09 +01:00
|
|
|
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();
|
2015-12-18 14:18:36 +01:00
|
|
|
this->doGaussBlur();
|
|
|
|
/*for(int x=0; x<this->width; x++) {
|
2015-12-18 14:06:09 +01:00
|
|
|
for(int y=0; y<this->height; y++) {
|
|
|
|
//this->gradient_magnitude = sqrt(...)
|
|
|
|
}
|
2015-12-18 14:18:36 +01:00
|
|
|
}*/
|
2015-12-18 14:06:09 +01:00
|
|
|
|
2015-12-18 13:16:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|