Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Feb 5th, 2013  |  syntax: None  |  size: 4.32 KB  |  hits: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import hypermedia.video.*;
  2. import java.awt.*;
  3.  
  4. OpenCV opencv;
  5. PImage img;
  6. int n;
  7. PVector norm;
  8. PVector tan;
  9. Rectangle rect;
  10. int blobNum;
  11.  
  12. float[] tilt(Blob[] blobs, PImage img)
  13. {
  14.   img.loadPixels();
  15.  
  16.   int brightestX = 0;
  17.   int brightestY = 0;
  18.   int brightestJ = 0;
  19.   float brightestF = 0;
  20.   int brightestLoc = 0;
  21.  
  22.   for (int j = 0; j < blobs[blobNum].points.length; j++)
  23.   {
  24.     int y = blobs[blobNum].points[j].y;
  25.     int x = blobs[blobNum].points[j].x;
  26.     int loc = x+y*width;
  27.  
  28.     float r = red(img.pixels[loc]);
  29.     float g = green(img.pixels[loc]);
  30.     float b = blue(img.pixels[loc]);
  31.  
  32.     float gray = (r+g+b)/3;
  33.     if (gray > brightestF)
  34.     {
  35.       brightestF = gray;
  36.       brightestX = x;
  37.       brightestY = y;
  38.       brightestJ = j;
  39.       r = 255;
  40.     }
  41.     img.pixels[loc] = color(r, g, b);
  42.   }
  43.  
  44.   brightestLoc = brightestX+brightestY*width;
  45.  
  46.   float r = red(img.pixels[brightestLoc]);
  47.   float g = green(img.pixels[brightestLoc]);
  48.   float b = blue(img.pixels[brightestLoc]);
  49.   r = 0;
  50.   g = 255;
  51.   b = 0;
  52.   img.pixels[brightestLoc] = color(r, g, b);
  53.  
  54.   //FIND NORMAL
  55.   int dist = 10;
  56.   int x2 = blobs[blobNum].points[brightestJ+dist].x;
  57.   int x1 = blobs[blobNum].points[brightestJ-dist].x;
  58.   int y2 = blobs[blobNum].points[brightestJ+dist].y;
  59.   int y1 = blobs[blobNum].points[brightestJ-dist].y;
  60.   int dx = x2-x1;
  61.   int dy = y2-y1;
  62.  
  63.   //  tan = new PVector(dx, dy);
  64.   PVector up = new PVector(0, 1);
  65.   norm = new PVector(-dy, dx);
  66.   //  float tilt = PVector.angleBetween(up, norm); //radians
  67.   float tilt = norm.heading();
  68.   //  println(degrees(bearing(norm)));
  69.  
  70.   img.updatePixels();
  71.   line(x1, y1, x2, y2);
  72.   line(brightestX, brightestY, brightestX-dy, brightestY+dx);
  73.  
  74.   float[] returns = new float[2];
  75.   returns[0] = tilt;
  76.   returns[1] = brightestF;
  77.  
  78.   return returns;
  79.   //  print(norm);
  80. }
  81.  
  82.  
  83.  
  84. float[] slant(float maxContourIntensity, PImage img, Rectangle rect)
  85. {
  86.   int brx = 0;
  87.   int bry = 0;
  88.   int brl = 0;
  89.   float brf = 0;
  90.   img.loadPixels();
  91.   for (int i = rect.x; i < rect.x+rect.width; i++)
  92.   {
  93.     for (int j = rect.y; j < rect.y+rect.height; j++)
  94.     {
  95.       int loc = i+(j*width);
  96.       //println(loc);
  97.       float ra = red(img.pixels[loc]);
  98.       float ga = green(img.pixels[loc]);
  99.       float ba = blue(img.pixels[loc]);
  100.  
  101.       float gray = (ra+ga+ba)/3;
  102.       if (gray > brf)
  103.       {
  104.         brf = gray;
  105.         brx = i;
  106.         bry = j;
  107.         brl = loc;
  108.         //        ba = 255;
  109.       }
  110.       img.pixels[loc] = color(ra, ga, ba);
  111.     }
  112.   }
  113.  
  114.   Point p = new Point(brx, bry);
  115.   //  print(p);
  116.   point(p.x, p.y);
  117.  
  118.   float bl = 255;
  119.   float rl = 0;
  120.   float gl = 0;
  121.  
  122.   img.pixels[brl] = color(rl, gl, bl);
  123.  
  124.   img.updatePixels();
  125.   //  image(img, 0, 0);
  126.  
  127.   float cosa = (float)maxContourIntensity / (float)brf;
  128.   float slant = acos(cosa); //radians i think
  129.   //  print(cosa + " : " + slant);
  130.   float[] slantReturns = new float[3];
  131.   slantReturns[0] = slant;
  132.   slantReturns[1] = p.x;
  133.   slantReturns[2] = p.y;
  134.  
  135.   return slantReturns;
  136. }
  137.  
  138.  
  139.  
  140.  
  141. void setup()
  142. {
  143.   size(1690/2, 650/2, P3D);
  144.  
  145.   opencv = new OpenCV(this);
  146.   //  opencv.capture(640, 480);
  147.   opencv.loadImage("balls.jpeg", width, height);
  148.   img = opencv.image();
  149.  
  150.   background(255);
  151.   //  opencv.read();
  152.   opencv.threshold(80);
  153.  
  154.   Blob[] blobs = opencv.blobs(10, width*height/2, 100, true, OpenCV.MAX_VERTICES*4);
  155.   opencv.restore();
  156.  
  157.   float[] tiltReturns = tilt(blobs, opencv.image());
  158.   //  println(degrees(tilt));
  159.  
  160.   float tilt = tiltReturns[0];
  161.   float maxC = tiltReturns[1];
  162.   //detect surface maxima
  163.  
  164.   blobNum = 2;
  165.   rect = blobs[blobNum].rectangle;
  166.   //  rect(rect.x, rect.y, rect.width, rect.height);
  167.   float[] slantReturns = slant(maxC, img, rect);
  168.   float slant = slantReturns[0];
  169.   int bsx = (int)slantReturns[1];
  170.   int bsy = (int)slantReturns[2];
  171.  
  172.   println("Tilt: " + degrees(tilt) + " : Slant: " + degrees(slant));
  173.  
  174.   pushMatrix();
  175.   translate(width/2, height/2, 0);
  176.   noFill();
  177.   image(img, -width/2, -height/2, width, height);
  178.   image(img, 0, 0);
  179.   stroke(256, 0, 0);
  180.   sphere(256);
  181.   stroke(0, 0, 256);
  182.   float x, y, z;
  183.   x = y = z = 0;
  184.   y = sin(tilt)*128;
  185.   x = cos(tilt)*128;
  186.   z = cos(slant)*128;
  187.   println("X: "+ x + " Y: " + y + " Z: " + z);
  188.   line(bsx, bsy, 0, bsx+2*x, bsy+2*y, 2*z);
  189.   popMatrix();
  190.  
  191.   //  print(rect);
  192. }