Calibrate works?

This commit is contained in:
jan 2016-07-10 00:36:44 -07:00
commit cc711c6343
1104 changed files with 636510 additions and 75 deletions

View file

@ -0,0 +1,181 @@
package gab.opencv;
import java.util.ArrayList;
import processing.core.*;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.Rect;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Point;
import java.awt.Rectangle;
public class Contour {
private ArrayList<PVector> points;
private Point[] inputPoints;
private double polygonApproximationFactor;
private PApplet parent;
private Rectangle boundingBox;
public MatOfPoint pointMat;
public Contour(PApplet parent, MatOfPoint mat){
polygonApproximationFactor = mat.size().height * 0.01;
this.parent = parent;
this.pointMat = mat;
Rect r = Imgproc.boundingRect(mat);
boundingBox = new Rectangle(r.x, r.y, r.width, r.height);
loadPoints(mat.toArray());
}
public Contour(PApplet parent, MatOfPoint2f mat){
polygonApproximationFactor = mat.size().height * 0.01;
this.parent = parent;
this.pointMat = new MatOfPoint(mat.toArray());
Rect r = Imgproc.minAreaRect(mat).boundingRect();
boundingBox = new Rectangle(r.x, r.y, r.width, r.height);
loadPoints(mat.toArray());
}
public void loadPoints(Point[] pts){
points = new ArrayList<PVector>();
inputPoints = pts;
for(int i = 0; i < inputPoints.length; i++){
points.add(new PVector((float)inputPoints[i].x, (float)inputPoints[i].y));
}
}
/**
* Check if the Contour contains a given x-y point.
* Particularly, useful for interaction via mouseX and mouseY.
*
* @param x
* @param y
* @return boolean
*/
public boolean containsPoint(int x, int y){
Point p = new Point(x,y);
MatOfPoint2f m = new MatOfPoint2f(pointMat.toArray());
double r = Imgproc.pointPolygonTest(m,p, false);
return r == 1;
}
/**
* The polygonApproximationFactor is used to determine
* how strictly to follow a curvy polygon when converting
* it into a simpler polygon with getPolygonApproximation().
* For advanced use only. Set to a sane value by default.
*
* @param polygonApproximationFactor, a double
*/
public void setPolygonApproximationFactor(double polygonApproximationFactor){
this.polygonApproximationFactor = polygonApproximationFactor;
}
/**
* Access the current polygonApproximationFactor. The polygonApproximationFactor
* is used to determine how strictly to follow a curvy polygon when converting
* it into a simpler polygon with getPolygonApproximation().
*
* @return polygonApproximationFactor, a double
*/
public double getPolygonApproximationFactor(){
return polygonApproximationFactor;
}
/**
* Get a new Contour that results from calculating
* the polygon approximation of the current Contour.
* The tightness of the approximation is set by the polygonApproximationFactor,
* See setPolygonApproximationFactor() and getPolygonApproximationFactor().
*
* @return
*/
public Contour getPolygonApproximation(){
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(inputPoints), approx, polygonApproximationFactor, true);
return new Contour(parent, approx);
}
/**
* Calculate a convex hull from the current Contour.
* Returns a new Contour representing the convex hull.
*
* @return Contour
*/
public Contour getConvexHull(){
MatOfInt hull = new MatOfInt();
MatOfPoint points = new MatOfPoint(pointMat);
Imgproc.convexHull(points, hull);
Point[] hp = new Point[hull.height()];
for(int i = 0; i < hull.height(); i++){
int index = (int)hull.get(i,0)[0];
hp[i] = new Point(pointMat.get(index,0));
}
MatOfPoint hullPoints = new MatOfPoint();
hullPoints.fromArray(hp);
return new Contour(parent, hullPoints);
}
/**
* Draw the Contour as a closed shape with one vertex per-point.
*
*/
public void draw(){
parent.beginShape();
for (PVector p : points) {
parent.vertex(p.x, p.y);
}
parent.endShape(PConstants.CLOSE);
}
/**
* Get the points that make up the Contour.
*
* @return ArrayList<PVector> points
*/
public ArrayList<PVector> getPoints(){
return points;
}
/**
* The number of points in the Contour.
*
* @return int
*/
public int numPoints(){
return points.size();
}
/**
* Get the bounding box for the Contour.
*
* @return A java.awt.Rectangle
*/
public Rectangle getBoundingBox(){
return boundingBox;
}
/**
* The area of the Contour's bounding box. In most cases, this is a good approximation for the Contour's area.
*
* @return float area
*/
public float area(){
return (boundingBox.width * boundingBox.height);
}
}

View file

@ -0,0 +1,16 @@
package gab.opencv;
import java.util.Comparator;
public class ContourComparator implements Comparator<Contour> {
public int compare(Contour c1, Contour c2) {
if(c1.area() == c2.area()){
return 0;
}
else if (c1.area() > c2.area()) {
return -1;
} else{
return 1;
}
}
}

View file

@ -0,0 +1,135 @@
package gab.opencv;
import processing.core.*;
import java.awt.Rectangle;
import org.opencv.video.Video;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Core;
import org.opencv.core.Scalar;
public class Flow {
private Mat prev;
private Mat flow;
private boolean hasFlow = false;
private double pyramidScale = 0.5;
private int nLevels = 4;
private int windowSize = 8;
private int nIterations = 2;
private int polyN = 7;
private double polySigma = 1.5;
private int runningFlags = Video.OPTFLOW_FARNEBACK_GAUSSIAN;
private PApplet parent;
public Flow(PApplet parent) {
flow = new Mat();
this.parent = parent;
}
public int width(){
return flow.width();
}
public int height(){
return flow.height();
}
public boolean hasFlow(){
return hasFlow;
}
public void calculateOpticalFlow(Mat m) {
int flags = runningFlags;
if (!hasFlow) {
prev = m.clone();
flags = Video.OPTFLOW_USE_INITIAL_FLOW;
hasFlow = true;
}
Video.calcOpticalFlowFarneback(prev, m, flow, pyramidScale, nLevels, windowSize, nIterations, polyN, polySigma, flags);
prev = m.clone();
}
public PVector getTotalFlowInRegion(int x, int y, int w, int h) {
Mat region = flow.submat(y, y+h, x, x+w);
Scalar total = Core.sumElems(region);
return new PVector((float)total.val[0], (float)total.val[1]);
}
public PVector getAverageFlowInRegion(int x, int y, int w, int h) {
PVector total = getTotalFlowInRegion(x, y, w, h);
return new PVector(total.x/(flow.width() * flow.height()), total.y/(flow.width()*flow.height()));
}
public PVector getTotalFlow() {
return getTotalFlowInRegion(0, 0, flow.width(), flow.height());
}
public PVector getAverageFlow() {
return getAverageFlowInRegion(0, 0, flow.width(), flow.height());
}
public PVector getFlowAt(int x, int y){
return new PVector((float)flow.get(y, x)[0], (float)flow.get(y, x)[1]);
}
public void draw() {
int stepSize = 4;
for (int y = 0; y < flow.height(); y+=stepSize) {
for (int x = 0; x < flow.width(); x+=stepSize) {
PVector flowVec = getFlowAt(x,y);
parent.line(x, y, x+flowVec.x, y+flowVec.y);
}
}
}
public void setPyramidScale(double v){
pyramidScale = v;
}
public double getPyramidScale(){
return pyramidScale;
}
public void setLevels(int n){
nLevels = n;
}
public int getLevels(){
return nLevels;
}
public void setWindowSize(int s){
windowSize = s;
}
public int getWindowSize(){
return windowSize;
}
public void setIterations(int i){
nIterations = i;
}
public int getIterations(){
return nIterations;
}
public void setPolyN(int n){
polyN = n;
}
public int getPolyN(){
return polyN;
}
public void setPolySigma(double s){
polySigma = s;
}
public double getPolySigma(){
return polySigma;
}
}

View file

@ -0,0 +1,31 @@
package gab.opencv;
import processing.core.*;
import org.opencv.core.Mat;
public class Histogram {
private Mat mat;
private PApplet parent;
public Histogram(PApplet parent, Mat mat){
this.mat = mat;
this.parent = parent;
}
public void draw(int x, int y, int w, int h) {
parent.pushMatrix();
parent.translate(x, y);
int numBins = mat.height();
float binWidth = w/(float)numBins;
for (int i = 0; i < numBins; i++) {
float v = (float)mat.get(i, 0)[0];
parent.rect(i*binWidth, h, binWidth, -h*v);
}
parent.popMatrix();
}
public Mat getMat(){
return mat;
}
}

View file

@ -0,0 +1,34 @@
package gab.opencv;
import processing.core.*;
public class Line {
public PVector start, end;
public double angle;
public double x1, y1, x2, y2;
public Line(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
start = new PVector((float)x1, (float)y1);
end = new PVector((float)x2, (float)y2);
// measure the angle between this line
// and a vertical line oriented up
angle = angleBetween(x1, y1, x2, y2, 0, 0, 0, -1);
}
public double angleFrom(Line other){
return angleBetween(x1, y1, x2, y2, other.x1, other.y1, other.x2, other.y2);
}
public static double angleBetween(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
double angle1 = Math.atan2(y1 - y2, x1 - x2);
double angle2 = Math.atan2(y3 - y4, x3 - x4);
return angle1-angle2;
}
}

File diff suppressed because it is too large Load diff