文件
dayu-color/tools/tracking/DYOpenCVTracker.cpp
T

157 行
9.1 KiB
C++

// 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::shared_ptr<FlowBackend> flow;
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);
// 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});
}
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);}
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;}
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;}
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;}
}
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> 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);
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]);
}
}
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);
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);}
}
}