If you are doing line detection on camera images, then you want the OpenCV for Processing library.
Use a Hough transform for line detection:
PDE > File > Examples > Contributed Libraries > OpenCV for Processing > HoughLineDetection
If for example your input image was this:
Then a slight modification of that demo sketch:
import gab.opencv.*;
OpenCV opencv;
PImage src;
ArrayList<Line> lines;
void setup() {
size(900, 300);
stroke(0,0,255);
src = loadImage("red-lines.png");
src.resize(0, 300);
opencv = new OpenCV(this, src);
opencv.findCannyEdges(20, 75);
// Find lines with Hough line detection
// Arguments are: threshold, minLengthLength, maxLineGap
lines = opencv.findLines(100, 30, 20);
println(lines);
}
void draw() {
background(255);
// original image on left
image(src,0,0);
// edge detection pixel data in center (white-on-black)
image(opencv.getOutput(), 300, 0);
// rendering of detected line data on right (blue)
translate(600,0);
// lines.angle gives the angle in radians, measured in double precision
// lines also have "start" and "end" PVectors with the x,y position.
for (Line line : lines) {
line(line.start.x, line.start.y, line.end.x, line.end.y);
}
}
…will give you output like this:
Now you can use line-line collision detection, e.g. this example from Thompson:
boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
// calculate the distance to intersection point
float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
// optionally, draw a circle where the lines meet
float intersectionX = x1 + (uA * (x2-x1));
float intersectionY = y1 + (uA * (y2-y1));
fill(255,0,0);
noStroke();
ellipse(intersectionX,intersectionY, 20,20);
return true;
}
return false;
}
…to find if your lines intersect. Rather than returning true, you could instead return a PVector with the point of intersection.