CG2_Tasks/canny_edge_machine.cpp

285 lines
8.9 KiB
C++

#ifndef CANNY_EDGE_MACHINE_C
#define CANNY_EDGE_MACHINE_C
#include <QtGlobal>
#include <QMainWindow>
#include <QColor>
#include <iostream>
#include <math.h>
#include "lazy_image.cpp"
class CannyEdgeMachine {
private:
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
int filter_size;
double t_low;
double t_high;
public:
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() {
free(this->gradient_magnitude);
free(this->maximum_magnitude);
free(this->binary_edge_pixels);
free(this->gradient_x);
free(this->gradient_y);
};
void setGaussFilterSize(int filter_size) {
this->filter_size = filter_size;
};
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[i] = 0;
this->maximum_magnitude[i] = 0;
this->binary_edge_pixels[i] = 0;
this->gradient_x[i] = 0;
this->gradient_y[i] = 0;
}
};
void doGaussBlur(int filter_width) {
// build the gauss filter
int* filter = (int*) malloc(sizeof(int) * filter_width * filter_width);
int sum_weights = 0;
int filter_middle = (filter_width)/2;
for(int x=0; x<filter_width; x++) {
int i = x;
if(x > filter_middle) i = filter_middle - (x - filter_middle);
for(int y=0; y<filter_width; y++) {
int j = y;
if(y > filter_middle) j = filter_middle - (y - filter_middle);
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;
for(int x=(0+filter_offset); x<(this->width-filter_offset); x++) {
for(int y=(0+filter_offset); y<(this->height-filter_offset); y++) {
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);
QColor color = QColor::fromRgb(this->original->getPixel(x+dx, y+dy, LazyImage::DEFAULT));
color.getHsl(&h, &s, &l);
sum_intensity += (l * filter[fy*filter_width + fx]);
}
}
QColor color = QColor::fromRgb(this->original->getPixel(x, y, LazyImage::DEFAULT));
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 doGradiants(void) {
// build the gradiant vector
int gradiant_size = 3;
double* gradiant_vector = (double*) malloc(sizeof(double) * gradiant_size);
gradiant_vector[0] = -0.5;
gradiant_vector[1] = 0;
gradiant_vector[2] = 0.5;
int gradiant_offset = gradiant_size/2;
// calculate gradiants
double sum_intensity;
for(int x=(0+gradiant_offset); x<(this->width-gradiant_offset); x++) {
for(int y=(0+gradiant_offset); y<(this->height-gradiant_offset); y++) {
int h, s, l;
// x gradiant
sum_intensity = 0;
for(int i=0; i<gradiant_size; i++) {
int dx = i - gradiant_offset;
QColor color = QColor::fromRgb(this->original->getPixel(x+dx, y, LazyImage::DEFAULT));
color.getHsl(&h, &s, &l);
sum_intensity += l * gradiant_vector[i];
}
this->gradient_x[y*this->width + x] = sum_intensity;
// y gradiant
sum_intensity = 0;
for(int i=0; i<gradiant_size; i++) {
int dy = i - gradiant_offset;
QColor color = QColor::fromRgb(this->original->getPixel(x, y+dy, LazyImage::DEFAULT));
color.getHsl(&h, &s, &l);
sum_intensity += l * gradiant_vector[i];
}
this->gradient_y[y*this->width + x] = sum_intensity;
}
}
free(gradiant_vector);
};
void doGradiantMagnitude(void) {
for(int x=0; x<this->width; x++) {
for(int y=0; y<this->height; y++) {
int gradiant_x = this->gradient_x[y*this->width + x];
int gradiant_y = this->gradient_y[y*this->width + x];
this->gradient_magnitude[y*this->width + x] = sqrt(pow(gradiant_x, 2) + pow(gradiant_y, 2));
}
}
};
int getOrientationSector(double dx, double dy) {
// Matrix multiplication with rotation matrix pi/8
//
// cos(pi/8) -sin(pi/8)
// sin(pi/8) cos(pi/8)
double octangle = 3.141592/8; // I know ...
double cosoct = cos(octangle);
double sinoct = sin(octangle);
double neg_sinoct = -sinoct;
// Do matrix multiplication
double new_dx = dx * cosoct + dy * neg_sinoct;
double new_dy = dx * sinoct + dy * cosoct;
if(new_dy < 0) {
new_dx = -new_dx;
new_dy = -new_dy;
}
int orientation_sector;
if(new_dx >= 0 && new_dx >= new_dy) orientation_sector = 0;
if(new_dx >= 0 && new_dx < new_dy) orientation_sector = 1;
if(new_dx < 0 && -new_dx < new_dy) orientation_sector = 2;
if(new_dx < 0 && -new_dy >= new_dy) orientation_sector = 3;
return orientation_sector;
};
bool isLocalMax(int x, int y, int orientation_sector) {
double local_magnitude = this->gradient_magnitude[y * this->width + x];
if(local_magnitude < this->t_low) {
return false;
} else {
int magnitude_l, magnitude_r;
switch(orientation_sector) {
case 0:
magnitude_l = this->gradient_magnitude[y * this->width + (x-1)];
magnitude_r = this->gradient_magnitude[y * this->width + (x+1)];
break;
case 1:
magnitude_l = this->gradient_magnitude[(y-1) * this->width + (x-1)];
magnitude_r = this->gradient_magnitude[(y+1) * this->width + (x+1)];
break;
case 2:
magnitude_l = this->gradient_magnitude[(y-1) * this->width + x];
magnitude_r = this->gradient_magnitude[(y+1) * this->width + x];
break;
case 3:
magnitude_l = this->gradient_magnitude[(y-1) * this->width + (x+1)];
magnitude_r = this->gradient_magnitude[(y+1) * this->width + (x-1)];
break;
}
return ((magnitude_l <= local_magnitude) && (local_magnitude > magnitude_r));
}
};
void filterLocalMaxima(void) {
for(int x=1; x<this->width-2; x++) {
for(int y=1; y<this->height-2; y++) {
double dx = this->gradient_x[y*this->width + x];
double dy = this->gradient_y[y*this->width + x];
// get orientation sector
int orientation_sector = this->getOrientationSector(dx, dy);
if(this->isLocalMax(x, y, orientation_sector)) {
this->maximum_magnitude[y*this->width + x] = this->gradient_magnitude[y*this->width + x];
}
}
}
};
void traceAndThreshold(int x, int y) {
this->binary_edge_pixels[y*this->width + x] = 1;
int x_l = std::max(x-1, 0);
int x_r = std::min(x+1, this->width-1);
int y_l = std::max(y-1, 0);
int y_r = std::min(y+1, this->height-1);
for(int x=x_l; x<=x_r; x++) {
for(int y=y_l; y<y_r; y++) {
if((this->maximum_magnitude[y*this->width + x] >= this->t_high)
&& (this->binary_edge_pixels[y*this->width + x] == 0)) {
this->traceAndThreshold(x, y);
}
}
}
};
void workLocalMaxima(void) {
for(int x=1; x<this->width-2; x++) {
for(int y=1; y<this->height-2; y++) {
if((this->maximum_magnitude[y*this->width + x] >= this->t_high)
&& (this->binary_edge_pixels[y*this->width + x] == 0)) {
this->traceAndThreshold(x, y);
}
}
}
};
void showEdges(void) {
QRgb black = QColor::fromRgb(0, 0, 0).rgb();
QRgb white = QColor::fromRgb(255, 255, 255).rgb();
for(int x=0; x<this->width; x++) {
for(int y=0; y<this->height; y++) {
int pixel = this->binary_edge_pixels[y*this->width + x];
if(pixel > 0) {
this->working_copy->getImage()->setPixel(x, y, white);
} else {
this->working_copy->getImage()->setPixel(x, y, black);
}
}
}
};
void work() {
this->reset();
this->doGaussBlur(this->filter_size);
this->doGradiants();
this->doGradiantMagnitude();
this->filterLocalMaxima();
this->workLocalMaxima();
this->showEdges();
};
};
#endif