feat: WIP unified workspace shared mask objects and OpenCV tracker core

这个提交包含在:
mac 于 2026-11-05 03:16:17 +08:00 提交
1 父节点 78e53b3889
当前提交 ca352248c1
36 个文件已更改
+5298 -164

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

+137
查看文件
@@ -0,0 +1,137 @@
// Independent reconstruction using the public OpenCV API. No commercial source.
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/calib3d.hpp>
namespace dy {
struct OpenCVTracker::Impl {
cv::Mat previous;
std::vector<cv::Point2f> points, polygon;
std::vector<FlowPoint> mask;
TrackerROI seed{};
TrackResult value{};
cv::Point2f center{};
int frameIndex=0, width=0, height=0;
bool running=false;
static cv::Mat gray8(const GrayFrame& f) {
if(!f.valid())return {};
cv::Mat out(f.height,f.width,CV_8UC1);
for(size_t i=0;i<f.pixels.size();++i) {
float x=f.pixels[i];
if(!std::isfinite(x))return {};
out.data[i]=cv::saturate_cast<uchar>(x*255.f);
}
return out;
}
void detect() {
std::vector<cv::Point> vertices,hull;
for(auto p:polygon)vertices.emplace_back(int(p.x),int(p.y));
points.clear();
if(vertices.size()<3)return;
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);
if(!points.empty())cv::cornerSubPix(previous,points,{10,10},{-1,-1},
{cv::TermCriteria::COUNT|cv::TermCriteria::EPS,20,.03});
}
TrackResult fail(TrackFailure why) {
running=false;value.valid=false;value.confidence=0;value.failure=why;return value;
}
};
OpenCVTracker::OpenCVTracker():impl_(std::make_shared<Impl>()){}
OpenCVTracker::~OpenCVTracker()=default;
void OpenCVTracker::setMask(std::vector<FlowPoint> p){impl_->mask=std::move(p);}
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;}
bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
auto& t=*impl_;t.running=false;t.frameIndex=1;t.points.clear();t.polygon.clear();
t.value={roi.cx,roi.cy,1,0,0,0,0,false};
if(!f.valid()||!std::isfinite(roi.cx)||!std::isfinite(roi.cy)||
!std::isfinite(roi.width)||!std::isfinite(roi.height)||!std::isfinite(roi.angle)||
roi.cx<0||roi.cx>1||roi.cy<0||roi.cy>1||roi.width<=0||roi.height<=0||roi.width>8||roi.height>8) {
t.fail(TrackFailure::InvalidFrame);return false;
}
try {
t.width=f.width;t.height=f.height;t.seed=roi;t.center={roi.cx*f.width,roi.cy*f.height};
t.previous=Impl::gray8(f);if(t.previous.empty()){t.fail(TrackFailure::InvalidFrame);return false;}
auto outline=t.mask;
if(outline.empty())outline={{-.5f,-.5f},{.5f,-.5f},{.5f,.5f},{-.5f,.5f}};
float c=std::cos(roi.angle),s=std::sin(roi.angle);
for(auto p:outline){float x=p.x*roi.width*f.width,y=p.y*roi.height*f.height;
if(!std::isfinite(x)||!std::isfinite(y)){t.fail(TrackFailure::InvalidFrame);return false;}
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;}
t.running=true;t.value.valid=true;t.value.confidence=1;return true;
} catch(const cv::Exception&){t.fail(TrackFailure::Backend);return false;}
}
TrackResult OpenCVTracker::track(const GrayFrame& f) {
auto& t=*impl_;
if(!t.running||!f.valid()||f.width!=t.width||f.height!=t.height)return t.fail(TrackFailure::InvalidFrame);
try {
auto next=Impl::gray8(f);if(next.empty())return t.fail(TrackFailure::InvalidFrame);
// The reset detected the seed (index zero). Advancing frames redetect
// on indices 1,2,4… in the corresponding previous image.
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> 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]);}
}
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);
for(size_t i=0;i<src.size();++i){src[i]-=t.center;dst[i]-=t.center;}
cv::Mat inliers;
cv::Mat affine=cv::estimateAffine2D(src,dst,inliers,cv::RANSAC,3.,2000,.99,10);
if(affine.empty())return t.fail(TrackFailure::IncoherentMotion);
double a=affine.at<double>(0,0),b=affine.at<double>(0,1),tx=affine.at<double>(0,2);
double c=affine.at<double>(1,0),d=affine.at<double>(1,1),ty=affine.at<double>(1,2);
double det=a*d-b*c,sx=std::hypot(a,c),sy=det/sx,angle=std::atan2(c,a);
if(!cv::checkRange(affine)||!(det>0)||!(sx>0)||!(sy>0))return t.fail(TrackFailure::InvalidTransform);
std::vector<float> errors;int accepted=0;
for(size_t i=0;i<src.size();++i)if(inliers.at<uchar>(int(i))){
++accepted;cv::Point2f q(float(a*src[i].x+b*src[i].y+tx),float(c*src[i].x+d*src[i].y+ty));
errors.push_back(float(cv::norm(q-dst[i])));}
t.value.inliers=accepted;
if(accepted<4)return t.fail(TrackFailure::IncoherentMotion);
std::nth_element(errors.begin(),errors.begin()+errors.size()/2,errors.end());
t.value.medianError=errors[errors.size()/2];
float confidence=std::clamp(float(accepted)/float(src.size())*std::exp(-t.value.medianError/3.f),0.f,1.f);
// Product guards are deliberate: no silent successful empty/degenerate fit.
double totalX=t.value.scaleX*sx,totalY=t.value.scaleY*sy;
auto center=t.center+cv::Point2f(float(tx),float(ty));
if(!std::isfinite(totalX)||!std::isfinite(totalY)||totalX<.1||totalX>10||totalY<.1||totalY>10||
sx<.5||sx>2||sy<.5||sy>2||std::abs(angle)>.6)return t.fail(TrackFailure::InvalidTransform);
if(confidence<.3f)return t.fail(TrackFailure::LowConfidence);
if(center.x<0||center.x>=t.width||center.y<0||center.y>=t.height)return t.fail(TrackFailure::OutsideFrame);
float cosA=float(std::cos(angle)),sinA=float(std::sin(angle));
for(auto& p:t.polygon){auto q=p-t.center;p={center.x+float(sx)*q.x*cosA-float(sy)*q.y*sinA,
center.y+float(sx)*q.x*sinA+float(sy)*q.y*cosA};}
t.center=center;t.value.cx=center.x/t.width;t.value.cy=center.y/t.height;
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;
}catch(const cv::Exception&){return t.fail(TrackFailure::Backend);}
}
}
+79
查看文件
@@ -0,0 +1,79 @@
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
#include <iostream>
#include <string>
#include <chrono>
// Known analytic motion is the oracle. No target app or runtime is used.
static float noise(int x,int y,uint32_t seed=0) {
uint32_t n=uint32_t(x)*73856093u^uint32_t(y)*19349663u^seed;
n^=n>>13;n*=1274126177u;n^=n>>16;return .1f+.8f*float(n&65535)/65535.f;
}
static float texture(float x,float y,uint32_t seed=0) {
float gx=x/3.f,gy=y/3.f;int ix=int(std::floor(gx)),iy=int(std::floor(gy));
float dx=gx-ix,dy=gy-iy;
return (1-dy)*((1-dx)*noise(ix,iy,seed)+dx*noise(ix+1,iy,seed))+
dy*((1-dx)*noise(ix,iy+1,seed)+dx*noise(ix+1,iy+1,seed));
}
static dy::GrayFrame frame(int w,int h,double tx=0,double ty=0,double angle=0,double sx=1,double sy=1,uint32_t seed=0) {
dy::GrayFrame f{w,h,std::vector<float>(size_t(w)*h)};
double cx=w*.5,cy=h*.5,c=std::cos(angle),s=std::sin(angle);
for(int y=0;y<h;y++)for(int x=0;x<w;x++){
double dx=x-cx-tx,dy=y-cy-ty;
f.pixels[y*w+x]=texture(float((c*dx+s*dy)/sx+cx),float((-s*dx+c*dy)/sy+cy),seed);
}return f;
}
int main() {
int failed=0,count=0;std::cout<<"{\"version\":\""<<dy::OpenCVTracker::version()<<"\",\"checks\":[";
auto check=[&](const char*name,bool ok,double error=0){if(count++)std::cout<<",";failed+=!ok;
std::cout<<"{\"name\":\""<<name<<"\",\"pass\":"<<(ok?"true":"false")<<",\"error\":"<<error<<"}";};
check("pinned-public-opencv-version",std::string(dy::OpenCVTracker::version())=="4.5.1");
{
dy::OpenCVTracker t;auto f=frame(640,480);bool started=t.reset(f,{.5,.5,.85,.85,0});
check("300-feature-capacity",started&&t.result().features==300,t.result().features);
auto r=t.track(frame(640,480,2.25,-1.5,.025,1.025,.98));
double error=std::hypot((r.cx-.5)*640-2.25,(r.cy-.5)*480+1.5);
check("translation-rotation-independent-xy-scale",started&&r.valid&&error<.5&&std::abs(r.rotation-.025)<.004&&
std::abs(r.scaleX-1.025)<.005&&std::abs(r.scaleY-.98)<.005,error);
}
{
dy::OpenCVTracker t;auto f=frame(512,384);t.setMask({{.8f,-.5f},{1.8f,-.5f},{1.8f,.5f},{.8f,.5f}});
bool ok=t.reset(f,{.35,.5,.2,.45,0});dy::TrackResult r=t.result();
for(int i=1;i<=12&&ok;++i){r=t.track(frame(512,384,1.25*i,.5*i));ok=r.valid;}
double error=std::hypot((r.cx-.35)*512-15,(r.cy-.5)*384-6);
check("free-path-outside-creation-box-12-frame-subpixel",ok&&error<.7,error);
}
{
dy::OpenCVTracker t;auto f=frame(640,480);bool ok=t.reset(f,{.5,.5,.65,.65,0});
for(int i=0;i<8&&ok;++i)ok=t.track(f).valid;
auto r=t.result();double error=std::hypot((r.cx-.5)*640,(r.cy-.5)*480);
check("periodic-redetection-keeps-static-position",ok&&error<.05,error);
r=t.track(frame(640,480,0,0,0,1,1,0x3344));
check("unrelated-cut-stops-with-last-trusted-position",!r.valid&&!t.active()&&std::abs(r.cx-.5)*640<.05);
dy::GrayFrame flat{640,480,std::vector<float>(640*480,.5f)};
check("flat-mask-rejected",!t.reset(flat,{.5,.5,.7,.7,0}));
}
{
dy::OpenCVTracker t;auto f=frame(640,480);bool ok=t.reset(f,{.5,.5,.9,.9,0});
auto moving=f;
for(int y=0;y<480;++y)for(int x=0;x<640;++x)
moving.pixels[y*640+x]=texture(float(x-(x<460?8:0)),float(y));
auto r=t.track(moving);double error=std::abs((r.cx-.5)*640-8);
check("moving-majority-prefers-subject-over-static-background",ok&&r.valid&&error<.5&&r.matches<r.features*.85,error);
}
{
dy::OpenCVTracker a,b;auto f=frame(640,480);bool ok=a.reset(f,{.3,.4,.3,.3,0})&&b.reset(f,{.7,.6,.3,.3,0});
auto x=a.track(frame(640,480,5,2));auto y=b.track(frame(640,480,-3,-4));
check("independent-tracker-objects-do-not-share-motion",ok&&x.valid&&y.valid&&std::abs((x.cx-.3)*640-5)<.5&&std::abs((y.cx-.7)*640+3)<.5);
auto old=a.result();x=a.track({});check("invalid-frame-preserves-authored-geometry",!x.valid&&x.cx==old.cx&&x.cy==old.cy&&x.rotation==old.rotation);
f.pixels[10]=std::numeric_limits<float>::quiet_NaN();
check("nonfinite-source-rejected",!a.reset(f,{.5,.5,.4,.4,0}));
}
{
auto begin=std::chrono::steady_clock::now();dy::OpenCVTracker t;bool ok=t.reset(frame(3840,2160),{.5,.5,.5,.5,0});
auto r=t.track(frame(3840,2160,8,4));double error=std::hypot((r.cx-.5)*3840-8,(r.cy-.5)*2160-4);
check("4k-complete-source-frame",ok&&r.valid&&error<.5,error);
auto ms=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-begin).count();
std::cerr<<"4K generated images plus tracking: "<<ms<<"ms\n";
}
std::cout<<"],\"pass\":"<<(failed?"false":"true")<<",\"count\":"<<count<<",\"runtimeTargetAppUsed\":false}\n";return failed?1:0;
}