当静态字段中存在多个对象时跟踪单个对象

时间:2014-04-12 21:24:49

标签: opencv processing

当存在多个对象时,我试图在静态字段中跟踪单个移动对象。在一位伟大的导师的帮助下,我得到了以下代码。我使用opencv来处理库。但是当代码被编译时,我得到一个错误: 无法从元素类型转换      ArrayList to ArrayList>在线:     for(ArrayList< ArrayList> blob:blobgp)

import gab.opencv.*;
import processing.video.*;
 import java.awt.Rectangle;
 int x, y;

  OpenCV opencv;
   Capture cam;

   ArrayList<Contour> contours;
   PVector previousPosition;

  void setup() {
  cam = new Capture(this, 640/2, 480/2);
   size(cam.width, cam.height);
   opencv = new OpenCV(this, cam.width, cam.height);
   opencv.useGray();
   opencv.startBackgroundSubtraction(5, 3, 0.1);

   cam.start();
   previousPosition = new PVector();
   }

  void draw() {

  track();
  stroke(255, 0, 0); 
  noFill();
  strokeWeight(5); 
  ellipse(x, y, 10, 10);
 }
void track() {

        image(cam, 0, 0);
 opencv.loadImage(cam);
 opencv.updateBackground();
 opencv.erode();
 opencv.dilate();
 ArrayList<Contour> contours = opencv.findContours(false, true);
 ArrayList<Contour> contourblobs =new ArrayList<Contour>();
 ArrayList<ArrayList<Contour>> blobgp = new ArrayList<ArrayList<Contour>>();
 contourblobs.add(contours.get(0));
 blobgp.add(contourblobs);

 for (int i = 1; i < contours.size(); i++) {
ArrayList<Contour> remainingcontour =new ArrayList<Contour>();
remainingcontour.add(contours.get(i));

PVector contourCenter = centerOfContour(remainingcontour);

boolean matchesExistingBlob = false;

for (ArrayList< ArrayList<Contour> > blob : blobgp ) {
  PVector blobCenter = centerOfBlob(blob);
  if (PVector.dist(blobCenter, contourCenter) < threshold) {
    blob.add(contour);
    matchesExistingBlob = true;
  }
}
// if it didn't match an existing blob
// create a new one
if (!matchesExistingBlob) {
  ArrayList<ArrayList<Contour>> newBlob =new ArrayList<ArrayList<Contour>>();
  newBlob.add(contour);
   }
 }

// now use unique blobs to draw the dots:
for (ArrayList<ArrayList<Contour>> blob : blobgp) {
PVector c  = centerOfBlob(blob);

x=c.x;
y=c.y;

  }
}  




 // helper functions

PVector centerOfContour(ArrayList<Contour> remainingcontour) {
PVector result = new PVector();
int numPoints = 0;
 for (PVector p : contour.getPoints()) {
  result.x += p.x;
  result.y += p.y;
  numPoints++;
}
 result.x /= numPoints;
 result.y /= numPoints;
 return result;
 }

 PVector centerOfBlob(ArrayList<ArrayList<Contour>> blob) {
  PVector result = new PVector();
  for (ArrayList<Contour> contour : blob) {
    PVector contourCenter = centerOfContour(contour);
    result.x += contourCenter.x;
    result.y += contourCenter.y;
   }
 result.x /= blob.size();
 result.y /= blob.size();
 return result;
 }
 }

1 个答案:

答案 0 :(得分:1)

您应该更好地理解您使用的代码。

例如,如果您仔细考虑函数期望的参数以及传递给它们的内容,则可以摆脱语法错误:

import gab.opencv.*;
import processing.video.*;
import java.awt.Rectangle;
float x, y;

OpenCV opencv;
Capture cam;

ArrayList<Contour> contours;
PVector previousPosition;
int threshold = 20;

void setup() {
  cam = new Capture(this, 640/2, 480/2);
  size(cam.width, cam.height);
  opencv = new OpenCV(this, cam.width, cam.height);
  opencv.useGray();
  opencv.startBackgroundSubtraction(5, 3, 0.1);

  cam.start();
  previousPosition = new PVector();
}

void draw() {

  track();
  stroke(255, 0, 0); 
  noFill();
  strokeWeight(5); 
  ellipse(x, y, 10, 10);
}
void track() {

  image(cam, 0, 0);
  opencv.loadImage(cam);
  opencv.updateBackground();
  opencv.erode();
  opencv.dilate();
  ArrayList<Contour> contours = opencv.findContours(false, true);
  ArrayList<Contour> contourblobs =new ArrayList<Contour>();
  ArrayList<ArrayList<Contour>> blobgp = new ArrayList<ArrayList<Contour>>();
  if(contours.size() > 0){
    contourblobs.add(contours.get(0));
    blobgp.add(contourblobs);

    for (int i = 1; i < contours.size(); i++) {
      ArrayList<Contour> remainingcontour =new ArrayList<Contour>();
      remainingcontour.add(contours.get(i));

      PVector contourCenter = centerOfContour(remainingcontour);

      boolean matchesExistingBlob = false;
      PVector blobCenter = centerOfBlob(blobgp);
        if (PVector.dist(blobCenter, contourCenter) < threshold) {
          blobgp.add(contours);
          matchesExistingBlob = true;
        }
      /*
      for (ArrayList< ArrayList<Contour> > blob : blobgp ) {
        PVector blobCenter = centerOfBlob(blob);
        if (PVector.dist(blobCenter, contourCenter) < threshold) {
          blob.add(contour);
          matchesExistingBlob = true;
        }
      }
      */
      // if it didn't match an existing blob
      // create a new one
      if (!matchesExistingBlob) {
        ArrayList<ArrayList<Contour>> newBlob =new ArrayList<ArrayList<Contour>>();
        newBlob.add(contours);
      }
    }

    // now use unique blobs to draw the dots:
    /*
    for (ArrayList<ArrayList<Contour>> blob : blobgp) {
      PVector c  = centerOfBlob(blob);

      x=c.x;
      y=c.y;
    }
    */
    PVector c  = centerOfBlob(blobgp);
      x=c.x;
      y=c.y;
    }
}  

// helper functions

PVector centerOfContour(ArrayList<Contour> remainingcontour) {
  PVector result = new PVector();
  int numPoints = 0;
  for (Contour contour : contours) {
    for (PVector p : contour.getPolygonApproximation().getPoints()) {
      result.x += p.x;
      result.y += p.y;
      numPoints++;
    }
  }
  result.x /= numPoints;
  result.y /= numPoints;
  return result;
}

PVector centerOfBlob(ArrayList<ArrayList<Contour>> blob) {
  PVector result = new PVector();
  for (ArrayList<Contour> contour : blob) {
    PVector contourCenter = centerOfContour(contour);
    result.x += contourCenter.x;
    result.y += contourCenter.y;
  }
  result.x /= blob.size();
  result.y /= blob.size();
  return result;
}

虽然上面的代码将编译并运行,但我怀疑它会做你以后的事情。这给我们带来了一个问题:你想要实现什么目标。 &#34;尝试在多个对象时跟踪静态字段中的单个移动对象。听起来很模糊。你的算法如何工作? (什么是blobgp?似乎只处理第一个轮廓(contours.get(0))等。)

您是否只是想在可能包含多个blob的场景中显示移动blob的中心?如果你有多个blob并且只有一个正在移动并且你对它感兴趣,你能不能简单地减去背景,那么移动的blob将是唯一检测到的blob?