[TASK] Implement generic gaussian blur filter.
This commit is contained in:
parent
bd07eb280c
commit
55b07d48cb
|
@ -12,14 +12,108 @@
|
||||||
class CannyEdgeMachine {
|
class CannyEdgeMachine {
|
||||||
|
|
||||||
private:
|
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:
|
public:
|
||||||
CannyEdgeMachine(){
|
CannyEdgeMachine(LazyImage* original, LazyImage* working_copy){
|
||||||
// TODO
|
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() {
|
~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; 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;
|
||||||
|
for(int x=0+filter_offset; x<this->width-filter_offset; x++) {
|
||||||
|
for(int y=0+filter_offset; y<this->width-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->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; x<this->width; x++) {
|
||||||
|
for(int y=0; y<this->height; y++) {
|
||||||
|
//this->gradient_magnitude = sqrt(...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -650,7 +650,14 @@ void ImageViewer::applyBasicFilterWithOverflowHandling() {
|
||||||
*/
|
*/
|
||||||
void ImageViewer::runCannyEdge(void) {
|
void ImageViewer::runCannyEdge(void) {
|
||||||
//TODO
|
//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!");
|
run_canny_edge = new QPushButton("Run canny edge algorithm!");
|
||||||
QObject::connect(run_canny_edge, SIGNAL(clicked()), this, SLOT(runCannyEdge()));
|
QObject::connect(run_canny_edge, SIGNAL(clicked()), this, SLOT(runCannyEdge()));
|
||||||
|
|
||||||
|
task_tab5_scrolllayout->addWidget(run_canny_edge);
|
||||||
|
|
||||||
tabWidget->addTab(task_tab5_widget, "Task #5");
|
tabWidget->addTab(task_tab5_widget, "Task #5");
|
||||||
|
|
||||||
//Show it
|
//Show it
|
||||||
|
|
|
@ -298,8 +298,15 @@ class LazyImage {
|
||||||
//std::cout << output_r << "," << output_g << "," << output_b << std::endl << std::endl;
|
//std::cout << output_r << "," << output_g << "," << output_b << std::endl << std::endl;
|
||||||
QColor result = QColor(output_r, output_g, output_b);
|
QColor result = QColor(output_r, output_g, output_b);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int width(void) {
|
||||||
|
return this->img->width();
|
||||||
|
};
|
||||||
|
|
||||||
|
int height(void) {
|
||||||
|
return this->img->height();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue