Update position based on the new frame
| 171 | } |
| 172 | // Update position based on the new frame |
| 173 | cv::Rect KCFTracker::update(cv::Mat image) |
| 174 | { |
| 175 | if (_roi.x + _roi.width <= 0) _roi.x = -_roi.width + 1; |
| 176 | if (_roi.y + _roi.height <= 0) _roi.y = -_roi.height + 1; |
| 177 | if (_roi.x >= image.cols - 1) _roi.x = image.cols - 2; |
| 178 | if (_roi.y >= image.rows - 1) _roi.y = image.rows - 2; |
| 179 | |
| 180 | float cx = _roi.x + _roi.width / 2.0f; |
| 181 | float cy = _roi.y + _roi.height / 2.0f; |
| 182 | |
| 183 | |
| 184 | float peak_value; |
| 185 | cv::Point2f res = detect(_tmpl, getFeatures(image, 0, 1.0f), peak_value); |
| 186 | |
| 187 | if (scale_step != 1) { |
| 188 | // Test at a smaller _scale |
| 189 | float new_peak_value; |
| 190 | cv::Point2f new_res = detect(_tmpl, getFeatures(image, 0, 1.0f / scale_step), new_peak_value); |
| 191 | |
| 192 | if (scale_weight * new_peak_value > peak_value) { |
| 193 | res = new_res; |
| 194 | peak_value = new_peak_value; |
| 195 | _scale /= scale_step; |
| 196 | _roi.width /= scale_step; |
| 197 | _roi.height /= scale_step; |
| 198 | } |
| 199 | |
| 200 | // Test at a bigger _scale |
| 201 | new_res = detect(_tmpl, getFeatures(image, 0, scale_step), new_peak_value); |
| 202 | |
| 203 | if (scale_weight * new_peak_value > peak_value) { |
| 204 | res = new_res; |
| 205 | peak_value = new_peak_value; |
| 206 | _scale *= scale_step; |
| 207 | _roi.width *= scale_step; |
| 208 | _roi.height *= scale_step; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Adjust by cell size and _scale |
| 213 | _roi.x = cx - _roi.width / 2.0f + ((float) res.x * cell_size * _scale); |
| 214 | _roi.y = cy - _roi.height / 2.0f + ((float) res.y * cell_size * _scale); |
| 215 | |
| 216 | if (_roi.x >= image.cols - 1) _roi.x = image.cols - 1; |
| 217 | if (_roi.y >= image.rows - 1) _roi.y = image.rows - 1; |
| 218 | if (_roi.x + _roi.width <= 0) _roi.x = -_roi.width + 2; |
| 219 | if (_roi.y + _roi.height <= 0) _roi.y = -_roi.height + 2; |
| 220 | |
| 221 | assert(_roi.width >= 0 && _roi.height >= 0); |
| 222 | cv::Mat x = getFeatures(image, 0); |
| 223 | train(x, interp_factor); |
| 224 | |
| 225 | return _roi; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | // Detect object in the current frame. |