Replace every production tracking path with Apple Vision; remove OpenCV linkage

这个提交包含在:
mac 于 2026-11-06 19:40:07 +08:00 提交
1 父节点 dbab2ab0cc
当前提交 8af7013ad4
22 个文件已更改
+600 -43

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

+1 -1
查看文件
@@ -13,7 +13,7 @@
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
#import <os/log.h>
#include "MaskCore/DYTracker.hpp"
#include "MaskCore/DYOpenCVTracker.hpp"
#include "MaskCore/DYVisionRegionTracker.hpp"
#include "MaskCore/DYObjectTracker.hpp"
#include <atomic>
#include "DYViewerParameterBridge.inc.h"
+3 -1
查看文件
@@ -22,7 +22,9 @@
DYMaskParameters _trackingSeed;
DYStampMotion _trackingStampMotion;
dy::TrackerROI _trackingStampSourceSeed;
dy::OpenCVTracker _toolTracker,_stampSourceTracker;
dy::VisionRegionTracker _toolTracker,_stampSourceTracker;
std::vector<dy::FlowPoint> _trackingShapePolygon;
dy::TrackerROI _trackingObjectROI;
dy::ObjectTracker _objectTracker;
BOOL _trackingObjectMode;
NSUInteger _trackingObjectPending,_trackingObjectRepaired;
+5 -9
查看文件
@@ -218,7 +218,7 @@
}
std::vector<dy::FlowPoint>polygon;NSData *nodes=_trackingCandidate[@"nodes"];if(_trackingSeed.kind==DYMaskBezier){auto path=dyFlattenMask((const DYBezierNode*)nodes.bytes,nodes.length/sizeof(DYBezierNode));for(int i=0;i<path.count;i++)polygon.push_back({path.x[i],path.y[i]});}else if(_trackingSeed.kind==DYMaskRectangle)polygon={{-.5f,-.5f},{.5f,-.5f},{.5f,.5f},{-.5f,.5f}};else for(int i=0;i<128;i++){float angle=i*(2.f*M_PI/128);polygon.push_back({.5f*std::cos(angle),.5f*std::sin(angle)});}
if(_trackingStampMotion.active)_stampSourceTracker.setMask(polygon);
_toolTracker.setMask(std::move(polygon));return YES;
_trackingShapePolygon=polygon;_toolTracker.setMask(std::move(polygon));return YES;
}
- (BOOL)finishToolTrackingLoss:(const dy::TrackResult&)lost region:(NSString*)region atTime:(CMTime)time error:(NSError**)error {
NSString *reason=@"区域内运动不一致";
@@ -250,21 +250,17 @@
NSData *rgba=preview[@"rgba"];int width=[preview[@"width"]intValue],height=[preview[@"height"]intValue];
if(width<16||height<16||width>640||height>640||rgba.length!=size_t(width)*height*4)return [self finishToolAnalysis:@"目标跟踪未取得完整分析画面,原轨迹已保留" error:error];
if(!_trackingSamples.count){
// Vision consumes an axis-aligned object rectangle. Track the true bounds
// of a rotated authored shape, then apply only its uniform scale/position.
float aspect=float(width)/height,ca=std::cos(_trackingSeed.angle),sa=std::sin(_trackingSeed.angle);
float w=_trackingSeed.width*aspect,h=_trackingSeed.height;
BOOL ellipse=_trackingSeed.kind==DYMaskEllipse;
float bw=ellipse?std::hypot(w*ca,h*sa):fabs(w*ca)+fabs(h*sa);
float bh=ellipse?std::hypot(w*sa,h*ca):fabs(w*sa)+fabs(h*ca);
dy::TrackerROI roi={_trackingSeed.centerX,_trackingSeed.centerY,bw/aspect,bh,0};
dy::TrackerROI seed={_trackingSeed.centerX,_trackingSeed.centerY,_trackingSeed.width,_trackingSeed.height,_trackingSeed.angle};
dy::TrackerROI roi=dy::visionMaskBounds(seed,_trackingShapePolygon,float(width)/height);_trackingObjectROI=roi;
os_log(OS_LOG_DEFAULT,"dayu_object_track_worker main=%{public}d",(int)NSThread.isMainThread);
if(!_objectTracker.reset((const uint8_t*)rgba.bytes,width,height,_trackingFlip,roi,1./CMTimeGetSeconds(_trackingFrameDuration)))return [self finishToolAnalysis:@"无法建立目标外观,请调整蒙版后重试" error:error];
objectStep={{roi.cx,roi.cy,roi.width,roi.height,1},true,false,false,0};
}else objectStep=_objectTracker.track((const uint8_t*)rgba.bytes,width,height,_trackingFlip);
target=_objectTracker.result(objectStep);
target.cx+=(_trackingSeed.centerX-_trackingObjectROI.cx)*target.scale;target.cy+=(_trackingSeed.centerY-_trackingObjectROI.cy)*target.scale;
if(!target.valid)return [self finishToolAnalysis:@"目标未能重新确认,已停止;请调整蒙版后继续" error:error];
}else if(!_trackingSamples.count){
os_log(OS_LOG_DEFAULT,"dayu_vision_region_worker main=%{public}d stamp=%{public}d",(int)NSThread.isMainThread,(int)stamp);
dy::TrackerROI targetROI={_trackingSeed.centerX,_trackingSeed.centerY,_trackingSeed.width,_trackingSeed.height,_trackingSeed.angle};
if(!_toolTracker.reset(frame,targetROI))
return [self finishToolAnalysis:stamp?@"覆盖区域内纹理不足,原图章已保留":@"蒙版内纹理不足,原蒙版已保留" error:error];
+1 -1
查看文件
@@ -320,7 +320,7 @@ BOOL DYMosaicTracksMask(NSDictionary *doc,NSString *maskID){
NSDictionary *mosaic=doc[@"mosaic"],*mask=DYSharedMaskWithID(doc,maskID);
NSArray *ids=mosaic[@"maskIDs"]?: (mosaic[@"maskID"]?@[mosaic[@"maskID"]]:@[]);
DYMaskParameters p=DYMaskAtTime(mask,kCMTimeInvalid);
return mask&&(p.kind==DYMaskEllipse||p.kind==DYMaskRectangle)&&!mask[@"stampSource"]&&[mask[@"enabled"]boolValue]&&[mosaic[@"enabled"]boolValue]&&[mosaic[@"source"]isEqual:@"mask"]&&[ids containsObject:maskID];
return mask&&(p.kind<=DYMaskBezier)&&!mask[@"stampSource"]&&[mask[@"enabled"]boolValue]&&[mosaic[@"enabled"]boolValue]&&[mosaic[@"source"]isEqual:@"mask"]&&[ids containsObject:maskID];
}
BOOL DYRepairObjectTrackingGap(NSMutableArray *samples,DYMaskParameters next,NSUInteger gap){
if(!gap)return YES;if(samples.count<=gap||!dyValidMaskParameters(next))return NO;
@@ -10,7 +10,7 @@
NSMutableArray *updates=[NSMutableArray array];
id observer=[NSNotificationCenter.defaultCenter addObserverForName:@"DYToolTrackingStatus" object:host queue:nil usingBlock:^(NSNotification *note){if(note.userInfo[@"tracking"])[updates addObject:note.userInfo];}];
run.need(@"tracking-renders-valid-source-before-analysis",capture(0));run.record(@"inactive-tracking-avoids-full-frame-motion-readback",[[plugin valueForKey:@"toolCaptureDevices"]count]==0);int undoBefore=base->_undoStarts;request(1);BOOL callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:&error]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:&error];
for(int i=0;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:&error];}host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();NSDictionary *tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];DYMaskParameters end=DYMaskAtTime(tracked,timeAt(5));run.record(@"host-callback-opencv-tracking-six-exact-frames",callbacks&&[tracked[@"keys"]count]==6&&fabs(end.centerX-(.5+10./tw))<.004&&fabs(end.centerY-(.5+5./th))<.004,@{@"keyCount":@([tracked[@"keys"]count]),@"centerX":@(end.centerX),@"centerY":@(end.centerY),@"error":reason(error)});
for(int i=0;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:&error];}host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();NSDictionary *tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];DYMaskParameters end=DYMaskAtTime(tracked,timeAt(5));run.record(@"host-callback-vision-tracking-six-exact-frames",callbacks&&[tracked[@"keys"]count]==6&&fabs(end.centerX-(.5+10./tw))<.004&&fabs(end.centerY-(.5+5./th))<.004,@{@"keyCount":@([tracked[@"keys"]count]),@"centerX":@(end.centerX),@"centerY":@(end.centerY),@"error":reason(error)});
NSDictionary *lastPreview=updates.count>=2?updates[updates.count-2]:nil;
DYMaskParameters shown{};
[lastPreview[@"shape"] getBytes:&shown length:sizeof(shown)];
@@ -62,7 +62,7 @@
const NSUInteger w=3840,h=2160;DYDiagnosticTile *fine=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];Pixels pixels(w*h*4);
for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++){float encoded=x%3==1?.8f:.2f,linear=powf((encoded+.055f)/1.055f,2.4f);auto i=(y*w+x)*4;pixels[i]=pixels[i+1]=pixels[i+2]=linear;pixels[i+3]=1;}
[[fine metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h) mipmapLevel:0 withBytes:pixels.data() bytesPerRow:w*8];auto gray=[plugin toolGrayFrame:fine error:nil];double maximum=0;for(NSUInteger y=0;y<(NSUInteger)gray.height;y++)for(NSUInteger x=0;x<(NSUInteger)gray.width;x++){double code=x%3==1?.8:.2;double expected=std::round(std::pow(std::pow((code+.055)/1.055,2.4),1/2.2)*255)/255.;maximum=std::max(maximum,fabs(double(gray.pixels[y*gray.width+x])-expected));}
run.record(@"tracking-4k-frame-preserves-full-detail-for-opencv",gray.valid()&&gray.width==3840&&gray.height==2160&&maximum<.002,@{@"width":@(gray.width),@"height":@(gray.height),@"maximumError":@(maximum)});
run.record(@"tracking-4k-frame-preserves-full-detail-for-vision-crops",gray.valid()&&gray.width==3840&&gray.height==2160&&maximum<.002,@{@"width":@(gray.width),@"height":@(gray.height),@"maximumError":@(maximum)});
auto compact=[plugin toolGrayFrame:fine preview:nullptr error:nil];BOOL identical=compact.valid()&&compact.pixels.empty()&&compact.bytes.size()==w*h;
for(size_t i=0;i<compact.bytes.size()&&identical;++i)identical=compact.at(i)==gray.pixels[i];
run.record(@"tracking-metal-gray8-compact-readback-matches-float-reference",identical,@{@"readbackBytes":@(compact.bytes.size()),@"previousReadbackBytes":@(gray.pixels.size()*sizeof(float))});
@@ -178,9 +178,9 @@
objectDoc[@"mosaic"]=[NSMutableDictionary dictionaryWithDictionary:@{@"enabled":@YES,@"source":@"mask",@"maskIDs":@[ID],@"size":@16}];
run.record(@"mosaic-selected-mask-uses-object-tracking",DYMosaicTracksMask(objectDoc,ID)&&!DYMosaicTracksMask(objectDoc,@"missing"));
shape.kind=DYMaskBezier;objectMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];
run.record(@"bezier-mosaic-preserves-polygon-flow-tracking",!DYMosaicTracksMask(objectDoc,ID));
run.record(@"bezier-mosaic-uses-vision-object-tracking",DYMosaicTracksMask(objectDoc,ID));
shape.kind=DYMaskRectangle;objectMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];
objectDoc[@"mosaic"][@"enabled"]=@NO;run.record(@"disabled-mosaic-keeps-precise-flow-path",!DYMosaicTracksMask(objectDoc,ID));objectDoc[@"mosaic"][@"enabled"]=@YES;
objectDoc[@"mosaic"][@"enabled"]=@NO;run.record(@"disabled-mosaic-uses-precise-vision-registration",!DYMosaicTracksMask(objectDoc,ID));objectDoc[@"mosaic"][@"enabled"]=@YES;
DYWriteTools(base,objectDoc,kCMTimeZero);capture(0);host->_state=kFxAnalysisState_NotAnalyzing;
[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"shared",@"mask":ID,@"direction":@1}]];
BOOL ok=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
@@ -162,7 +162,7 @@ static BOOL DYViewerSanitizeValue(NSDictionary *d,double input,double *output) {
if(current&&[faces[@"boxes"]count])add([NSString stringWithFormat:@"将本帧 %lu 张人脸转为可选跟踪蒙版",(unsigned long)[faces[@"boxes"]count]],@{@"importFaces":faces,@"actionTime":[(NSDictionary*)CMTimeCopyAsDictionary(actionTime,kCFAllocatorDefault)autorelease]},NO);
else {NSMenuItem *hint=[menu addItemWithTitle:current?@"本帧未识别到人脸":@"先开启全部人脸,暂停后再次打开以指定人脸" action:nil keyEquivalent:@""];hint.enabled=NO;}
[menu addItem:NSMenuItem.separatorItem];
NSMenuItem *hint=[menu addItemWithTitle:@"勾选后逐个跟踪;矩形/椭圆支持短时预测与重新定位" action:nil keyEquivalent:@""];hint.enabled=NO;
NSMenuItem *hint=[menu addItemWithTitle:@"勾选后逐个跟踪;支持短时预测与重新定位" action:nil keyEquivalent:@""];hint.enabled=NO;
NSArray *IDs=settings[@"maskIDs"]?: (settings[@"maskID"]?@[settings[@"maskID"]]:@[]);NSInteger count=0;
for(NSDictionary *mask in DYSharedMasks(doc)){if(mask[@"stampSource"])continue;count++;DYMaskParameters p=DYMaskAtTime(mask,kCMTimeInvalid);NSString *shape=mask[@"label"]?:[NSString stringWithFormat:@"蒙版 %ld · %@",(long)count,@[@"椭圆",@"矩形",@"钢笔",@"渐变",@"HSL",@"亮度"][p.kind]];
add([shape stringByAppendingString:[mask[@"keys"]count]?@" · 已跟踪":@" · 静态"],@{@"toggleMask":mask[@"id"]},[settings[@"enabled"]boolValue]&&[settings[@"source"]isEqual:@"mask"]&&[IDs containsObject:mask[@"id"]]);
+1 -1
查看文件
@@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.19.0</string>
<key>CFBundleVersion</key>
<string>107</string>
<string>108</string>
<key>NSHumanReadableCopyright</key>
<string></string>
<key>PlugInKit</key>
@@ -0,0 +1,99 @@
#pragma once
#import <Vision/Vision.h>
#import <CoreVideo/CoreVideo.h>
#include "DYTracker.hpp"
namespace dy {
inline TrackerROI visionMaskBounds(TrackerROI roi,const std::vector<FlowPoint>& polygon,float aspect){
float x0=1e9f,y0=1e9f,x1=-1e9f,y1=-1e9f,c=std::cos(roi.angle),s=std::sin(roi.angle);
for(auto p:polygon){float x=p.x*roi.width*aspect,y=p.y*roi.height;
float xx=roi.cx+(x*c-y*s)/aspect,yy=roi.cy+x*s+y*c;
x0=std::min(x0,xx);x1=std::max(x1,xx);y0=std::min(y0,yy);y1=std::max(y1,yy);}
return polygon.size()<3?roi:TrackerROI{(x0+x1)*.5f,(y0+y1)*.5f,x1-x0,y1-y0,0};
}
// Precision path for authored masks and independently moving clone regions.
// Vision estimates the local projective motion. We validate it against pixels
// inside the actual polygon, then expose the existing center/scale/angle format.
// No OpenCV code or fallback is used.
class VisionRegionTracker {
struct H {
double a[9]={1,0,0,0,1,0,0,0,1};
FlowPoint map(FlowPoint p)const{double z=a[6]*p.x+a[7]*p.y+a[8];return {float((a[0]*p.x+a[1]*p.y+a[2])/z),float((a[3]*p.x+a[4]*p.y+a[5])/z)};}
static H mul(const H& l,const H& r){H h;for(int y=0;y<3;y++)for(int x=0;x<3;x++){h.a[y*3+x]=0;for(int k=0;k<3;k++)h.a[y*3+x]+=l.a[y*3+k]*r.a[k*3+x];}return h;}
} cumulative_;
GrayFrame previous_;
std::vector<FlowPoint> polygon_,samples_;
TrackerROI seed_{};
TrackResult result_;
int width_=0,height_=0;
bool active_=false;
static float pixel(const GrayFrame& f,float x,float y){
x=std::clamp(x,0.f,float(f.width-1));y=std::clamp(y,0.f,float(f.height-1));
int ix=int(x),iy=int(y),jx=std::min(ix+1,f.width-1),jy=std::min(iy+1,f.height-1);float fx=x-ix,fy=y-iy;
return (f.at(iy*f.width+ix)*(1-fx)+f.at(iy*f.width+jx)*fx)*(1-fy)+(f.at(jy*f.width+ix)*(1-fx)+f.at(jy*f.width+jx)*fx)*fy;
}
static bool inside(const std::vector<FlowPoint>& polygon,float x,float y){bool yes=false;for(size_t i=0,j=polygon.size()-1;i<polygon.size();j=i++){
auto a=polygon[i],b=polygon[j];if((a.y>y)!=(b.y>y))if(x<(b.x-a.x)*(y-a.y)/(b.y-a.y)+a.x)yes=!yes;}return yes;}
bool framePoint(FlowPoint p)const{return std::isfinite(p.x)&&std::isfinite(p.y)&&p.x>=1&&p.y>=1&&p.x<width_-2&&p.y<height_-2;}
double correlation(const GrayFrame& current,const H& next,int& count)const{
double a=0,b=0,aa=0,bb=0,ab=0;count=0;
for(auto p:samples_){auto l=cumulative_.map(p),r=next.map(p);if(!framePoint(l)||!framePoint(r))continue;double x=pixel(previous_,l.x,l.y),y=pixel(current,r.x,r.y);a+=x;b+=y;aa+=x*x;bb+=y*y;ab+=x*y;++count;}
if(count<30)return -1;double va=aa-a*a/count,vb=bb-b*b/count;if(va/count<.0004||vb/count<.0004)return -1;
return (ab-a*b/count)/std::sqrt(va*vb);
}
CVPixelBufferRef crop(const GrayFrame& frame,float ox,float oy,float rw,float rh,int w,int h){
CVPixelBufferRef p=nullptr;if(CVPixelBufferCreate(kCFAllocatorDefault,w,h,kCVPixelFormatType_32BGRA,nullptr,&p)!=kCVReturnSuccess)return nullptr;
CVPixelBufferLockBaseAddress(p,0);auto *out=(uint8_t*)CVPixelBufferGetBaseAddress(p);size_t stride=CVPixelBufferGetBytesPerRow(p);
for(int y=0;y<h;y++)for(int x=0;x<w;x++){uint8_t v=std::lround(std::clamp(pixel(frame,ox+x*rw/w,oy+y*rh/h),0.f,1.f)*255);auto *d=out+y*stride+x*4;d[0]=d[1]=d[2]=v;d[3]=255;}
CVPixelBufferUnlockBaseAddress(p,0);return p;
}
TrackResult fail(TrackFailure reason){active_=false;result_.valid=false;result_.failure=reason;return result_;}
public:
void setMask(std::vector<FlowPoint> p){polygon_=std::move(p);}
void stop(){active_=false;previous_={};samples_.clear();}
bool active()const{return active_;}
TrackResult result()const{return result_;}
bool reset(const GrayFrame& frame,TrackerROI roi){
stop();result_={};seed_=roi;width_=frame.width;height_=frame.height;cumulative_=H{};
if(!frame.valid()||!std::isfinite(roi.cx)||!std::isfinite(roi.cy)||!std::isfinite(roi.width)||!std::isfinite(roi.height)||!std::isfinite(roi.angle)||roi.width<=0||roi.height<=0)return false;
if(polygon_.size()<3)polygon_={{-.5,-.5},{.5,-.5},{.5,.5},{-.5,.5}};
float loX=1e9,loY=1e9,hiX=-1e9,hiY=-1e9;for(auto p:polygon_){loX=std::min(loX,p.x);hiX=std::max(hiX,p.x);loY=std::min(loY,p.y);hiY=std::max(hiY,p.y);}
float c=std::cos(roi.angle),s=std::sin(roi.angle);
for(int y=0;y<24;y++)for(int x=0;x<24;x++){
float u=loX+(hiX-loX)*(x+.5f)/24,v=loY+(hiY-loY)*(y+.5f)/24;if(!inside(polygon_,u,v))continue;
float xx=u*roi.width*width_,yy=v*roi.height*height_;FlowPoint p{roi.cx*width_+xx*c-yy*s,roi.cy*height_+xx*s+yy*c};if(framePoint(p))samples_.push_back(p);
}
previous_=frame;int count;double score=correlation(frame,cumulative_,count);if(score<.6){stop();result_.failure=TrackFailure::InsufficientTexture;return false;}
result_.cx=roi.cx;result_.cy=roi.cy;result_.valid=true;result_.confidence=1;result_.features=result_.inliers=count;active_=true;return true;
}
TrackResult track(const GrayFrame& frame){
if(!active_||!frame.valid()||frame.width!=width_||frame.height!=height_)return fail(TrackFailure::InvalidFrame);
float x0=1e9,y0=1e9,x1=-1e9,y1=-1e9;
for(auto p:samples_){auto q=cumulative_.map(p);x0=std::min(x0,q.x);y0=std::min(y0,q.y);x1=std::max(x1,q.x);y1=std::max(y1,q.y);}
float cx=(x0+x1)*.5f,cy=(y0+y1)*.5f,rw=std::min(float(width_),std::max(96.f,(x1-x0)*1.5f)),rh=std::min(float(height_),std::max(96.f,(y1-y0)*1.5f));
float ox=std::clamp(cx-rw*.5f,0.f,width_-rw),oy=std::clamp(cy-rh*.5f,0.f,height_-rh);
float factor=std::min(1.f,320.f/std::max(rw,rh));int w=std::max(16,int(std::lround(rw*factor))),h=std::max(16,int(std::lround(rh*factor)));
CVPixelBufferRef from=crop(previous_,ox,oy,rw,rh,w,h),to=crop(frame,ox,oy,rw,rh,w,h);if(!from||!to){if(from)CVPixelBufferRelease(from);if(to)CVPixelBufferRelease(to);return fail(TrackFailure::Backend);}
H m;bool found=false;
@autoreleasepool {
auto request=[[VNHomographicImageRegistrationRequest alloc]initWithTargetedCVPixelBuffer:from options:@{}];
auto handler=[[VNImageRequestHandler alloc]initWithCVPixelBuffer:to options:@{}];NSError *error=nil;
if([handler performRequests:@[request] error:&error]&&request.results.count){auto warp=request.results.firstObject.warpTransform;found=true;for(int y=0;y<3;y++)for(int x=0;x<3;x++){m.a[y*3+x]=warp.columns[x][y];found&=std::isfinite(m.a[y*3+x]);}}
[handler release];[request release];
}
CVPixelBufferRelease(from);CVPixelBufferRelease(to);if(!found)return fail(TrackFailure::Backend);
double sx=w/rw,sy=h/rh;H into{{sx,0,-ox*sx,0,-sy,h-1+oy*sy,0,0,1}},out{{1/sx,0,ox,0,-1/sy,oy+(h-1)/sy,0,0,1}};
H step=H::mul(out,H::mul(m,into)),next=H::mul(step,cumulative_);
FlowPoint seed{seed_.cx*width_,seed_.cy*height_};auto center=next.map(seed),before=cumulative_.map(seed);
float c=std::cos(seed_.angle),s=std::sin(seed_.angle);auto xp=next.map({seed.x+20*c,seed.y+20*s}),yp=next.map({seed.x-20*s,seed.y+20*c});
float ax=(xp.x-center.x)/20,ay=(xp.y-center.y)/20,bx=(yp.x-center.x)/20,by=(yp.y-center.y)/20;
float scaleX=std::hypot(ax,ay),scaleY=std::hypot(bx,by),angle=std::atan2(ay,ax)-seed_.angle;
if(!framePoint(center)||!std::isfinite(scaleX)||!std::isfinite(scaleY)||scaleX<.0625||scaleY<.0625||scaleX>16||scaleY>16||ax*by-ay*bx<=0||
scaleX/result_.scaleX<.8||scaleX/result_.scaleX>1.25||scaleY/result_.scaleY<.8||scaleY/result_.scaleY>1.25||std::abs(std::remainder(angle-result_.rotation,float(2*M_PI)))>.2||std::hypot(center.x-before.x,center.y-before.y)>std::max(rw,rh)*.3)
return fail(TrackFailure::InvalidTransform);
int count;double ncc=correlation(frame,next,count);if(ncc<.65||count<samples_.size()*.6)return fail(TrackFailure::LowConfidence);
cumulative_=next;previous_=frame;result_.cx=center.x/width_;result_.cy=center.y/height_;result_.scaleX=scaleX;result_.scaleY=scaleY;result_.scale=std::sqrt(scaleX*scaleY);result_.rotation=angle;result_.confidence=ncc;result_.valid=true;result_.features=result_.inliers=result_.matches=count;result_.failure=TrackFailure::None;return result_;
}
};
}