(Created page with "Quick and dirty Euglena tracking using OpenCV, Syphon, and OSC. * Full source: File:TrackEuglenaSyphonOSC.zip") |
No edit summary |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Quick and dirty Euglena tracking using OpenCV, Syphon, and OSC. | === Quick and dirty Euglena tracking using OpenCV, Syphon, and OSC. === | ||
Sources at GitHub: https://github.com/lscherff/biogames/tree/master/TrackEuglenaSyphonOSC | |||
=== Threshold based Euglena detection === | |||
<source lang="java"> | |||
opencv.loadImage(cam); | |||
opencv.gray(); | |||
opencv.invert(); | |||
opencv.erode(); | |||
opencv.dilate(); | |||
threshold = int(map(mouseX,0,width,0,255)); | |||
opencv.threshold(threshold); | |||
// ... | |||
contours = opencv.findContours(); | |||
</source> | |||
=== Skipping large/small/malformed contours === | |||
<source lang="java"> | |||
for (Contour contour : contours) { | |||
float a = contour.area(); | |||
Rectangle r = contour.getBoundingBox(); | |||
float wh = float(r.width)/float(r.height); | |||
if (a > minArea && wh > 0.1 && wh < 10 && a < maxArea) { | |||
// ... | |||
</source> | |||
=== Maintaining Euglena identity in a list (based on proximity from frame to frame), computing speed as dx, dy and mean speed as mdx, mdy=== | |||
<source lang="java"> | |||
void checkEuglena(Contour c) { | |||
boolean found = false; | |||
Rectangle r = c.getBoundingBox(); | |||
for (Euglena e : euglenaList) { | |||
if (dist(e.x, e.y, r.x, r.y) < distanceThreshold) { | |||
e.dx = e.x - r.x; | |||
e.dy = e.y - r.y; | |||
e.x = r.x; | |||
e.y = r.y; | |||
e.found = true; | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) { | |||
euglenaList.add(new Euglena(r.x, r.y)); | |||
} | |||
} | |||
void deleteEuglena() { | |||
ListIterator<Euglena> iter = euglenaList.listIterator(); | |||
float newmdx = 0; | |||
float newmdy = 0; | |||
while (iter.hasNext()) { | |||
Euglena e = iter.next(); | |||
if (e.found==false) iter.remove(); | |||
else { | |||
e.found = false; | |||
newmdx = newmdx + e.dx; | |||
newmdy = newmdy + e.dy; | |||
} | |||
} | |||
if (euglenaList.size() > 0) { | |||
newmdx = newmdx / euglenaList.size(); | |||
newmdy = newmdy / euglenaList.size(); | |||
} else { | |||
newmdx = 0; | |||
newmdy = 0; | |||
} | |||
mdx += (newmdx - mdx)*0.01; | |||
mdy += (newmdy - mdy)*0.01; | |||
} | |||
</source> |
Latest revision as of 07:06, 10 June 2015
Quick and dirty Euglena tracking using OpenCV, Syphon, and OSC.
Sources at GitHub: https://github.com/lscherff/biogames/tree/master/TrackEuglenaSyphonOSC
Threshold based Euglena detection
opencv.loadImage(cam);
opencv.gray();
opencv.invert();
opencv.erode();
opencv.dilate();
threshold = int(map(mouseX,0,width,0,255));
opencv.threshold(threshold);
// ...
contours = opencv.findContours();
Skipping large/small/malformed contours
for (Contour contour : contours) {
float a = contour.area();
Rectangle r = contour.getBoundingBox();
float wh = float(r.width)/float(r.height);
if (a > minArea && wh > 0.1 && wh < 10 && a < maxArea) {
// ...
Maintaining Euglena identity in a list (based on proximity from frame to frame), computing speed as dx, dy and mean speed as mdx, mdy
void checkEuglena(Contour c) {
boolean found = false;
Rectangle r = c.getBoundingBox();
for (Euglena e : euglenaList) {
if (dist(e.x, e.y, r.x, r.y) < distanceThreshold) {
e.dx = e.x - r.x;
e.dy = e.y - r.y;
e.x = r.x;
e.y = r.y;
e.found = true;
found = true;
break;
}
}
if (!found) {
euglenaList.add(new Euglena(r.x, r.y));
}
}
void deleteEuglena() {
ListIterator<Euglena> iter = euglenaList.listIterator();
float newmdx = 0;
float newmdy = 0;
while (iter.hasNext()) {
Euglena e = iter.next();
if (e.found==false) iter.remove();
else {
e.found = false;
newmdx = newmdx + e.dx;
newmdy = newmdy + e.dy;
}
}
if (euglenaList.size() > 0) {
newmdx = newmdx / euglenaList.size();
newmdy = newmdy / euglenaList.size();
} else {
newmdx = 0;
newmdy = 0;
}
mdx += (newmdx - mdx)*0.01;
mdy += (newmdy - mdy)*0.01;
}