euglena/src/ObjectRecognition.java

56 lines
1.9 KiB
Java
Raw Normal View History

2016-07-10 00:36:44 -07:00
class EuglenaCounter {
int roiX, roiY, roiWidth, roiHeight, lastMillis, avgTime;
float previousAverage;
boolean averageReady;
OpenCV opencv;
ArrayList<int> counts;
EuglenaCounter(PApplet parent, int roiX, int roiY, int roiWidth, int roiHeight, int avgTime){
this.counts = new ArrayList<int>();
this.roiX = roiX;
this.roiY = roiY;
this.roiWidth = roiWidth;
this.roiHeight = roiHeight;
this.avgTime = avgTime;
this.opencv = new OpenCV(parent, roiWidth, roiHeight);
this.opencv.startBackgroundSubtraction(7, 3, .35);
this.reset();
}
void reset(){
this.lastMillis = millis();
this.counts.clear();
this.averageReady = false;
this.previousAverage = -1;
}
void update() {
pImage roiFrame = get(roiX, roiY, roiWidth, roiHeight); // Get pixels of interest and saves as image for valid image path
this.opencv.loadImage(roiFrame); // Input proper pixels into cv processing
this.opencv.updateBackground(); //Necessary for background subtraction
this.opencv.useGray(); //Create grayscale image
this.opencv.dilate(); //Clean up image
this.opencv.erode();
this.opencv.blur(3);
//dst = opencv.getOutput(); //Save computer vision as separate image path
//Find outline of moving objects - euglena
int count = opencv.findContours().size(); // Count number of outlines
this.counts.add(count);
int interval = millis() - this.lastMillis();
if (interval > this.avgTime) {
this.previousAverage = 0;
for (int c : this.counts) {
this.previousAverage = this.previousAverage + c;
}
this.previousAverage = this.previousAverage / this.counts.size();
this.averageReady = true;
this.counts.clear();
}
}
}