gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:50:23 27 4
gpt4 key购买 nike

当存在多个对象时,我试图在静态字段中跟踪单个移动对象。在一位伟大的导师的帮助下,我得到了以下代码。我正在使用 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) )等)。

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

关于opencv - 当静态字段中存在多个对象时跟踪单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23036297/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com