Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ public class HoughTransformation {
private int height;

public HoughTransformation(int width, int height) {
this.maxPoint = null;
this.bitmap = new float[width][height];
maxPoint = null;
bitmap = new float[width][height];
this.width = width;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make width and height final. I know this wasn't the aim of your PR, but it will be way easier to reason about all future changes this way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, bitmap.

this.height = height;
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
this.bitmap[x][y] = 0;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bitmap[x][y] = 0;
}
}
}

public void addLine(int x, int y, float brightness) {
// move the coordinate system to the center: -1 .. 1, -1 .. 1
float xf = ((2 * ((float) x)) / this.width) - 1;
float yf = ((2 * ((float) y)) / this.height) - 1;
for (int a = 0; a < this.width; a++) {
float xf = ((2 * ((float) x)) / width) - 1;
float yf = ((2 * ((float) y)) / height) - 1;
for (int a = 0; a < width; a++) {
// move a to the center
float af = ((2 * ((float) a)) / this.width) - 1;
float af = ((2 * ((float) a)) / width) - 1;
// compute b
float bf = yf - (af * xf);
// move b to the center of the original coordinate system
int b = (int) (((bf + 1) * this.height) / 2);
if ((0 < b) && (b < (this.height - 1))) {
this.bitmap[a][b] += brightness;
int b = (int) (((bf + 1) * height) / 2);
if ((0 < b) && (b < (height - 1))) {
bitmap[a][b] += brightness;
}
}
}

private Point computeMaxPoint() {
float max = 0;
int maxX = 0, maxY = 0;
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
float curr = this.bitmap[x][y];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
float curr = bitmap[x][y];
if (curr >= max) {
maxX = x;
maxY = y;
Expand All @@ -78,29 +78,29 @@ private Point computeMaxPoint() {
}

public Point getMaxPoint() {
if (this.maxPoint == null) {
this.maxPoint = this.computeMaxPoint();
if (maxPoint == null) {
maxPoint = computeMaxPoint();
}
return this.maxPoint;
return maxPoint;
}

private float getAverageValue() {
float sum = 0;
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
sum += this.bitmap[x][y];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
sum += bitmap[x][y];
}
}
return sum / (this.width * this.height);
return sum / (width * height);
}

public BufferedImage render(int renderType, int colorType) {
float average = this.getAverageValue();
BufferedImage output = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
float average = getAverageValue();
BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = output.createGraphics();
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
int value = (int) ((255 * this.bitmap[x][y]) / average / 3);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int value = (int) ((255 * bitmap[x][y]) / average / 3);
value = Math.max(0, Math.min(value, 255));
if (colorType == HoughTransformation.COLOR_BW) {
output.setRGB(x, y, new Color(value, value, value).getRGB());
Expand All @@ -109,26 +109,26 @@ public BufferedImage render(int renderType, int colorType) {
}
}
}
this.maxPoint = this.computeMaxPoint();
maxPoint = computeMaxPoint();
g.setColor(Color.ORANGE);
float a = ((2 * ((float) this.maxPoint.x)) / this.width) - 1;
float b = ((2 * ((float) this.maxPoint.y)) / this.height) - 1;
float a = ((2 * ((float) maxPoint.x)) / width) - 1;
float b = ((2 * ((float) maxPoint.y)) / height) - 1;
float x0f = -1;
float y0f = (a * x0f) + b;
float x1f = 1;
float y1f = (a * x1f) + b;
int y0 = (int) (((y0f + 1) * this.height) / 2);
int y1 = (int) (((y1f + 1) * this.height) / 2);
int dx = this.width;
int y0 = (int) (((y0f + 1) * height) / 2);
int y1 = (int) (((y1f + 1) * height) / 2);
int dx = width;
int dy = y1 - y0;
this.dx = dx;
this.dy = dy;
this.angle = (float) ((180 * Math.atan(this.dy / this.dx)) / Math.PI);
angle = (float) ((180 * Math.atan(dy / dx)) / Math.PI);
if (renderType == HoughTransformation.RENDER_ALL) {
g.drawOval(this.maxPoint.x - 5, this.maxPoint.y - 5, 10, 10);
g.drawLine(0, (this.height / 2) - (dy / 2) - 1, this.width, ((this.height / 2) + (dy / 2)) - 1);
g.drawLine(0, ((this.height / 2) - (dy / 2)) + 0, this.width, (this.height / 2) + (dy / 2) + 0);
g.drawLine(0, ((this.height / 2) - (dy / 2)) + 1, this.width, (this.height / 2) + (dy / 2) + 1);
g.drawOval(maxPoint.x - 5, maxPoint.y - 5, 10, 10);
g.drawLine(0, (height / 2) - (dy / 2) - 1, width, ((height / 2) + (dy / 2)) - 1);
g.drawLine(0, ((height / 2) - (dy / 2)) + 0, width, (height / 2) + (dy / 2) + 0);
g.drawLine(0, ((height / 2) - (dy / 2)) + 1, width, (height / 2) + (dy / 2) + 1);
}
return output;
}
Expand Down