WIP: benchmark Metal mask flow and reduce inspector repeated work

这个提交包含在:
mac 于 2026-11-06 17:56:41 +08:00 提交
1 父节点 ca4abaf2c3
当前提交 48ac192d4b
12 个文件已更改
+392 -18

没有与搜索条件相匹配的文件

+23 -4
查看文件
@@ -8,6 +8,7 @@
namespace dy {
struct OpenCVTracker::Impl {
cv::Mat previous;
std::shared_ptr<FlowBackend> flow;
std::vector<cv::Point2f> points, polygon;
std::vector<FlowPoint> mask;
TrackerROI seed{};
@@ -34,7 +35,15 @@ struct OpenCVTracker::Impl {
cv::convexHull(vertices,hull,false,true);
cv::Mat area=cv::Mat::zeros(height,width,CV_8UC1);
cv::fillPoly(area,std::vector<std::vector<cv::Point>>{hull},cv::Scalar(255),cv::LINE_AA);
cv::goodFeaturesToTrack(previous,points,300,.03,10.,area,3,false,.04);
// Corner detection previously evaluated the whole 4K frame even for a
// small face. Keep a halo for the gradient/local-maximum stencil, then
// restore full-frame coordinates before subpixel refinement and flow.
cv::Rect bounds=cv::boundingRect(hull);
bounds.x-=12;bounds.y-=12;bounds.width+=24;bounds.height+=24;
bounds&=cv::Rect(0,0,width,height);
if(bounds.empty())return;
cv::goodFeaturesToTrack(previous(bounds),points,300,.03,10.,area(bounds),3,false,.04);
for(auto& p:points)p+=cv::Point2f(float(bounds.x),float(bounds.y));
if(!points.empty())cv::cornerSubPix(previous,points,{10,10},{-1,-1},
{cv::TermCriteria::COUNT|cv::TermCriteria::EPS,20,.03});
}
@@ -46,6 +55,7 @@ struct OpenCVTracker::Impl {
OpenCVTracker::OpenCVTracker():impl_(std::make_shared<Impl>()){}
OpenCVTracker::~OpenCVTracker()=default;
void OpenCVTracker::setMask(std::vector<FlowPoint> p){impl_->mask=std::move(p);}
void OpenCVTracker::setFlowBackend(std::shared_ptr<FlowBackend> flow){impl_->flow=std::move(flow);stop();}
const char* OpenCVTracker::version(){return CV_VERSION;}
TrackResult OpenCVTracker::result()const{return impl_->value;}
bool OpenCVTracker::active()const{return impl_->running;}
@@ -70,6 +80,7 @@ bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
t.polygon.emplace_back(t.center.x+c*x-s*y,t.center.y+s*x+c*y);}
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;}
t.running=true;t.value.valid=true;t.value.confidence=1;return true;
} catch(const cv::Exception&){t.fail(TrackFailure::Backend);return false;}
}
@@ -84,19 +95,27 @@ TrackResult OpenCVTracker::track(const GrayFrame& f) {
if(t.frameIndex<2||t.frameIndex%2==0)t.detect();
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;
if(t.flow){
std::vector<FlowPoint> input;input.reserve(t.points.size());
for(auto p:t.points)input.push_back({p.x,p.y});
std::vector<FlowMatch> matches;
if(!t.flow->advance(f,input,matches))return t.fail(TrackFailure::Backend);
for(auto m:matches){src.emplace_back(m.p.x,m.p.y);dst.emplace_back(m.q.x,m.q.y);}
}else{
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);
std::vector<cv::Point2f> src,dst,movingSrc,movingDst;
const float motionThreshold=std::max(t.width,t.height)*.01f;
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;
src.push_back(t.points[i]);dst.push_back(forward[i]);
if(cv::norm(forward[i]-t.points[i])>=motionThreshold){movingSrc.push_back(t.points[i]);movingDst.push_back(forward[i]);}
}
}
const float motionThreshold=std::max(t.width,t.height)*.01f;
for(size_t i=0;i<src.size();++i)if(cv::norm(dst[i]-src[i])>=motionThreshold){movingSrc.push_back(src[i]);movingDst.push_back(dst[i]);}
if(movingSrc.size()>src.size()/2){src.swap(movingSrc);dst.swap(movingDst);}
t.value.matches=int(src.size());
if(src.size()<4)return t.fail(TrackFailure::InsufficientMatches);