我沒有設法端口卡爾曼濾波器C++樣品威廉鏈接到,因爲不是所有的從C++ API函數存在於OpenCV的Java包裝,所以這是據我已經有了:
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import org.opencv.core.*;
import org.opencv.video.*;
int w = 640;
int h = 480;
int hw = w/2;
int hh = h/2;
Capture video;
OpenCV opencv;
/*
Mat img(500, 500, CV_8UC3);
KalmanFilter KF(4, 2, 0);
Mat_<float> state(4, 1); // (x, y, Vx, Vy)
Mat processNoise(4, 1, CV_32F);
Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
*/
Mat img;
KalmanFilter KF;
Mat processNoise;
MatOfFloat state,measurement;
void setup() {
size(w, h);
video = new Capture(this, hw, hh);
opencv = new OpenCV(this, hw, hh);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
img = new Mat(w,h,CvType.CV_8UC3);
KF = new KalmanFilter(4,2,0,CvType.CV_32F);
state = new MatOfFloat(4,1);
}
void draw() {
background(255);
scale(2);
opencv.loadImage(video);
image(video, 0, 0);
noFill();
stroke(0, 255, 0);
strokeWeight(3);
Rectangle[] faces = opencv.detect();
if(faces.length > 0)
rect(faces[0].x,faces[0].y,faces[0].width,faces[0].height);
}
void captureEvent(Capture c) {
c.read();
}
你可能注意到了,我沒有得到很遠,像statePre
或transitionMatrix
性質是不可見的。
雖然不一樣,也許有點平均可能會有所幫助:
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
int w = 640;
int h = 480;
int hw = w/2;
int hh = h/2;
Capture video;
OpenCV opencv;
int historySize = 10;
Rectangle[] history;
void setup() {
size(w, h);
video = new Capture(this, hw, hh);
opencv = new OpenCV(this, hw, hh);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
setupHistory();
}
void setupHistory(){
history = new Rectangle[historySize];
for(int i = 0 ; i < historySize ; i++) history[i] = new Rectangle();
}
void keyPressed(){
if(key == 'h' && historySize > 1) {
noLoop();
historySize--;
setupHistory();
loop();
}
if(key == 'H') {
noLoop();
historySize++;
setupHistory();
loop();
}
println(historySize);
}
void draw() {
background(255);
scale(2);
opencv.loadImage(video);
image(video, 0, 0);
noFill();
stroke(0, 255, 0);
strokeWeight(3);
Rectangle[] faces = opencv.detect();
if(faces.length > 0){
Rectangle f = mousePressed ? average(faces[0]) : faces[0];
rect(f.x,f.y,f.width,f.height);
}
}
void captureEvent(Capture c) {
c.read();
}
Rectangle average(Rectangle newFace){
Rectangle avg = new Rectangle();
for(int i = historySize - 1; i > 0; i--){
history[i] = history[i-1];
avg.x += history[i].x;
avg.y += history[i].y;
avg.width += history[i].width;
avg.height += history[i].height;
}
history[0] = newFace;
avg.x += history[0].x;
avg.y += history[0].y;
avg.width += history[0].width;
avg.height += history[0].height;
avg.x /= historySize;
avg.y /= historySize;
avg.width /= historySize;
avg.height /= historySize;
return avg;
}
按住按下看到流暢的結果鼠標。使用H
鍵增加,使用h
減小歷史記錄大小。
http://stackoverflow.com/questions/15678585/how-to-smooth-the-tracking-in-camshift/15683124#15683124 – William
@William,是的,當我在搜索時遇到了這個問題。但我無法將代碼移植到Processing。 – Shrihari