|
| 1 | +import gab.opencv.*; |
| 2 | +import org.opencv.imgproc.Imgproc; |
| 3 | +import org.opencv.core.Mat; |
| 4 | +import java.awt.geom.Line2D; |
| 5 | + |
| 6 | +OpenCV opencv; |
| 7 | + |
| 8 | +ArrayList<Line2D> lines; |
| 9 | + |
| 10 | +void setup() { |
| 11 | + PImage src = loadImage("film_scan.jpg"); |
| 12 | + src.resize(0, 800); |
| 13 | + size(src.width, src.height, P2D); |
| 14 | + |
| 15 | + opencv = new OpenCV(this, src); |
| 16 | + opencv.findSobelEdges(1, 0); |
| 17 | + opencv.threshold(20); |
| 18 | + |
| 19 | + lines = findLines(opencv.getGray(), 200, 120, 5); |
| 20 | + |
| 21 | + for(Line2D l : lines){ |
| 22 | + println(Math.toDegrees(angleOf(l))); |
| 23 | + } |
| 24 | + |
| 25 | + noLoop(); |
| 26 | +} |
| 27 | + |
| 28 | +double angleOf(Line2D l){ |
| 29 | + Line2D vertical = new Line2D.Double(0,0, 0,1); |
| 30 | + |
| 31 | + return angleBetween(l, vertical); |
| 32 | +} |
| 33 | + |
| 34 | +double angleBetween(Line2D line1, Line2D line2) { |
| 35 | + double angle1 = Math.atan2(line1.getY1() - line1.getY2(), |
| 36 | + line1.getX1() - line1.getX2()); |
| 37 | + double angle2 = Math.atan2(line2.getY1() - line2.getY2(), |
| 38 | + line2.getX1() - line2.getX2()); |
| 39 | + return angle1-angle2; |
| 40 | +} |
| 41 | + |
| 42 | +ArrayList<Line2D> findLines(Mat m, int threshold, double minLength, double maxGap) { |
| 43 | + ArrayList<Line2D> result = new ArrayList<Line2D>(); |
| 44 | + |
| 45 | + Mat lineMat = new Mat(); |
| 46 | + Imgproc.HoughLinesP(m, lineMat, 1, PI/180.0, threshold, minLength, maxGap); |
| 47 | + for (int i = 0; i < lineMat.width(); i++) { |
| 48 | + double[] coords = lineMat.get(0, i); |
| 49 | + result.add(new Line2D.Double(coords[0], coords[1], coords[2], coords[3])); |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | +} |
| 54 | + |
| 55 | +void draw() { |
| 56 | + image(opencv.getOutput(), 0, 0); |
| 57 | + stroke(0, 255, 0); |
| 58 | + strokeWeight(2); |
| 59 | + for (Line2D line : lines) { |
| 60 | + line((float)line.getX1(), (float)line.getY1(), (float)line.getX2(), (float)line.getY2()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
0 commit comments