当静态场中存在多个对象时,跟踪单个对象



当存在多个对象时,我试图在静态字段中跟踪单个移动对象。在一位伟大导师的帮助下,我得到了以下代码。我正在使用opencv来处理库。但当代码被编译时,我得到了一个错误:无法从中转换元素类型ArrayList到ArrayList>的行:对于(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;
 }
 }

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

例如,如果您小心处理函数期望的参数和传递给它们的参数,就可以消除语法错误:

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;
}

虽然上面的代码将编译并运行,但我怀疑它是否能满足您的要求。这就引出了一个问题:你在努力实现什么。"当有多个物体时,试图在静态场中跟踪单个运动物体"听起来很模糊。你上面的算法应该如何工作?(什么是blobgp?似乎只处理第一个轮廓(contours.get(0))等)

您只是想在可能包含多个斑点的场景中显示移动斑点的中心吗?如果你有多个斑点,而只有一个在移动,并且你对此感兴趣,你能不能简单地先减去背景,这样移动的斑点将是唯一检测到的斑点?

最新更新