diff --git a/ofxMachineVisionLib/ofxMachineVisionLib.vcxproj b/ofxMachineVisionLib/ofxMachineVisionLib.vcxproj index 56ef32f..8ec1b2b 100644 --- a/ofxMachineVisionLib/ofxMachineVisionLib.vcxproj +++ b/ofxMachineVisionLib/ofxMachineVisionLib.vcxproj @@ -55,18 +55,22 @@ + + + + diff --git a/src/ofxMachineVision/Grabber/Simple.cpp b/src/ofxMachineVision/Grabber/Simple.cpp index aee4344..46fa057 100644 --- a/src/ofxMachineVision/Grabber/Simple.cpp +++ b/src/ofxMachineVision/Grabber/Simple.cpp @@ -2,6 +2,9 @@ #include "ofAppRunner.h" #include "ofSystemUtils.h" +using namespace ofxCv; +using namespace cv; + namespace ofxMachineVision { namespace Grabber { //---------- @@ -249,6 +252,30 @@ namespace ofxMachineVision { } } + //---------- + shared_ptr Simple::getFreshFrameAveraged(int numExposures) { + if (numExposures <= 1) + return this->getFreshFrame(); + + shared_ptr averagedFrame(new Frame()); + averagedFrame->getPixelsRef().allocate(this->getWidth(), this->getHeight(), OF_IMAGE_COLOR); + + Mat exposureMat; + Mat accumulatedMat = Mat(this->getHeight(), this->getWidth(), CV_16UC3, Scalar(0)); + for (int i = 0; i < numExposures; i++) { + auto frame = this->getFreshFrame(); + frame->lockForReading(); + toCv(frame->getPixelsRef()).convertTo(exposureMat, CV_16UC3); + frame->unlock(); + + accumulatedMat += exposureMat; + } + accumulatedMat *= (1.f / numExposures); + accumulatedMat.convertTo(toCv(averagedFrame->getPixelsRef()), CV_8UC3); + + return averagedFrame; + } + //---------- void Simple::update() { CHECK_OPEN_SILENT diff --git a/src/ofxMachineVision/Grabber/Simple.h b/src/ofxMachineVision/Grabber/Simple.h index 83f30e1..8506c5d 100644 --- a/src/ofxMachineVision/Grabber/Simple.h +++ b/src/ofxMachineVision/Grabber/Simple.h @@ -5,6 +5,7 @@ #include "Base.h" #include "ofxMachineVision/Frame.h" +#include "ofxCvMin.h" namespace ofxMachineVision { namespace Grabber { @@ -28,6 +29,7 @@ namespace ofxMachineVision { Microseconds getLastTimestamp() const { return this->lastTimestamp; } long getLastFrameIndex() const { return this->lastFrameIndex; } shared_ptr getFreshFrame(float timeoutSeconds = 5.0f); + shared_ptr getFreshFrameAveraged(int numExposures = 1); /** \name ofBaseUpdates