-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwrap.cpp
More file actions
142 lines (119 loc) · 3.99 KB
/
unwrap.cpp
File metadata and controls
142 lines (119 loc) · 3.99 KB
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* This program takes a single image from the a camera pointed at a
* spherical mirror and unwraps it to produce a 360-degree panoramic view.
*
* This is to be used for tracking of people and navigation of a robot.
*
* Ben Selby, 2013
*/
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <string>
#define PI 3.141592654
// define the image parameters for cropping the mirror - should be a square
const int OFFSET_X = 96;
const int OFFSET_Y = 8;
const int WIDTH = 465;
const int HEIGHT = WIDTH;
const int RADIUS = WIDTH/2;
// specify input and output locations:
const std::string output_path = "output/";
const std::string input_path = "input_img/";
int get_time_diff( struct timeval *result, struct timeval *t1, struct timeval *t2 )
{
long int diff = (t2->tv_usec + 1000000*t2->tv_sec) - (t1->tv_usec + 1000000*t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;
return (diff>0);
}
int main( int argc, char** argv )
{
int CENTRE_X, CENTRE_Y;
if ( argc < 2 )
{
printf( "Usage: %s <image_filename> [centre_x centre_y]\n", argv[0] );
return -1;
}
if ( argc == 4 )
{
CENTRE_X = atoi( argv[2] ) - OFFSET_X;
CENTRE_Y = atoi( argv[3] ) - OFFSET_Y;
std::cout << "Using specified point [" << CENTRE_X <<", " <<
CENTRE_Y << "] as centre of image" << std::endl;
}
else
{
CENTRE_X = RADIUS;
CENTRE_Y = RADIUS;
}
cv::Mat src, cropped_img, unwrapped_img;
cv::Mat map_x, map_y;
struct timeval start_time, end_time, time_diff, calc_time;
gettimeofday(&start_time, NULL);
// Load the specified image
std::string path = argv[1];
src = cv::imread( path, 1 );
if ( !src.data )
{
printf( "Failed to load image, exiting.\n" );
return -1;
}
// Crop the input image to contain only the mirror
struct CvSize src_size;
src_size = src.size();
cv::Rect ROI( OFFSET_X, OFFSET_Y, WIDTH, HEIGHT );
cropped_img = src( ROI );
// create the unwrapped image and maps with same size as the cropped image
unwrapped_img.create( RADIUS, (int) 2*PI*RADIUS, cropped_img.type() );
map_x.create( unwrapped_img.size(), CV_32FC1 );
map_y.create( unwrapped_img.size(), CV_32FC1 );
// develop the map arrays for the unwarping from polar coordinates
// i = y coord, j = x coord
int rows = unwrapped_img.rows;
int cols = unwrapped_img.cols;
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
{
double theta = (double) j / RADIUS;// - (3*PI/2); // discretization in radians
map_x.at<float>(rows-i,j) = CENTRE_X + i*sin(theta);
map_y.at<float>(rows-i,j) = CENTRE_Y + i*cos(theta);
}
}
gettimeofday( &calc_time, NULL);
// let OpenCV handle the interpolation for the gaps in the unwrapped image
cv::remap( cropped_img, unwrapped_img,
map_x,
map_y,
CV_INTER_LINEAR,
cv::BORDER_CONSTANT,
cv::Scalar(0,0,0)
);
gettimeofday( &end_time, NULL );
get_time_diff( &time_diff, &start_time, &end_time );
printf( "Time Elapsed: %ld.%06ld seconds\n", time_diff.tv_sec, time_diff.tv_usec );
get_time_diff( &time_diff, &calc_time, &end_time );
printf( "Remap time: %ld.%06ld seconds\n", time_diff.tv_sec, time_diff.tv_usec );
cvNamedWindow( "Original", 1 );
imshow( "Original", src );
cvNamedWindow("Cropped", 1 );
imshow( "Cropped", cropped_img );
cvNamedWindow( "Unwrapped", 1 );
imshow( "Unwrapped", unwrapped_img );
int saved = cv::waitKey(0);
// if 's' key is pressed, save the unwrapped image
if ( saved == 1179731 || saved == 1048691 )
{
int last_index = path.find_last_of("/");
std::string img_name = path.substr(last_index+1, path.length()-1);
std::string prefix = "unwrapped_";
img_name = prefix + img_name;
std::string out_path = output_path + img_name;
std::cout<<"Saved unwrapped image to: "<<out_path<<std::endl;
imwrite(out_path, unwrapped_img);
}
return 0;
}