-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflo2png.cpp
86 lines (74 loc) · 2.3 KB
/
flo2png.cpp
1
2
3
4
5
6
7
8
9
10
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
#include "opencv2/highgui.hpp"
#include "opencv2/video.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/optflow.hpp"
#include <fstream>
#include <limits>
using namespace std;
using namespace cv;
using namespace optflow;
const String keys = "{help h usage ? | | print this message }"
"{@image1 | | image1.flo}"
"{@image2 | | image2.png}";
static Mat flowToDisplay(const Mat flow)
{
Mat flow_split[2];
Mat magnitude, angle;
Mat hsv_split[3], hsv, rgb;
split(flow, flow_split);
cartToPolar(flow_split[0], flow_split[1], magnitude, angle, true);
normalize(magnitude, magnitude, 0, 255, NORM_MINMAX);
hsv_split[0] = angle; // already in degrees - no normalization needed
hsv_split[1] = Mat::ones(angle.size(), angle.type());
hsv_split[2] = magnitude;
merge(hsv_split, 3, hsv);
cvtColor(hsv, rgb, COLOR_HSV2BGR);
return rgb;
}
static Mat flowTo3Channels(const Mat flow)
{
Mat flow_split[2],rgb;
Mat blue_chan = Mat::zeros(flow.size(), CV_32FC1);
split(flow, flow_split);
vector<Mat> channels;
channels.push_back(flow_split[0]);
channels.push_back(flow_split[1]);
channels.push_back(blue_chan);
merge(channels,rgb);
return rgb;
}
int main( int argc, char** argv )
{
CommandLineParser parser(argc, argv, keys);
parser.about("convert .flo file to .png in colorcode");
if ( parser.has("help") || argc < 2 )
{
parser.printMessage();
printf("EXAMPLES:\n");
printf("./flo2exr in.flo out.png\n");
return 0;
}
String inputfile = parser.get<String>(0);
String outputfile = parser.get<String>(1);
if ( !parser.check() )
{
parser.printErrors();
return 0;
}
Mat_<Point2f> flow;
flow = optflow::readOpticalFlow(inputfile);
if ( !flow.data )
{
printf("No flow data \n");
return -1;
}
//Mat flow_image = flowToDisplay(flow);
//namedWindow( "Computed flow", WINDOW_AUTOSIZE );
//imshow( "Computed flow", flow_image );
//waitKey(0);
//write result as .png
Mat colorflow = flowToDisplay(flow);
//cv::cvtColor(flow_3chan, flow_3chan, CV_BGR2RGB);
cv::imwrite(outputfile, colorflow);
printf("writing png file : %s\n",outputfile.c_str());
}