Keep stable OpenCV flow with compact Metal capture and pyramid reuse
这个提交包含在:
1 父节点
48ac192d4b
当前提交
1cc7f8edb2
15 个文件已更改
+184
-29
没有与搜索条件相匹配的文件
@@ -8,6 +8,7 @@
|
||||
namespace dy {
|
||||
struct OpenCVTracker::Impl {
|
||||
cv::Mat previous;
|
||||
std::vector<cv::Mat> previousPyramid;
|
||||
std::shared_ptr<FlowBackend> flow;
|
||||
std::vector<cv::Point2f> points, polygon;
|
||||
std::vector<FlowPoint> mask;
|
||||
@@ -19,6 +20,7 @@ struct OpenCVTracker::Impl {
|
||||
|
||||
static cv::Mat gray8(const GrayFrame& f) {
|
||||
if(!f.valid())return {};
|
||||
if(f.pixels.empty())return cv::Mat(f.height,f.width,CV_8UC1,const_cast<uint8_t*>(f.bytes.data())).clone();
|
||||
cv::Mat out(f.height,f.width,CV_8UC1);
|
||||
for(size_t i=0;i<f.pixels.size();++i) {
|
||||
float x=f.pixels[i];
|
||||
@@ -59,7 +61,7 @@ void OpenCVTracker::setFlowBackend(std::shared_ptr<FlowBackend> flow){impl_->flo
|
||||
const char* OpenCVTracker::version(){return CV_VERSION;}
|
||||
TrackResult OpenCVTracker::result()const{return impl_->value;}
|
||||
bool OpenCVTracker::active()const{return impl_->running;}
|
||||
void OpenCVTracker::stop(){impl_->running=false;impl_->value.valid=false;}
|
||||
void OpenCVTracker::stop(){impl_->running=false;impl_->value.valid=false;impl_->previous.release();impl_->previousPyramid.clear();impl_->points.clear();}
|
||||
|
||||
bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
|
||||
auto& t=*impl_;t.running=false;t.frameIndex=1;t.points.clear();t.polygon.clear();
|
||||
@@ -81,6 +83,7 @@ bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
|
||||
t.detect();t.value.features=int(t.points.size());t.value.inliers=t.value.features;
|
||||
if(t.points.size()<4){t.fail(TrackFailure::InsufficientTexture);return false;}
|
||||
if(t.flow&&!t.flow->reset(f)){t.fail(TrackFailure::Backend);return false;}
|
||||
if(!t.flow)cv::buildOpticalFlowPyramid(t.previous,t.previousPyramid,{21,21},3);
|
||||
t.running=true;t.value.valid=true;t.value.confidence=1;return true;
|
||||
} catch(const cv::Exception&){t.fail(TrackFailure::Backend);return false;}
|
||||
}
|
||||
@@ -96,6 +99,7 @@ TrackResult OpenCVTracker::track(const GrayFrame& f) {
|
||||
t.value.features=int(t.points.size());t.value.matches=0;t.value.inliers=0;t.value.medianError=0;
|
||||
if(t.points.size()<4)return t.fail(TrackFailure::InsufficientTexture);
|
||||
std::vector<cv::Point2f> src,dst,movingSrc,movingDst;
|
||||
std::vector<cv::Mat> nextPyramid;
|
||||
if(t.flow){
|
||||
std::vector<FlowPoint> input;input.reserve(t.points.size());
|
||||
for(auto p:t.points)input.push_back({p.x,p.y});
|
||||
@@ -106,8 +110,11 @@ TrackResult OpenCVTracker::track(const GrayFrame& f) {
|
||||
std::vector<cv::Point2f> forward,backward;
|
||||
std::vector<uchar> fs,bs;std::vector<float> fe,be;
|
||||
cv::TermCriteria termination(cv::TermCriteria::COUNT|cv::TermCriteria::EPS,30,.01);
|
||||
cv::calcOpticalFlowPyrLK(t.previous,next,t.points,forward,fs,fe,{21,21},3,termination,0,1e-4);
|
||||
cv::calcOpticalFlowPyrLK(next,t.previous,forward,backward,bs,be,{21,21},3,termination,0,1e-4);
|
||||
// Build each frame and its gradients once; reuse them for both flow
|
||||
// directions and as the previous frame on the next analysis callback.
|
||||
cv::buildOpticalFlowPyramid(next,nextPyramid,{21,21},3);
|
||||
cv::calcOpticalFlowPyrLK(t.previousPyramid,nextPyramid,t.points,forward,fs,fe,{21,21},3,termination,0,1e-4);
|
||||
cv::calcOpticalFlowPyrLK(nextPyramid,t.previousPyramid,forward,backward,bs,be,{21,21},3,termination,0,1e-4);
|
||||
for(size_t i=0;i<t.points.size();++i) {
|
||||
// Checking both statuses also rejects a failed backward solve.
|
||||
if(!fs[i]||!bs[i]||cv::norm(backward[i]-t.points[i])>=1.)continue;
|
||||
@@ -150,7 +157,7 @@ TrackResult OpenCVTracker::track(const GrayFrame& f) {
|
||||
t.value.scaleX=float(totalX);t.value.scaleY=float(totalY);t.value.scale=std::sqrt(t.value.scaleX*t.value.scaleY);
|
||||
t.value.rotation+=float(angle);t.value.confidence=confidence;t.value.valid=true;t.value.failure=TrackFailure::None;
|
||||
t.points.clear();for(size_t i=0;i<dst.size();++i)t.points.push_back(dst[i]+(center-cv::Point2f(float(tx),float(ty))));
|
||||
t.previous=next;++t.frameIndex;return t.value;
|
||||
t.previous=next;t.previousPyramid=std::move(nextPyramid);++t.frameIndex;return t.value;
|
||||
}catch(const cv::Exception&){return t.fail(TrackFailure::Backend);}
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户