-
-
Save matthewepler/5449282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gab.opencvpro.*; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.CvType; | |
import org.opencv.core.MatOfPoint; | |
import org.opencv.core.Point; | |
import java.awt.Rectangle; | |
OpenCVPro opencv; | |
PImage src, dst; | |
int searchColumn = 70; | |
int distanceBetweenSprockets = 32; | |
ArrayList<MatOfPoint> contours; | |
class Sprocket{ | |
int top; | |
int bottom; | |
Sprocket(int top, int bottom){ | |
this.top = top; | |
this.bottom = bottom; | |
} | |
} | |
ArrayList<Sprocket> sprockets; | |
Rectangle roi; | |
void setup() { | |
src = loadImage("_MG_9847.JPG"); | |
src.resize(500, 0); | |
size(src.width*2, src.height); | |
roi = new Rectangle(0, 270, src.width, 300); | |
opencv = new OpenCVPro(this, src.width, src.height); | |
opencv.loadImage(src); | |
opencv.gray(); | |
Mat grayMat = opencv.getBufferGray(); | |
Mat roiMat = grayMat.submat(roi.y, roi.y+roi.height, roi.x, roi.x+roi.width ); | |
opencv.setBufferGray(roiMat); | |
opencv.findSobelEdges(0, 2); | |
opencv.threshold(50); | |
dst = createImage(src.width, src.height, ARGB); | |
opencv.toPImage(opencv.getBufferGray(), dst); | |
ArrayList<Integer> sprocketEdges = new ArrayList<Integer>(); | |
for (int row = 1; row < dst.height; row++) { | |
int prevI = searchColumn + (row-1)*dst.width; | |
int thisI = searchColumn + row*dst.width; | |
boolean thisRow = brightness(dst.pixels[thisI]) > 0 ; | |
boolean prevRow = brightness(dst.pixels[prevI]) > 0; | |
if (thisRow && !prevRow) { | |
sprocketEdges.add(row); | |
} | |
} | |
sprockets = new ArrayList<Sprocket>(); | |
// loop over sprocketEdges and determine pairs | |
for(int i = 1; i < sprocketEdges.size(); i++){ | |
int prev = sprocketEdges.get(i-1); | |
int curr = sprocketEdges.get(i); | |
if(abs(curr - prev) < distanceBetweenSprockets){ | |
sprockets.add(new Sprocket(prev, curr)); | |
} | |
} | |
// println(sprocketTops.size()); | |
} | |
void drawContours(ArrayList<MatOfPoint> cntrs) { | |
for (MatOfPoint contour : cntrs) { | |
beginShape(); | |
Point[] points = contour.toArray(); | |
for (int i = 0; i < points.length; i++) { | |
vertex((float)points[i].x, (float)points[i].y); | |
} | |
endShape(); | |
} | |
} | |
void draw() { | |
image(src, 0, 0); | |
image(dst, src.width, 0); | |
//translate(src.width, 0); | |
noFill(); | |
strokeWeight(4); | |
stroke(255, 0, 0); | |
rect(roi.x, roi.y, roi.width, roi.height); | |
strokeWeight(1); | |
stroke(0, 0, 255); | |
translate(roi.x, roi.y); | |
line(searchColumn, 0, searchColumn, src.height); | |
for(Sprocket sprocket : sprockets){ | |
stroke(0, 255, 0); | |
line(0, sprocket.top, src.width, sprocket.top); | |
stroke(255, 0, 0); | |
line(0, sprocket.bottom, src.width, sprocket.bottom); | |
} | |
// for (Integer row : sprocketTops) { | |
// line(0, row, src.width, row); | |
// } | |
// | |
// stroke(255, 0, 0); | |
// | |
// for (Integer row : sprocketBottoms) { | |
// line(0, row, src.width, row); | |
// } | |
//drawContours(contours); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment