[BUGFIX] Fixed a segfault, fixed bad initialization of malloc'd space.

This commit is contained in:
Jan Philipp Timme 2015-12-18 15:00:05 +01:00
parent 9c3cceef31
commit 0ba9cdd25c
1 changed files with 35 additions and 13 deletions

View File

@ -60,11 +60,11 @@ class CannyEdgeMachine {
void reset(void) { void reset(void) {
for(int i=0; i<this->pixels; i++) { for(int i=0; i<this->pixels; i++) {
this->gradient_magnitude = 0; this->gradient_magnitude[i] = 0;
this->maximum_magnitude = 0; this->maximum_magnitude[i] = 0;
this->binary_edge_pixels = 0; this->binary_edge_pixels[i] = 0;
this->gradient_x = 0; this->gradient_x[i] = 0;
this->gradient_y = 0; this->gradient_y[i] = 0;
} }
}; };
@ -112,39 +112,61 @@ class CannyEdgeMachine {
gradiant_vector[0] = -0.5; gradiant_vector[0] = -0.5;
gradiant_vector[1] = 0; gradiant_vector[1] = 0;
gradiant_vector[2] = 0.5; gradiant_vector[2] = 0.5;
int gradiant_offset = gradiant_size; int gradiant_offset = gradiant_size/2;
// calculate gradiants // calculate gradiants
double sum_intensity;
for(int x=(0+gradiant_offset); x<(this->width-gradiant_offset); x++) { for(int x=(0+gradiant_offset); x<(this->width-gradiant_offset); x++) {
for(int y=(0+gradiant_offset); y<(this->height-gradiant_offset); y++) { for(int y=(0+gradiant_offset); y<(this->height-gradiant_offset); y++) {
int h, s, l; int h, s, l;
// x gradiant // x gradiant
double sum_intensity = 0; sum_intensity = 0;
for(int i=0; i<gradiant_size; i++) { for(int i=0; i<gradiant_size; i++) {
int dx = i - gradiant_size/2; int dx = i - gradiant_offset;
QColor color = QColor::fromRgb(this->original->getPixel(x+dx, y, LazyImage::DEFAULT)); QColor color = QColor::fromRgb(this->original->getPixel(x+dx, y, LazyImage::DEFAULT));
color.getHsl(&h, &s, &l); color.getHsl(&h, &s, &l);
sum_intensity += l * gradiant_vector[i]; sum_intensity += l * gradiant_vector[i];
} }
this->gradient_x[y*width + x] = sum_intensity; this->gradient_x[y*this->width + x] = sum_intensity;
// y gradiant // y gradiant
double sum_intensity = 0; sum_intensity = 0;
for(int i=0; i<gradiant_size; i++) { for(int i=0; i<gradiant_size; i++) {
int dy = i - gradiant_size/2; int dy = i - gradiant_offset;
QColor color = QColor::fromRgb(this->original->getPixel(x, y+dy, LazyImage::DEFAULT)); QColor color = QColor::fromRgb(this->original->getPixel(x, y+dy, LazyImage::DEFAULT));
color.getHsl(&h, &s, &l); color.getHsl(&h, &s, &l);
sum_intensity += l * gradiant_vector[i]; sum_intensity += l * gradiant_vector[i];
} }
this->gradient_y[y*width + x] = sum_intensity; this->gradient_y[y*this->width + x] = sum_intensity;
} }
} }
free(gradiant_vector); 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));
}
}
};
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];
}
}
};
void work() { void work() {
this->reset(); this->reset();
this->doGaussBlur(); this->doGaussBlur();
this->doGradiants(); this->doGradiants();
this->doGradiantMagnitude();
this->filterLocalMaxima();
// TraceAndThreshold
}; };
}; };