Skip to content

Commit

Permalink
Revert "trying to optimize the line detector"
Browse files Browse the repository at this point in the history
This reverts commit d5c5c1d.
  • Loading branch information
ShaharMS committed Nov 18, 2022
1 parent 727de4e commit 12fcbfa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 64 deletions.
14 changes: 7 additions & 7 deletions compile.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

--js bin/main.js
--define feature_detection_tests
#--define draw_tests
#--define mirror_flip_tests
#--define blur_tests
#--define filter_tests
#--define convolve_tests
#--define simple_tests
#--define noise_tests
--define draw_tests
--define mirror_flip_tests
--define blur_tests
--define filter_tests
--define convolve_tests
--define simple_tests
--define noise_tests
4 changes: 0 additions & 4 deletions src/VisionMain.hx
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ class VisionMain {
printImage(image.clone().laplacianOfGaussianEdgeDetection());
end = haxe.Timer.stamp();
trace("Laplacian edge detection took: " + MathTools.truncate(end - start, 4) + " seconds");
start = haxe.Timer.stamp();
printImage(image.clone().sobelEdgeDiffOperator().blackAndWhite());
end = haxe.Timer.stamp();
trace("Eroded Perwitt edge detection took: " + MathTools.truncate(end - start, 4) + " seconds");
#end
});
ImageTools.loadFromFile("https://upload.wikimedia.org/wikipedia/commons/5/50/Vd-Orig.png", image ->
Expand Down
18 changes: 8 additions & 10 deletions src/vision/Vision.hx
Original file line number Diff line number Diff line change
Expand Up @@ -643,15 +643,14 @@ class Vision {
case VeryHigh_VerySlow: X9;
}
var edgeDetected = cannyEdgeDetection(image.clone().removeView(), 1, kernalSize, 0.05, 0.16);
SimpleLineDetector.image = edgeDetected.clone();
var lines:Array<Line2D> = [];
var actualLines:Array<Line2D> = [];
for (x in 0...image.width) {
for (y in 0...image.height) {
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, true));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, false, true));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, true, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, false, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, true, true));
}
}
for (l in lines) {
Expand All @@ -662,13 +661,12 @@ class Vision {
lines = [];
//now, get a mirrored version
edgeDetected = cannyEdgeDetection(image.clone().removeView().mirror(), 1, kernalSize, 0.05, 0.16);
SimpleLineDetector.image = edgeDetected.clone();
for (x in 0...image.width) {
for (y in 0...image.height) {
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, true));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, false, true));
lines.push(SimpleLineDetector.findLineFromPoint({x: x, y: y}, minLineLength, true, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, false, true));
lines.push(SimpleLineDetector.findLineFromPoint(edgeDetected, {x: x, y: y}, minLineLength, true, true));
}
}
for (l in lines) {
Expand Down
66 changes: 31 additions & 35 deletions src/vision/algorithms/SimpleLineDetector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@ using vision.tools.MathTools;
*/
class SimpleLineDetector {

public static var cachedPoints:Array<IntPoint2D> = [];

public static var image:Image;

public static function findLineFromPoint(point:IntPoint2D, minLineLength:Float, preferTTB:Bool = false, preferRTL:Bool = false):Line2D {
if (image.getUnsafePixel(point.x, point.y) == 0) return null;
public static var cachedPoints:Map<Int, Array<IntPoint2D>> = [];

public static function findLineFromPoint(image:Image, point:IntPoint2D, minLineLength:Float, preferTTB:Bool = false, preferRTL:Bool = false):Line2D {
//final rtl = preferRTL == true ? 1 : 0, ttb = preferTTB == true ? 1 : 0;
//if (cachedPoints[ttb | rtl << 1].contains(point)) return null;
final startX = point.x, startY = point.y;
final yArr = preferTTB ? [0, 1, 2] : [0, -1, -2];
final xArr = preferRTL ? [0, -1, -2] : [0, 1, 2];
if (image.getUnsafePixel(point.x, point.y) == 0) return null;

// now, were going to start looking for points around the point to find the entire line.
var prev:Null<IntPoint2D> = {x: 0, y: 0};
var prev:Null<IntPoint2D> = null;
var prev2:Null<IntPoint2D> = null;
var currentDirection:Null<IntPoint2D> = {x: 0, y: 0};
function expand() {
if (currentDirection != null && image.hasPixel(point.x + currentDirection.x, point.y + currentDirection.y) && image.getUnsafePixel(point.x + currentDirection.x, point.y + currentDirection.y).red == 255) {
if (currentDirection != null && image.hasPixel(point.x + currentDirection.x, point.y + currentDirection.y) && image.getPixel(point.x + currentDirection.x, point.y + currentDirection.y).red == 255) {
point.x = point.x + currentDirection.x;
point.y = point.y + currentDirection.y;
//cachedPoints[ttb | rtl << 1].push(point.copy());

// used to prevent infinite recursion
prev2 = {x: prev.x, y: prev.y};
prev = {x: point.x, y: point.y};

if (prev == null) {
prev = {x: point.x, y: point.y};
} else {
prev2 = {x: prev.x, y: prev.y};
prev = {x: point.x, y: point.y};
}
if ((if (preferTTB) currentDirection.y else currentDirection.x) == 0) {
if ((point.x == prev.x && point.y == prev.y) || (point.x == prev2.x && point.y == prev2.y)) {
return;
Expand All @@ -48,15 +52,19 @@ class SimpleLineDetector {
for (Y in yArr) {
if (X == 0 && Y == 0 || !image.hasPixel(point.x + X, point.y + Y))
continue;
if (image.getUnsafePixel(point.x + X, point.y + Y).red == 255) {
if (image.getPixel(point.x + X, point.y + Y).red == 255) {
currentDirection = {x: X, y: Y};
point.x = point.x + X;
point.y = point.y + Y;
//cachedPoints[ttb | rtl << 1].push(point.copy());

// used to prevent infinite recursion
prev2 = {x: prev.x, y: prev.y};
prev = {x: point.x, y: point.y};

if (prev == null) {
prev = {x: point.x, y: point.y};
} else {
prev2 = {x: prev.x, y: prev.y};
prev = {x: point.x, y: point.y};
}
if ((if (preferTTB) Y else X) == 0) {
if ((point.x == prev.x && point.y == prev.y) || (point.x == prev2.x && point.y == prev2.y)) {
return;
Expand Down Expand Up @@ -125,25 +133,13 @@ class SimpleLineDetector {
return (coveredPixels /*The biggest gap */- gapChecker.length) / totalPixels * 100;
}

static function depositPoints(x1:Float, y1:Float, x2:Float, y2:Float) {
var dx = Math.abs(x2 - x1);
var dy = Math.abs(y2 - y1);
var sx = (x1 < x2) ? 1 : -1;
var sy = (y1 < y2) ? 1 : -1;
var err = dx - dy;
while (true) {
cachedPoints.push({x: x1, y: y1});
if (x1 == x2 && y1 == y2)
break;
var e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}






public function new(image:Image) {

}
}
8 changes: 0 additions & 8 deletions src/vision/ds/IntPoint2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,4 @@ abstract IntPoint2D(Impl) {
public inline function copy() {
return new IntPoint2D(x, y);
}

@:op(A == B) public inline function equals(lhs:IntPoint2D):Bool {
return x == lhs.x && y == lhs.y;
}

@:op(A != B) inline function this_not_equals_IntPoint2D(lhs:IntPoint2D):Bool {
return x != lhs.x || y != lhs.y;
}
}

0 comments on commit 12fcbfa

Please sign in to comment.