// This file is part of VisionCPP, a lightweight C++ template library
// for computer vision and image processing.
//
// Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
//
// Contact: visioncpp@codeplay.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// \file optical_flow_LK.cc
///
/// -------Example--------
/// Lucas-Kanade Optical Flow
///
/// \brief This example implements the Lucas-Kanade Optical Flow
/// This optical flow needs to solve the following equation
/// | u | = inv(sum w | Dx^2 Dx*Dy | ) * sum w | -Dx*Dt |
/// | v | ( | Dx*Dy Dy^2 | ) | -Dy*Dt |
/// where w is the 3x3 window of ones
///
/// doing algebraic manipulation
/// n = sum(Dx^2) * sum (Dy^2) - (sum (Dx * Dy)^2)
/// u = (-sum (Dy^2) * sum (Dx*Dt) + sum (Dx*Dy) * sum (Dy*Dt)) / n
/// v = ( sum (Dx*Dt) * sum (Dx*Dy) - sum (Dx^2) * sum (Dy*Dt)) / n
// include OpenCV for camera display
#include
// include VisionCPP
#include
// operator that transforms uv coordinates into polar coordinates
// it was created for displaying the optical flow in RGB
struct OP_UVtoPolar {
visioncpp::pixel::F32C3 operator()(visioncpp::pixel::F32C2 t) {
float intensity = cl::sycl::clamp(
cl::sycl::sqrt(t[0] * t[0] + t[1] * t[1]) / 2.0f, 0.0f, 1.0f);
float angle = cl::sycl::atan2(t[1], t[0]) / (2.0f * M_PI);
float chn = 1.0f;
return visioncpp::pixel::F32C3(angle, chn, intensity);
}
};
// main program
int main(int argc, char **argv) {
// open video or camera
cv::VideoCapture cap;
if (argc == 1) {
cap.open(0);
std::cout