|
| 1 | +// This file is part of the OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +// |
| 6 | +//[1] David S. Bolme et al. "Visual Object Tracking using Adaptive Correlation Filters" |
| 7 | +// http://www.cs.colostate.edu/~draper/papers/bolme_cvpr10.pdf |
| 8 | +// |
| 9 | + |
| 10 | +// |
| 11 | +// credits: |
| 12 | +// Kun-Hsin Chen: for initial c++ code |
| 13 | +// Cracki: for the idea of only converting the used patch to gray |
| 14 | +// |
| 15 | + |
| 16 | +#include "opencv2/tracking.hpp" |
| 17 | + |
| 18 | +namespace cv { |
| 19 | +namespace tracking { |
| 20 | + |
| 21 | +struct DummyModel : TrackerModel |
| 22 | +{ |
| 23 | + virtual void modelUpdateImpl(){} |
| 24 | + virtual void modelEstimationImpl( const std::vector<Mat>& ){} |
| 25 | +}; |
| 26 | + |
| 27 | + |
| 28 | +const double eps=0.00001; // for normalization |
| 29 | +const double rate=0.2; // learning rate |
| 30 | +const double psrThreshold=5.7; // no detection, if PSR is smaller than this |
| 31 | + |
| 32 | + |
| 33 | +struct MosseImpl : TrackerMOSSE |
| 34 | +{ |
| 35 | +protected: |
| 36 | + |
| 37 | + Point2d center; //center of the bounding box |
| 38 | + Size size; //size of the bounding box |
| 39 | + Mat hanWin; |
| 40 | + Mat G; //goal |
| 41 | + Mat H, A, B; //state |
| 42 | + |
| 43 | + // Element-wise division of complex numbers in src1 and src2 |
| 44 | + Mat divDFTs( const Mat &src1, const Mat &src2 ) const |
| 45 | + { |
| 46 | + Mat c1[2],c2[2],a1,a2,s1,s2,denom,re,im; |
| 47 | + |
| 48 | + // split into re and im per src |
| 49 | + cv::split(src1, c1); |
| 50 | + cv::split(src2, c2); |
| 51 | + |
| 52 | + // (Re2*Re2 + Im2*Im2) = denom |
| 53 | + // denom is same for both channels |
| 54 | + cv::multiply(c2[0], c2[0], s1); |
| 55 | + cv::multiply(c2[1], c2[1], s2); |
| 56 | + cv::add(s1, s2, denom); |
| 57 | + |
| 58 | + // (Re1*Re2 + Im1*Im1)/(Re2*Re2 + Im2*Im2) = Re |
| 59 | + cv::multiply(c1[0], c2[0], a1); |
| 60 | + cv::multiply(c1[1], c2[1], a2); |
| 61 | + cv::divide(a1+a2, denom, re, 1.0 ); |
| 62 | + |
| 63 | + // (Im1*Re2 - Re1*Im2)/(Re2*Re2 + Im2*Im2) = Im |
| 64 | + cv::multiply(c1[1], c2[0], a1); |
| 65 | + cv::multiply(c1[0], c2[1], a2); |
| 66 | + cv::divide(a1+a2, denom, im, -1.0); |
| 67 | + |
| 68 | + // Merge Re and Im back into a complex matrix |
| 69 | + Mat dst, chn[] = {re,im}; |
| 70 | + cv::merge(chn, 2, dst); |
| 71 | + return dst; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + void preProcess( Mat &window ) const |
| 76 | + { |
| 77 | + window.convertTo(window, CV_32F); |
| 78 | + log(window + 1.0f, window); |
| 79 | + |
| 80 | + //normalize |
| 81 | + Scalar mean,StdDev; |
| 82 | + meanStdDev(window, mean, StdDev); |
| 83 | + window = (window-mean[0]) / (StdDev[0]+eps); |
| 84 | + |
| 85 | + //Gaussain weighting |
| 86 | + window = window.mul(hanWin); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + double correlate( const Mat &image_sub, Point &delta_xy ) const |
| 91 | + { |
| 92 | + Mat IMAGE_SUB, RESPONSE, response; |
| 93 | + // filter in dft space |
| 94 | + dft(image_sub, IMAGE_SUB, DFT_COMPLEX_OUTPUT); |
| 95 | + mulSpectrums(IMAGE_SUB, H, RESPONSE, 0, true ); |
| 96 | + idft(RESPONSE, response, DFT_SCALE|DFT_REAL_OUTPUT); |
| 97 | + // update center position |
| 98 | + double maxVal; Point maxLoc; |
| 99 | + minMaxLoc(response, 0, &maxVal, 0, &maxLoc); |
| 100 | + delta_xy.x = maxLoc.x - int(response.size().width/2); |
| 101 | + delta_xy.y = maxLoc.y - int(response.size().height/2); |
| 102 | + // normalize response |
| 103 | + Scalar mean,std; |
| 104 | + meanStdDev(response, mean, std); |
| 105 | + return (maxVal-mean[0]) / (std[0]+eps); // PSR |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + Mat randWarp( const Mat& a ) const |
| 110 | + { |
| 111 | + cv::RNG rng(8031965); |
| 112 | + |
| 113 | + // random rotation |
| 114 | + double C=0.1; |
| 115 | + double ang = rng.uniform(-C,C); |
| 116 | + double c=cos(ang), s=sin(ang); |
| 117 | + // affine warp matrix |
| 118 | + Mat_<float> W(2,3); |
| 119 | + W << c + rng.uniform(-C,C), -s + rng.uniform(-C,C), 0, |
| 120 | + s + rng.uniform(-C,C), c + rng.uniform(-C,C), 0; |
| 121 | + |
| 122 | + // random translation |
| 123 | + Mat_<float> center_warp(2, 1); |
| 124 | + center_warp << a.cols/2, a.rows/2; |
| 125 | + W.col(2) = center_warp - (W.colRange(0, 2))*center_warp; |
| 126 | + |
| 127 | + Mat warped; |
| 128 | + warpAffine(a, warped, W, a.size(), BORDER_REFLECT); |
| 129 | + return warped; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + virtual bool initImpl( const Mat& image, const Rect2d& boundingBox ) |
| 134 | + { |
| 135 | + model = makePtr<DummyModel>(); |
| 136 | + |
| 137 | + Mat img; |
| 138 | + if (image.channels() == 1) |
| 139 | + img = image; |
| 140 | + else |
| 141 | + cvtColor(image, img, COLOR_BGR2GRAY); |
| 142 | + |
| 143 | + int w = getOptimalDFTSize(int(boundingBox.width)); |
| 144 | + int h = getOptimalDFTSize(int(boundingBox.height)); |
| 145 | + |
| 146 | + //Get the center position |
| 147 | + int x1 = int(floor((2*boundingBox.x+boundingBox.width-w)/2)); |
| 148 | + int y1 = int(floor((2*boundingBox.y+boundingBox.height-h)/2)); |
| 149 | + center.x = x1 + (w)/2; |
| 150 | + center.y = y1 + (h)/2; |
| 151 | + size.width = w; |
| 152 | + size.height = h; |
| 153 | + |
| 154 | + Mat window; |
| 155 | + getRectSubPix(img, size, center, window); |
| 156 | + createHanningWindow(hanWin, size, CV_32F); |
| 157 | + |
| 158 | + // goal |
| 159 | + Mat g=Mat::zeros(size,CV_32F); |
| 160 | + g.at<float>(h/2, w/2) = 1; |
| 161 | + GaussianBlur(g, g, Size(-1,-1), 2.0); |
| 162 | + double maxVal; |
| 163 | + minMaxLoc(g, 0, &maxVal); |
| 164 | + g = g / maxVal; |
| 165 | + dft(g, G, DFT_COMPLEX_OUTPUT); |
| 166 | + |
| 167 | + // initial A,B and H |
| 168 | + A = Mat::zeros(G.size(), G.type()); |
| 169 | + B = Mat::zeros(G.size(), G.type()); |
| 170 | + for(int i=0; i<8; i++) |
| 171 | + { |
| 172 | + Mat window_warp = randWarp(window); |
| 173 | + preProcess(window_warp); |
| 174 | + |
| 175 | + Mat WINDOW_WARP, A_i, B_i; |
| 176 | + dft(window_warp, WINDOW_WARP, DFT_COMPLEX_OUTPUT); |
| 177 | + mulSpectrums(G , WINDOW_WARP, A_i, 0, true); |
| 178 | + mulSpectrums(WINDOW_WARP, WINDOW_WARP, B_i, 0, true); |
| 179 | + A+=A_i; |
| 180 | + B+=B_i; |
| 181 | + } |
| 182 | + H = divDFTs(A,B); |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + virtual bool updateImpl( const Mat& image, Rect2d& boundingBox ) |
| 187 | + { |
| 188 | + if (H.empty()) // not initialized |
| 189 | + return false; |
| 190 | + |
| 191 | + Mat image_sub; |
| 192 | + getRectSubPix(image, size, center, image_sub); |
| 193 | + |
| 194 | + if (image_sub.channels() != 1) |
| 195 | + cvtColor(image_sub, image_sub, COLOR_BGR2GRAY); |
| 196 | + preProcess(image_sub); |
| 197 | + |
| 198 | + Point delta_xy; |
| 199 | + double PSR = correlate(image_sub, delta_xy); |
| 200 | + if (PSR < psrThreshold) |
| 201 | + return false; |
| 202 | + |
| 203 | + //update location |
| 204 | + center.x += delta_xy.x; |
| 205 | + center.y += delta_xy.y; |
| 206 | + |
| 207 | + Mat img_sub_new; |
| 208 | + getRectSubPix(image, size, center, img_sub_new); |
| 209 | + if (img_sub_new.channels() != 1) |
| 210 | + cvtColor(img_sub_new, img_sub_new, COLOR_BGR2GRAY); |
| 211 | + preProcess(img_sub_new); |
| 212 | + |
| 213 | + // new state for A and B |
| 214 | + Mat F, A_new, B_new; |
| 215 | + dft(img_sub_new, F, DFT_COMPLEX_OUTPUT); |
| 216 | + mulSpectrums(G, F, A_new, 0, true ); |
| 217 | + mulSpectrums(F, F, B_new, 0, true ); |
| 218 | + |
| 219 | + // update A ,B, and H |
| 220 | + A = A*(1-rate) + A_new*rate; |
| 221 | + B = B*(1-rate) + B_new*rate; |
| 222 | + H = divDFTs(A, B); |
| 223 | + |
| 224 | + // return tracked rect |
| 225 | + double x=center.x, y=center.y; |
| 226 | + int w = size.width, h=size.height; |
| 227 | + boundingBox = Rect2d(Point2d(x-0.5*w, y-0.5*h), Point2d(x+0.5*w, y+0.5*h)); |
| 228 | + return true; |
| 229 | + } |
| 230 | + |
| 231 | +public: |
| 232 | + MosseImpl() { isInit = 0; } |
| 233 | + |
| 234 | + // dummy implementation. |
| 235 | + virtual void read( const FileNode& ){} |
| 236 | + virtual void write( FileStorage& ) const{} |
| 237 | + |
| 238 | +}; // MosseImpl |
| 239 | + |
| 240 | +} // tracking |
| 241 | + |
| 242 | + |
| 243 | +Ptr<TrackerMOSSE> TrackerMOSSE::create() |
| 244 | +{ |
| 245 | + return makePtr<tracking::MosseImpl>(); |
| 246 | +} |
| 247 | + |
| 248 | + |
| 249 | +} // cv |
0 commit comments