-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllipseFit.cu
205 lines (197 loc) · 6.88 KB
/
EllipseFit.cu
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* EllipseFit.cu
*
* Created on: 2018-11-27
* Author: [email protected]
*/
#include <opencv2/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/core/mat.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include "Timer.cpp"
#include <cuda.h>
#include <cuda_runtime.h>
#include <math_constants.h>
#include "cublas_v2.h"
#include <cusolverDn.h>
using namespace std;
using namespace cv;
//--------------------------------------------------------------------------------------------
__global__
void sumColumns(float *matrix, float *result, int width, int height) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
int i = index / width;
int j = index % width;
if (i < height && j < width) {
atomicAdd(&result[j], matrix[i * width + j]);
}
}
static cv::RotatedRect GPUfitEllipse(std::vector <Point2f> points)
{
cv::RotatedRect box;
box.center.x = -1;
box.center.y = -1;
box.size.width = -1;
box.size.height = -1;
box.angle = -1;
if (points.size() >= 5)
{
float* A, *h_x;
cudaMallocHost((void**)&A, 5*points.size()*sizeof(float));
cudaMallocHost((void**)&h_x, 5*sizeof(float));
float mean_x = 0;
float mean_y = 0;
//Calculate mean of points
for (int i = 0; i < points.size(); i++)
{
mean_x += points[i].x;
mean_y += points[i].y;
}
mean_x /= points.size();
mean_y /= points.size();
//Substract mean to points and create points matrix
for (int i = 0; i < points.size(); i++)
{
points[i].x -= mean_x;
points[i].y -= mean_y;
A[5*i] = (float)points[i].x*(float)points[i].x;
A[5*i+1] = (float)points[i].x*(float)points[i].y;
A[5*i+2] = (float)points[i].y*(float)points[i].y;
A[5*i+3] = (float)points[i].x;
A[5*i+4] = (float)points[i].y;
}
//Initialize variables
float *d_A, *d_b, *d_x, alpha = 1.0f, beta = 0.0f, *gdwork;
float AA, B, C, D, E, phi, cos_phi, sin_phi, cos_phi2, sin_phi2, sin_phi_cos_phi, A1, C1, D1, E1, F2, mean_xx;
cublasHandle_t cublas_handle;
cublasCreate(&cublas_handle);
cusolverDnHandle_t solver_handle;
cusolverDnCreate(&solver_handle);
size_t work_bytes;
int *d_info, nil = 0;
cudaMalloc((void**)&d_info, sizeof(int));
cudaMalloc((void**)&d_A, 5*points.size()*sizeof(float));
cudaMalloc((void**)&d_b, 5*sizeof(float));
cudaMalloc((void**)&d_x, 5*sizeof(float));
//Copy points matrix to device
cudaMemcpy(d_A, A, 5*points.size()*sizeof(float), cudaMemcpyHostToDevice);
//Not count in time as it could be the same for all iterations
cusolverDnSSgels_bufferSize(solver_handle, 5, 5, 1, d_A, 5, d_b, 5, d_x, 5, NULL, &work_bytes);
cudaMalloc(&gdwork, work_bytes * sizeof(float));
//Start timer
Timer t;
t.start();
//Calculate sum of columns
sumColumns<<<points.size()*5/64+1, 64>>>(d_A, d_b, 5, points.size());
//Calculate covariance matrix (A^t*A)
cublasSgemm(cublas_handle, CUBLAS_OP_N, CUBLAS_OP_T, 5, 5, points.size(), &alpha, d_A, 5, d_A, 5, &beta, d_A, 5);
//Solve system Cov * x = Sum(X)
cusolverDnSSgels(solver_handle, 5, 5, 1, d_A, 5, d_b, 5, d_x, 5, gdwork, work_bytes, &nil, d_info);
//Pass result to host
cudaMemcpy(h_x, d_x, 5*sizeof(float), cudaMemcpyDeviceToHost);
AA = h_x[0];
B = h_x[1];
C = h_x[2];
D = h_x[3];
E = h_x[4];
phi = 0.5*atan2(B, C-AA);
cos_phi = cos(phi);
sin_phi = sin(phi);
cos_phi2 = cos_phi*cos_phi;
sin_phi2 = sin_phi*sin_phi;
sin_phi_cos_phi = sin_phi*cos_phi;
A1 = AA*cos_phi2 - B*sin_phi_cos_phi + C*sin_phi2;
C1 = AA*sin_phi2 + B*sin_phi_cos_phi + C*cos_phi2;
D1 = D*cos_phi - E*sin_phi;
E1 = D*sin_phi + E*cos_phi;
if (A1 < 0) {
A1 = -A1;
C1 = -C1;
D1 = -D1;
E1 = -E1;
}
F2 = 1 + (D1 * D1)/(4*A1) + (E1 * E1)/(4*C1);
mean_xx = cos_phi*mean_x - sin_phi*mean_y;
mean_y = sin_phi*mean_x + cos_phi*mean_y;
mean_x = mean_xx;
if (A1*C1 <= 0)
{
cout << "No ellipse found" << endl;
return box;
}
auto time = t.elapsed();
cout << "GPU Ellipse: " << time/10e6 << endl;
box.center.x = (mean_x - D1/2/A1)*cos_phi + (mean_y - E1/2/C1)*sin_phi;
box.center.y = (mean_y - E1/2/C1)*cos_phi - (mean_x - D1/2/A1)*sin_phi;
box.size.width = 2*sqrt(abs(F2/A1));
box.size.height = 2*sqrt(abs(F2/C1));
box.angle = phi*180/M_PI;
cudaFree(d_A);
cudaFree(d_b);
cudaFree(d_x);
cudaFree(d_info);
cudaFree(gdwork);
cusolverDnDestroy(solver_handle);
cublasDestroy(cublas_handle);
cudaFreeHost(h_x);
cudaFreeHost(A);
}
return box;
}
int main(int argc, char *argv[]) {
//create vector with lots of points
vector <Point2f> points;
points.push_back(Point2f(15, 101));
points.push_back(Point2f(40, 157));
points.push_back(Point2f(115, 140));
points.push_back(Point2f(37, 55));
points.push_back(Point2f(80, 35));
for (int i = 0; i < 500; i++) {
points.push_back(Point2f(15, 101));
points.push_back(Point2f(40, 157));
points.push_back(Point2f(115, 140));
points.push_back(Point2f(37, 55));
points.push_back(Point2f(80, 35));
}
// loop to test speed
for (int i = 0; i < 30; i++) {
//GPU fit ellipse
auto b = GPUfitEllipse(points);
cout << b.center.y << endl;
cout << b.center.x << endl;
cout << b.size.height << endl;
cout << b.size.width << endl;
cout << b.angle << endl;
Mat img = Mat::zeros(200, 200, CV_8UC1);
cvtColor(img, img, COLOR_GRAY2BGR);
for (auto p: points) {
circle(img, p, 1, Scalar(0, 0, 255), -1);
}
ellipse(img, b, Scalar(0, 255, 0), 2);
imshow("img", img);
cout << "end" << endl;
//CV fit ellipse
Timer t;
t.start();
auto bb = cv::fitEllipse(points);
auto time = t.elapsed();
cout << "CV Ellipse: " << time / 10e6 << endl;
cout << bb.center.y << endl;
cout << bb.center.x << endl;
cout << bb.size.height << endl;
cout << bb.size.width << endl;
cout << bb.angle << endl;
Mat img2 = Mat::zeros(200, 200, CV_8UC1);
cvtColor(img2, img2, COLOR_GRAY2BGR);
for (auto p: points) {
circle(img2, p, 1, Scalar(0, 0, 255), -1);
}
ellipse(img2, b, Scalar(0, 255, 0), 2);
imshow("img2", img2);
cout << "end" << endl;
}
waitKey(0);
exit(0);
}