|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | + |
| 4 | +#include <opencv2/core/utility.hpp> |
| 5 | +#include "opencv2/video.hpp" |
| 6 | +#include "opencv2/optflow.hpp" |
| 7 | +#include "opencv2/imgcodecs.hpp" |
| 8 | +#include "opencv2/highgui.hpp" |
| 9 | + |
| 10 | +using namespace cv; |
| 11 | +using namespace std; |
| 12 | +using namespace optflow; |
| 13 | + |
| 14 | +inline bool isFlowCorrect(Point2f u) |
| 15 | +{ |
| 16 | + return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9; |
| 17 | +} |
| 18 | + |
| 19 | +static Vec3b computeColor(float fx, float fy) |
| 20 | +{ |
| 21 | + static bool first = true; |
| 22 | + |
| 23 | + // relative lengths of color transitions: |
| 24 | + // these are chosen based on perceptual similarity |
| 25 | + // (e.g. one can distinguish more shades between red and yellow |
| 26 | + // than between yellow and green) |
| 27 | + const int RY = 15; |
| 28 | + const int YG = 6; |
| 29 | + const int GC = 4; |
| 30 | + const int CB = 11; |
| 31 | + const int BM = 13; |
| 32 | + const int MR = 6; |
| 33 | + const int NCOLS = RY + YG + GC + CB + BM + MR; |
| 34 | + static Vec3i colorWheel[NCOLS]; |
| 35 | + |
| 36 | + if (first) |
| 37 | + { |
| 38 | + int k = 0; |
| 39 | + |
| 40 | + for (int i = 0; i < RY; ++i, ++k) |
| 41 | + colorWheel[k] = Vec3i(255, 255 * i / RY, 0); |
| 42 | + |
| 43 | + for (int i = 0; i < YG; ++i, ++k) |
| 44 | + colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0); |
| 45 | + |
| 46 | + for (int i = 0; i < GC; ++i, ++k) |
| 47 | + colorWheel[k] = Vec3i(0, 255, 255 * i / GC); |
| 48 | + |
| 49 | + for (int i = 0; i < CB; ++i, ++k) |
| 50 | + colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255); |
| 51 | + |
| 52 | + for (int i = 0; i < BM; ++i, ++k) |
| 53 | + colorWheel[k] = Vec3i(255 * i / BM, 0, 255); |
| 54 | + |
| 55 | + for (int i = 0; i < MR; ++i, ++k) |
| 56 | + colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR); |
| 57 | + |
| 58 | + first = false; |
| 59 | + } |
| 60 | + |
| 61 | + const float rad = sqrt(fx * fx + fy * fy); |
| 62 | + const float a = atan2(-fy, -fx) / (float)CV_PI; |
| 63 | + |
| 64 | + const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1); |
| 65 | + const int k0 = static_cast<int>(fk); |
| 66 | + const int k1 = (k0 + 1) % NCOLS; |
| 67 | + const float f = fk - k0; |
| 68 | + |
| 69 | + Vec3b pix; |
| 70 | + |
| 71 | + for (int b = 0; b < 3; b++) |
| 72 | + { |
| 73 | + const float col0 = colorWheel[k0][b] / 255.f; |
| 74 | + const float col1 = colorWheel[k1][b] / 255.f; |
| 75 | + |
| 76 | + float col = (1 - f) * col0 + f * col1; |
| 77 | + |
| 78 | + if (rad <= 1) |
| 79 | + col = 1 - rad * (1 - col); // increase saturation with radius |
| 80 | + else |
| 81 | + col *= .75; // out of range |
| 82 | + |
| 83 | + pix[2 - b] = static_cast<uchar>(255.f * col); |
| 84 | + } |
| 85 | + |
| 86 | + return pix; |
| 87 | +} |
| 88 | + |
| 89 | +static void drawOpticalFlow(const Mat_<Point2f>& flow, Mat& dst, float maxmotion = -1) |
| 90 | +{ |
| 91 | + dst.create(flow.size(), CV_8UC3); |
| 92 | + dst.setTo(Scalar::all(0)); |
| 93 | + |
| 94 | + // determine motion range: |
| 95 | + float maxrad = maxmotion; |
| 96 | + |
| 97 | + if (maxmotion <= 0) |
| 98 | + { |
| 99 | + maxrad = 1; |
| 100 | + for (int y = 0; y < flow.rows; ++y) |
| 101 | + { |
| 102 | + for (int x = 0; x < flow.cols; ++x) |
| 103 | + { |
| 104 | + Point2f u = flow(y, x); |
| 105 | + |
| 106 | + if (!isFlowCorrect(u)) |
| 107 | + continue; |
| 108 | + |
| 109 | + maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y)); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + for (int y = 0; y < flow.rows; ++y) |
| 115 | + { |
| 116 | + for (int x = 0; x < flow.cols; ++x) |
| 117 | + { |
| 118 | + Point2f u = flow(y, x); |
| 119 | + |
| 120 | + if (isFlowCorrect(u)) |
| 121 | + dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// binary file format for flow data specified here: |
| 127 | +// http://vision.middlebury.edu/flow/data/ |
| 128 | +static void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName) |
| 129 | +{ |
| 130 | + static const char FLO_TAG_STRING[] = "PIEH"; |
| 131 | + |
| 132 | + ofstream file(fileName.c_str(), ios_base::binary); |
| 133 | + |
| 134 | + file << FLO_TAG_STRING; |
| 135 | + |
| 136 | + file.write((const char*) &flow.cols, sizeof(int)); |
| 137 | + file.write((const char*) &flow.rows, sizeof(int)); |
| 138 | + |
| 139 | + for (int i = 0; i < flow.rows; ++i) |
| 140 | + { |
| 141 | + for (int j = 0; j < flow.cols; ++j) |
| 142 | + { |
| 143 | + const Point2f u = flow(i, j); |
| 144 | + |
| 145 | + file.write((const char*) &u.x, sizeof(float)); |
| 146 | + file.write((const char*) &u.y, sizeof(float)); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +int main(int argc, const char* argv[]) |
| 152 | +{ |
| 153 | + cv::CommandLineParser parser(argc, argv, "{help h || show help message}" |
| 154 | + "{ @frame0 | | frame 0}{ @frame1 | | frame 1}{ @output | | output flow}"); |
| 155 | + if (parser.has("help")) |
| 156 | + { |
| 157 | + parser.printMessage(); |
| 158 | + return 0; |
| 159 | + } |
| 160 | + string frame0_name = parser.get<string>("@frame0"); |
| 161 | + string frame1_name = parser.get<string>("@frame1"); |
| 162 | + string file = parser.get<string>("@output"); |
| 163 | + if (frame0_name.empty() || frame1_name.empty() || file.empty()) |
| 164 | + { |
| 165 | + cerr << "Usage : " << argv[0] << " [<frame0>] [<frame1>] [<output_flow>]" << endl; |
| 166 | + return -1; |
| 167 | + } |
| 168 | + |
| 169 | + Mat frame0 = imread(frame0_name, IMREAD_GRAYSCALE); |
| 170 | + Mat frame1 = imread(frame1_name, IMREAD_GRAYSCALE); |
| 171 | + |
| 172 | + if (frame0.empty()) |
| 173 | + { |
| 174 | + cerr << "Can't open image [" << parser.get<string>("frame0") << "]" << endl; |
| 175 | + return -1; |
| 176 | + } |
| 177 | + if (frame1.empty()) |
| 178 | + { |
| 179 | + cerr << "Can't open image [" << parser.get<string>("frame1") << "]" << endl; |
| 180 | + return -1; |
| 181 | + } |
| 182 | + |
| 183 | + if (frame1.size() != frame0.size()) |
| 184 | + { |
| 185 | + cerr << "Images should be of equal sizes" << endl; |
| 186 | + return -1; |
| 187 | + } |
| 188 | + |
| 189 | + Mat_<Point2f> flow; |
| 190 | + Ptr<DualTVL1OpticalFlow> tvl1 = DualTVL1OpticalFlow::create(); |
| 191 | + |
| 192 | + const double start = (double)getTickCount(); |
| 193 | + tvl1->calc(frame0, frame1, flow); |
| 194 | + const double timeSec = (getTickCount() - start) / getTickFrequency(); |
| 195 | + cout << "calcOpticalFlowDual_TVL1 : " << timeSec << " sec" << endl; |
| 196 | + |
| 197 | + Mat out; |
| 198 | + drawOpticalFlow(flow, out); |
| 199 | + if (!file.empty()) |
| 200 | + writeOpticalFlowToFile(flow, file); |
| 201 | + |
| 202 | + imshow("Flow", out); |
| 203 | + waitKey(); |
| 204 | + |
| 205 | + return 0; |
| 206 | +} |
0 commit comments