[TASK] Switch to other bresenham implementation for now.

This commit is contained in:
Jan Philipp Timme 2016-01-08 13:10:59 +01:00
parent 4ce9e0e6c2
commit 85f2dd2824
2 changed files with 154 additions and 99 deletions

View File

@ -119,67 +119,67 @@ class HoughMachine {
}; };
void drawLineBresenham(int start_x, int start_y, int end_x, int end_y) {
this->bresenham_orientation_sector = this->getOrientationSector(end_x - start_x, end_y - start_y); void setPixelDebug(int x, int y, int color=0) {
// Now adapt input int our_color = QColor::fromRgb(0, 255, 0).rgb();
// Output will be adapted in setPixel() if(color == 0) color = our_color;
switch(this->bresenham_orientation_sector) { std::cout << " --> SET (" << x << ", " << y << ")" << std::endl;
case 0: this->working_copy->getImage()->setPixel(x, y, color);
// Going right, nothing to do here
this->bresenham(start_x, start_y, end_x, end_y);
break;
case 1:
// Going right, but more up
this->bresenham(start_y, start_x, end_y, end_x);
break;
case 2:
// Going left, but more up
this->bresenham(start_y, end_x, end_y, start_x);
break;
case 3:
// Going left
this->bresenham(end_x, start_y, start_x, end_y);
break;
case 4:
// Going left
this->bresenham(end_x, end_y, start_x, start_y);
break;
case 5:
// Going left, but more down
this->bresenham(end_y, end_x, start_y, start_x);
break;
case 6:
// Going right, but more down
this->bresenham(end_y, start_x, start_y, end_x);
break;
case 7:
// Going right
this->bresenham(start_x, end_y, end_x, start_y);
break;
}
}; };
void bresenham(int start_x, int start_y, int end_x, int end_y) { void otherBresenham(int x, int x2, int y, int y2) {
int x, y; int width = x2 - x;
int delta_x, delta_y; int height = y2 - y;
int d; int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
int delta_ne, delta_e; dx1 = (width < 0) ? -1 : 1;
x = start_x; dy1 = (height < 0) ? -1 : 1;
y = start_y; dx2 = (width < 0) ? -1 : 1;
delta_x = end_x - start_x; int longest = abs(width);
delta_y = end_y - start_y; int shortest = abs(height);
delta_ne = 2 * (delta_y - delta_x); if(longest <= shortest) {
delta_e = 2 * delta_y; longest = abs(height);
d = 2 * delta_y - delta_x; shortest = abs(width);
this->setPixel(start_x, start_y, x - start_x, y - start_y); dy2 = (height < 0) ? -1 : 1;
while(x < end_x) { dx2 = 0;
if(d >= 0) { }
d += delta_ne; x++; y++; int numerator = longest / 2;
} else { for(int i=0; i<=longest; i++) {
d += delta_e; x++; this->setPixelDebug(x, y);
numerator += shortest;
if(numerator >= longest) {
numerator -= longest;
x += dx1;
y += dy1;
} else {
x += dx2;
y += dy2;
}
}
};
void debugShow() {
std::cout << "Dimensions: " << this->n_ang << " x " << this->n_rad << " | " << std::endl;
int max_value = 0;
for(int i=0; i<this->n_ang*this->n_rad; i++) {
int current_value = this->hough_arr[i];
if(current_value > max_value) max_value = current_value;
}
for(int x=0; x<this->n_ang; x++) {
for(int y=0; y<this->n_rad; y++) {
int value = qRound(255.0*this->hough_arr[x*this->n_ang+y] / max_value);
if(value < 0) {
std::cout << value << std::endl;
value = 0;
}
if(value > 255) {
std::cout << value << std::endl;
value = 255;
}
int color = QColor::fromRgb(value, value, value).rgb();
this->working_copy->getImage()->setPixel(x,y,color);
} }
this->setPixel(start_x, start_y, x - start_x, y - start_y);
} }
}; };
@ -208,72 +208,121 @@ class HoughMachine {
if(new_dx < 0 && -new_dx >= new_dy) orientation_sector += 3; if(new_dx < 0 && -new_dx >= new_dy) orientation_sector += 3;
return orientation_sector; return orientation_sector;
}; };
void drawLineBresenham(int start_x, int start_y, int end_x, int end_y) {
this->bresenham_orientation_sector = this->getOrientationSector(end_x - start_x, end_y - start_y);
std::cout << "START LINE: (" << start_x << ", " << start_y << ") -> (" << end_x << ", " << end_y << ") - O: " << this->bresenham_orientation_sector << std::endl;
// other implementation for now.
this->otherBresenham(start_x, end_x, start_y, end_y);
// Now adapt input
// Output will be adapted in setPixel()
/*switch(this->bresenham_orientation_sector) {
case 0:
// Going right, nothing to do here
this->bresenham(start_x, start_y, end_x, end_y);
break;
case 1:
// Going right, but more up
this->bresenham(start_y, start_x, end_y, end_x);
break;
case 2:
// Going left, but more up
this->bresenham(start_y, end_x, end_y, start_x);
break;
case 3:
// Going left
this->bresenham(end_x, start_y, start_x, end_y);
break;
case 4:
// Going left
this->bresenham(end_x, end_y, start_x, start_y);
break;
case 5:
// Going left, but more down
this->bresenham(end_y, end_x, start_y, start_x);
break;
case 6:
// Going right, but more down
this->bresenham(start_x, end_y, end_x, start_y);
break;
case 7:
// Going right
this->bresenham(start_x, end_y, end_x, start_y);
break;
}*/
};
void setPixel(int start_x, int start_y, int dx, int dy) { /*void bresenham(int start_x, int start_y, int end_x, int end_y) {
std::cout << "BRESENHAM START LINE: (" << start_x << ", " << start_y << ") -> (" << end_x << ", " << end_y << ")" << std::endl;
int x, y;
int delta_x, delta_y;
int d;
int delta_ne, delta_e;
x = start_x;
y = start_y;
delta_x = end_x - start_x;
delta_y = end_y - start_y;
delta_ne = 2 * (delta_y - delta_x);
delta_e = 2 * delta_y;
d = 2 * delta_y - delta_x;
this->setPixel(start_x, start_y, x, y);
while(x < end_x) {
if(d >= 0) {
d += delta_ne; x++; y++;
} else {
d += delta_e; x++;
}
this->setPixel(start_x, start_y, x, y);
}
std::cout << std::endl;
};*/
/*void setPixel(int start_x, int start_y, int dx, int dy) {
std::cout << "setPixel(" << start_x << ", " << start_y << ", " << dx << ", " << dy << ")" << std::endl;
if(dx > start_x) dx = dx - start_x;
else dx = start_x - dx;
if(dy > start_y) dy = dy - start_y;
else dy = start_y - dy;
std::cout << " --> dx: " << dx << ", dy: " << dy << "" << std::endl;
int color = QColor::fromRgb(0, 255, 0).rgb(); int color = QColor::fromRgb(0, 255, 0).rgb();
//TODO: Fix this! This has to happen relative to the center point! :| //TODO: Fix this! This has to happen relative to the center point! :|
switch(this->bresenham_orientation_sector) { switch(this->bresenham_orientation_sector) {
case 0: case 0:
// Going right, nothing to do here // Going right, nothing to do here
this->working_copy->getImage()->setPixel(start_x + dx, start_y + dy, color); this->setPixelDebug(start_x + dx, start_y + dy, color);
break; break;
case 1: case 1:
// Going right, but more up // Going right, but more up
this->working_copy->getImage()->setPixel(start_x + dy, start_y + dx, color); this->setPixelDebug(start_x + dy, start_y + dx, color);
break; break;
case 2: case 2:
// Going left, but more up // Going left, but more up
this->working_copy->getImage()->setPixel(start_x - dy, start_y + dx, color); this->setPixelDebug(start_x - dy, start_y + dx, color);
break; break;
case 3: case 3:
// Going left // Going left
this->working_copy->getImage()->setPixel(start_x - dx, start_y + dy, color); this->setPixelDebug(start_x - dx, start_y + dy, color);
break; break;
case 4: case 4:
// Going left // Going left
this->working_copy->getImage()->setPixel(start_x - dx, start_y - dy, color); this->setPixelDebug(start_x - dx, start_y - dy, color);
break; break;
case 5: case 5:
// Going left, but more down // Going left, but more down
this->working_copy->getImage()->setPixel(start_x - dy, start_y - dx, color); this->setPixelDebug(start_x + dy, start_y + dx, color); // ok
break; break;
case 6: case 6:
// Going right, but more down // Going right, but more down
this->working_copy->getImage()->setPixel(start_x + dy, start_y - dx, color); this->setPixelDebug(start_x + dy, start_y - dx, color); // ?
break; break;
case 7: case 7:
// Going right // Going right
this->working_copy->getImage()->setPixel(start_x + dx, start_y - dy, color); this->setPixelDebug(start_x + dx, start_y - dy, color);
break; break;
} }
}; };*/
void debugShow() {
std::cout << "Dimensions: " << this->n_ang << " x " << this->n_rad << " | " << std::endl;
int max_value = 0;
for(int i=0; i<this->n_ang*this->n_rad; i++) {
int current_value = this->hough_arr[i];
if(current_value > max_value) max_value = current_value;
}
for(int x=0; x<this->n_ang; x++) {
for(int y=0; y<this->n_rad; y++) {
int value = qRound(255.0*this->hough_arr[x*this->n_ang+y] / max_value);
if(value < 0) {
std::cout << value << std::endl;
value = 0;
}
if(value > 255) {
std::cout << value << std::endl;
value = 255;
}
int color = QColor::fromRgb(value, value, value).rgb();
this->working_copy->getImage()->setPixel(x,y,color);
}
}
};
}; };

View File

@ -140,11 +140,17 @@ void ImageViewer::drawBlackLine() {
working_copy->getImage()->setPixel(i,i,0); working_copy->getImage()->setPixel(i,i,0);
}*/ }*/
HoughMachine* hm = new HoughMachine(original, working_copy); HoughMachine* hm = new HoughMachine(original, working_copy);
hm->drawLineBresenham(10, 10, 20, 20); //star centered around 20/20
hm->drawLineBresenham(10, 10, 20, 10); hm->drawLineBresenham(20, 20, 10, 10);
hm->drawLineBresenham(10, 10, 10, 20); hm->drawLineBresenham(20, 20, 20, 10);
hm->drawLineBresenham(10, 10, 15, 20); hm->drawLineBresenham(20, 20, 30, 10);
hm->drawLineBresenham(10, 10, 20, 15); hm->drawLineBresenham(20, 20, 30, 20);
hm->drawLineBresenham(20, 20, 30, 30);
hm->drawLineBresenham(20, 20, 20, 30);
hm->drawLineBresenham(20, 20, 10, 30);
hm->drawLineBresenham(20, 20, 10, 20);
delete hm; delete hm;
updateImageDisplay(); updateImageDisplay();
logFile << "Black line drawn." << std::endl; logFile << "Black line drawn." << std::endl;