Keep stable OpenCV flow with compact Metal capture and pyramid reuse

这个提交包含在:
mac 于 2026-11-06 18:01:57 +08:00 提交
1 父节点 48ac192d4b
当前提交 1cc7f8edb2
15 个文件已更改
+184 -29

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

+1 -1
查看文件
@@ -82,7 +82,7 @@ def main():
if dest.is_symlink():dest.unlink()
if not dest.exists():dest.symlink_to(src)
bridge=ROOT/'tools/tracking/DYOpenCVTracker.cpp';obj=libs/'DYOpenCVTracker.o';runtime=libs/'libDayuOpenCVTracker.a'
identity=hashlib.sha256(bridge.read_bytes()+(ROOT/'大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp').read_bytes()+marker.read_bytes()).hexdigest()
identity=hashlib.sha256(bridge.read_bytes()+(ROOT/'大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp').read_bytes()+(ROOT/'大鱼调色/Plugin/MaskCore/DYTracker.hpp').read_bytes()+marker.read_bytes()).hexdigest()
bridgeMarker=CACHE/'bridge-build.json'
if not runtime.exists() or not bridgeMarker.exists() or json.loads(bridgeMarker.read_text()).get('identity')!=identity:
run(['xcrun','clang++','-std=c++17','-arch','arm64','-mmacosx-version-min=14.6','-O3','-fvisibility=hidden',
+11 -4
查看文件
@@ -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);}
}
}
+4 -3
查看文件
@@ -6,16 +6,17 @@
#include <fstream>
#include <chrono>
int main(int argc,const char **argv){@autoreleasepool{
if(argc!=9)return 2;int w=atoi(argv[2]),h=atoi(argv[3]);if(w<16||h<16||w>8192||h>8192)return 2;
if(argc!=9&&argc!=10)return 2;int w=atoi(argv[2]),h=atoi(argv[3]);if(w<16||h<16||w>8192||h>8192)return 2;
bool compact=argc==10&&strcmp(argv[9],"compact")==0;
dy::TrackerROI roi{float(atof(argv[5])),float(atof(argv[6])),float(atof(argv[7])),float(atof(argv[8])),0};
NSMutableArray *runs=[NSMutableArray array];
for(int gpu=0;gpu<2;++gpu){
for(int gpu=0;gpu<(argc==10?1:2);++gpu){
dy::OpenCVTracker tracker;if(gpu){auto b=DYToolCreateMetalFlow([NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[4]]],nullptr);if(!b)return 3;tracker.setFlowBackend(b);}
std::ifstream input(argv[1],std::ios::binary);if(!input)return 2;
std::vector<uint8_t> bytes(size_t(w)*h);dy::GrayFrame f{w,h,std::vector<float>(bytes.size())};
NSMutableArray *frames=[NSMutableArray array];double total=0,maximum=0;
while(input.read((char*)bytes.data(),bytes.size())){
for(size_t i=0;i<bytes.size();++i)f.pixels[i]=bytes[i]/255.f;
if(compact){f.pixels.clear();f.bytes=bytes;}else for(size_t i=0;i<bytes.size();++i)f.pixels[i]=bytes[i]/255.f;
auto start=std::chrono::steady_clock::now();bool ok=frames.count?tracker.track(f).valid:tracker.reset(f,roi);
double ms=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-start).count();total+=ms;maximum=std::max(maximum,ms);auto r=tracker.result();
[frames addObject:@{@"valid":@(ok),@"cx":@(r.cx),@"cy":@(r.cy),@"scaleX":@(r.scaleX),@"scaleY":@(r.scaleY),@"rotation":@(r.rotation),@"confidence":@(r.confidence),@"failure":@((int)r.failure)}];if(!ok)break;
+10
查看文件
@@ -75,5 +75,15 @@ int main() {
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";
}
{
dy::OpenCVTracker floats,bytes;bool ok=true;double difference=0;
for(int i=0;i<12;++i){auto f=frame(640,480,1.25*i,.5*i);dy::GrayFrame b;b.width=f.width;b.height=f.height;
b.bytes.resize(f.pixels.size());for(size_t p=0;p<f.pixels.size();++p){b.bytes[p]=uint8_t(std::lround(f.pixels[p]*255));f.pixels[p]=b.bytes[p]/255.f;}
b.flipVertically();b.flipVertically();
ok&=i?floats.track(f).valid&&bytes.track(b).valid:floats.reset(f,{.5,.5,.4,.4,0})&&bytes.reset(b,{.5,.5,.4,.4,0});
auto a=floats.result(),c=bytes.result();difference=std::max(difference,std::hypot(double(a.cx-c.cx)*640,double(a.cy-c.cy)*480));
}
check("compact-gpu-byte-input-matches-quantized-float-and-flip",ok&&difference<1e-5,difference);
}
std::cout<<"],\"pass\":"<<(failed?"false":"true")<<",\"count\":"<<count<<",\"runtimeTargetAppUsed\":false}\n";return failed?1:0;
}