Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Voronoi 2D from OpenCV blob/ contour

$
0
0

Hello: I am trying to create a surface mesh ultimately, from OpenCV detected contours using ToxicLibs. I first want to create the Voronoi2D points, then convert them to 3D and add them to a mesh. Or Simply Triangulate the contour as a plane using the Voronoi class, then add that plane as faces to a mesh.

However I am getting stuck on the best way to add the voronoi points, my program is slow and keeps crashing. This is because the contour and bounding box change every frame, so operations have to occur every frame. Using nested for loops seems like a bad idea given that, but I am not sure how else to do it. Please let me know if you have advice.

If I use only the PolygonApproximation Contour, I can get the voronoi points and not compromise speed (yet), but i am wondering what the best way to convert these to 3D faces would be next. Thanks.

`

import gab.opencv.*; import processing.video.*; import java.awt.Rectangle;

import toxi.geom.*; import toxi.geom.mesh.*; import java.util.Iterator;

//for 2d

  import toxi.geom.*;
  import toxi.geom.mesh2d.*;

  import toxi.util.*;
  import toxi.util.datatypes.*;

  import toxi.processing.*;

  //// ranges for x/y positions of points
  FloatRange xpos, ypos;

  // helper class for rendering
  ToxiclibsSupport gfx;

  // empty voronoi mesh container
  Voronoi voronoi = new Voronoi();

  // optional polygon clipper
  PolygonClipper2D clip;


  //for mesh/shape

WETriangleMesh mesh=new WETriangleMesh("doodle");

PShape poly;

Vec3D prev=new Vec3D(); Vec3D p=new Vec3D(); Vec3D q=new Vec3D();

Vec2D newblob=new Vec2D();

float weight=0; color c; Capture video; OpenCV opencv; PImage src, colorFilteredImage; ArrayList contours;

// <1> Set the range of Hue values for our filter int rangeLow = 20; int rangeHigh = 35;

void setup() {

String[] cameras = Capture.list();
   println("Available cameras:");
   printArray(cameras);


video = new Capture(this, 640, 480, cameras[0]);
video.start();

opencv = new OpenCV(this, video.width, video.height);
contours = new ArrayList<Contour>();

size(900,900, P3D);
//smooth();

}

void draw() {

       updatePixels();
          if (video.available()) {
            video.read();
          }
          opencv.loadImage(video);
          opencv.useColor();
          src = opencv.getSnapshot();
          opencv.useColor(HSB);
          opencv.setGray(opencv.getH().clone());
          opencv.inRange(rangeLow, rangeHigh);
          colorFilteredImage = opencv.getSnapshot();


          contours = opencv.findContours(true, true);

          image(src, 0, 0);
          image(colorFilteredImage, src.width, 0);


          if (contours.size() > 0) {
            //update voronoi per frame
            voronoi = new Voronoi();

            Contour biggestContour = contours.get(0);
            Rectangle r = biggestContour.getBoundingBox();
            //println("aprx", biggestContour.getPolygonApproximation());

                    noFill();
                    strokeWeight(2);
                    stroke(255, 0, 0);
                    rect(r.x, r.y, r.width, r.height);

                    noStroke();
                    fill(255, 0, 0);
                    ellipse(r.x + r.width/2, r.y + r.height/2, 30, 30);

                     biggestContour.setPolygonApproximationFactor(20);
                    stroke(100, 100, 100);
                    biggestContour.getPolygonApproximation().draw();
                    //biggestContour.draw();
                    Contour ApproxContour = biggestContour.getPolygonApproximation();
                    //AddPoint function is too slow!
                    //AddPoint(r.width, r.height, ApproxContour);

            //convert countour to points
            for (PVector point : ApproxContour.getPoints()) {
              //THIS WORKS
              //vertex(point.x, point.y, point.z);
                       voronoi.addPoint(new Vec2D(point.x, point.y));
                       //Vec3D pos = new Vec3D(point.x, point.y, point.z);




             }








          for (Vec2D c : voronoi.getSites()) {
                     fill(0, 255, 0);
                  ellipse(c.x, c.y, 5, 5);
      }
          }

              }

void vertex(Vec3D v) { vertex(v.x,v.y,v.z); }

void drawMesh(int x, int y) { // background(0); lights(); translate(x, y, 50); noStroke(); beginShape(TRIANGLES); // iterate over all faces/triangles of the mesh for(Iterator i=mesh.faces.iterator(); i.hasNext();) { Face f=(Face)i.next(); // create vertices for each corner point vertex(f.a); vertex(f.b); vertex(f.c); } endShape();

// udpate rotation

//newblob.addSelf();

}

void mousePressed() {

color c = get(mouseX, mouseY);
println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));

int hue = int(map(hue(c), 0, 255, 0, 180));
println("hue to detect: " + hue);

rangeLow = hue - 5;
rangeHigh = hue + 5;

}

void keyPressed() { if (key=='s') { mesh.saveAsOBJ(sketchPath("doodle.obj")); mesh.saveAsSTL(sketchPath("doodle.stl")); } else { mesh.clear(); } }

void AddPoint(int x, int y, Contour c) { // loadPixels(); //updatePixels(); for ( int i = 0; i < x; i++) { for ( int j = 0; j < y;j++) { //int loc = i + j* x;

                          if (c.containsPoint(i, j)) {
                            voronoi.addPoint(new Vec2D(i, j));
                            fill(0, 0, 255);
                            ellipse(i, j, 2, 2);
                            println("gotpoint");
                            println("point: (", i,",", j, ")");
                             //updatePixels();



                          //pick a random point
                           //int x = int(random(r.width));
                          // int y = int(random(r.height));


                            //voronoi.addPoint(new Vec2D(src.pixels[x], src.pixels[y]));
                          }




                       }

                  }

}

`


Viewing all articles
Browse latest Browse all 2896

Trending Articles