optical_flow.cpp 6.32 KB
Newer Older
wester committed
1 2 3
#include <iostream>
#include <fstream>

wester committed
4 5 6
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
wester committed
7 8 9

using namespace std;
using namespace cv;
wester committed
10
using namespace cv::gpu;
wester committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

inline bool isFlowCorrect(Point2f u)
{
    return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
}

static Vec3b computeColor(float fx, float fy)
{
    static bool first = true;

    // relative lengths of color transitions:
    // these are chosen based on perceptual similarity
    // (e.g. one can distinguish more shades between red and yellow
    //  than between yellow and green)
    const int RY = 15;
    const int YG = 6;
    const int GC = 4;
    const int CB = 11;
    const int BM = 13;
    const int MR = 6;
    const int NCOLS = RY + YG + GC + CB + BM + MR;
    static Vec3i colorWheel[NCOLS];

    if (first)
    {
        int k = 0;

        for (int i = 0; i < RY; ++i, ++k)
            colorWheel[k] = Vec3i(255, 255 * i / RY, 0);

        for (int i = 0; i < YG; ++i, ++k)
            colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);

        for (int i = 0; i < GC; ++i, ++k)
            colorWheel[k] = Vec3i(0, 255, 255 * i / GC);

        for (int i = 0; i < CB; ++i, ++k)
            colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);

        for (int i = 0; i < BM; ++i, ++k)
            colorWheel[k] = Vec3i(255 * i / BM, 0, 255);

        for (int i = 0; i < MR; ++i, ++k)
            colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);

        first = false;
    }

    const float rad = sqrt(fx * fx + fy * fy);
    const float a = atan2(-fy, -fx) / (float) CV_PI;

    const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
    const int k0 = static_cast<int>(fk);
    const int k1 = (k0 + 1) % NCOLS;
    const float f = fk - k0;

    Vec3b pix;

    for (int b = 0; b < 3; b++)
    {
        const float col0 = colorWheel[k0][b] / 255.0f;
        const float col1 = colorWheel[k1][b] / 255.0f;

        float col = (1 - f) * col0 + f * col1;

        if (rad <= 1)
            col = 1 - rad * (1 - col); // increase saturation with radius
        else
            col *= .75; // out of range

        pix[2 - b] = static_cast<uchar>(255.0 * col);
    }

    return pix;
}

static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy, Mat& dst, float maxmotion = -1)
{
    dst.create(flowx.size(), CV_8UC3);
    dst.setTo(Scalar::all(0));

    // determine motion range:
    float maxrad = maxmotion;

    if (maxmotion <= 0)
    {
        maxrad = 1;
        for (int y = 0; y < flowx.rows; ++y)
        {
            for (int x = 0; x < flowx.cols; ++x)
            {
                Point2f u(flowx(y, x), flowy(y, x));

                if (!isFlowCorrect(u))
                    continue;

                maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
            }
        }
    }

    for (int y = 0; y < flowx.rows; ++y)
    {
        for (int x = 0; x < flowx.cols; ++x)
        {
            Point2f u(flowx(y, x), flowy(y, x));

            if (isFlowCorrect(u))
                dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
        }
    }
}

wester committed
124
static void showFlow(const char* name, const GpuMat& d_flowx, const GpuMat& d_flowy)
wester committed
125
{
wester committed
126 127
    Mat flowx(d_flowx);
    Mat flowy(d_flowy);
wester committed
128 129 130 131 132 133 134 135 136 137 138

    Mat out;
    drawOpticalFlow(flowx, flowy, out, 10);

    imshow(name, out);
}

int main(int argc, const char* argv[])
{
    if (argc < 3)
    {
wester committed
139 140
        cerr << "Usage : " << argv[0] << "<frame0> <frame1>" << endl;
        return -1;
wester committed
141 142
    }

wester committed
143 144
    Mat frame0 = imread(argv[1], IMREAD_GRAYSCALE);
    Mat frame1 = imread(argv[2], IMREAD_GRAYSCALE);
wester committed
145 146 147

    if (frame0.empty())
    {
wester committed
148
        cerr << "Can't open image ["  << argv[1] << "]" << endl;
wester committed
149 150 151 152
        return -1;
    }
    if (frame1.empty())
    {
wester committed
153
        cerr << "Can't open image ["  << argv[2] << "]" << endl;
wester committed
154 155 156 157 158 159 160 161 162 163 164 165
        return -1;
    }

    if (frame1.size() != frame0.size())
    {
        cerr << "Images should be of equal sizes" << endl;
        return -1;
    }

    GpuMat d_frame0(frame0);
    GpuMat d_frame1(frame1);

wester committed
166 167
    GpuMat d_flowx(frame0.size(), CV_32FC1);
    GpuMat d_flowy(frame0.size(), CV_32FC1);
wester committed
168

wester committed
169 170 171 172 173
    BroxOpticalFlow brox(0.197f, 50.0f, 0.8f, 10, 77, 10);
    PyrLKOpticalFlow lk; lk.winSize = Size(7, 7);
    FarnebackOpticalFlow farn;
    OpticalFlowDual_TVL1_GPU tvl1;
    FastOpticalFlowBM fastBM;
wester committed
174 175 176 177 178 179 180 181 182 183

    {
        GpuMat d_frame0f;
        GpuMat d_frame1f;

        d_frame0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
        d_frame1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);

        const int64 start = getTickCount();

wester committed
184
        brox(d_frame0f, d_frame1f, d_flowx, d_flowy);
wester committed
185 186 187 188

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "Brox : " << timeSec << " sec" << endl;

wester committed
189
        showFlow("Brox", d_flowx, d_flowy);
wester committed
190 191 192 193 194
    }

    {
        const int64 start = getTickCount();

wester committed
195
        lk.dense(d_frame0, d_frame1, d_flowx, d_flowy);
wester committed
196 197 198 199

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "LK : " << timeSec << " sec" << endl;

wester committed
200
        showFlow("LK", d_flowx, d_flowy);
wester committed
201 202 203 204 205
    }

    {
        const int64 start = getTickCount();

wester committed
206
        farn(d_frame0, d_frame1, d_flowx, d_flowy);
wester committed
207 208 209 210

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "Farn : " << timeSec << " sec" << endl;

wester committed
211
        showFlow("Farn", d_flowx, d_flowy);
wester committed
212 213 214 215 216
    }

    {
        const int64 start = getTickCount();

wester committed
217
        tvl1(d_frame0, d_frame1, d_flowx, d_flowy);
wester committed
218 219 220 221

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "TVL1 : " << timeSec << " sec" << endl;

wester committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        showFlow("TVL1", d_flowx, d_flowy);
    }

    {
        const int64 start = getTickCount();

        GpuMat buf;
        calcOpticalFlowBM(d_frame0, d_frame1, Size(7, 7), Size(1, 1), Size(21, 21), false, d_flowx, d_flowy, buf);

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "BM : " << timeSec << " sec" << endl;

        showFlow("BM", d_flowx, d_flowy);
    }

    {
        const int64 start = getTickCount();

        fastBM(d_frame0, d_frame1, d_flowx, d_flowy);

        const double timeSec = (getTickCount() - start) / getTickFrequency();
        cout << "Fast BM : " << timeSec << " sec" << endl;

        showFlow("Fast BM", d_flowx, d_flowy);
wester committed
246 247 248 249 250 251 252 253
    }

    imshow("Frame 0", frame0);
    imshow("Frame 1", frame1);
    waitKey();

    return 0;
}