Repository navigation
Expand file tree
/
Copy pathauv_just_final.cpp
More file actions
349 lines (279 loc) · 9.58 KB
/
Copy pathauv_just_final.cpp
File metadata and controls
349 lines (279 loc) · 9.58 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
//---------- All Global Variables----------------------------------
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<vector<Point> > polygons;
Point2f cent;
Point2f angle_base;
double dist;
double rotate_angle;
//--------------------------------------------------------------------------
void gray_world(Mat src1,float *ml,float *ma, float *mb, int p,int m)
{
*ma=0;
*mb=0;
*ml=0;
for(int i=0;i<src1.rows;i++)
{
for(int j=0;j<src1.cols;j++)
{
Vec3b v1=src1.at<Vec3b>(i,j);
float lc=pow(v1.val[0],p);
float ac=pow(v1.val[1],p);
float bc=pow(v1.val[2],p);
*ma=*ma+ac;
*mb=*mb+bc;
*ml=*ml+lc;
}
}
*ma=pow((float)*ma/(src1.cols*src1.rows),(float)1/p);
*mb=pow((float)*mb/(src1.cols*src1.rows),(float)1/p);
*ml=pow((float)*ml/(src1.cols*src1.rows),(float)1/p);
float r=0;
if(m==0)
{
r=(*ma+*mb+*ml)/3;
*ma=r/(*ma);
*mb=r/(*mb);
*ml=r/(*ml);
}
if(m==1)
{
r=(*ma+*mb+*ml)/3;
r=max(*ma,*mb);
r=max(r,*ml);
*ma=r/(*ma);
*mb=r/(*mb);
*ml=r/(*ml);
}
if(m==2)
{
r=sqrt((*ma)*(*ma)+(*mb)*(*mb)+(*ml)*(*ml));
*ma=(float)(*ma)/(float)r;
*mb=(float)(*mb)/(float)r;
*ml=(float)(*ml)/(float)r;
cerr << *ml << endl;
cerr << *ma << endl;
cerr << *mb << endl;
r=max(*ma,*mb);
r=max(r,*ml);
*ma=(float)r/(float)(*ma);
*mb=(float)r/(float)(*mb);
*ml=(float)r/(float)(*ml);
}
}
Mat white_balance(Mat src,int p_in,int m_in)
{
vector<Mat> bgr_planes;
//Mat src = image;
Mat dst;
src.copyTo(dst);
Mat src1 = src;
//split(src,bgr_planes);
split(src,bgr_planes);
float ma=0,mb=0,ml=0;
int p=p_in;
int m=m_in;
gray_world(src,&ml,&ma,&mb,p,m);
float r=(ma+mb+ml)/3;
if(m==1)
{
r=(ma+mb+ml)/3;
r=max(ma,mb);
r=max(r,ml);
}
MatIterator_<Vec3b> it=src1.begin<Vec3b>();
MatIterator_<Vec3b> itend=src1.end<Vec3b>();
MatIterator_<Vec3b> itout=dst.begin<Vec3b>();
for (;it!=itend;++it,++itout)
{
Vec3b v1=*it;
float l=v1.val[0];
float a=v1.val[1];
float b=v1.val[2];
a=a*(r/ma);
b=b*(r/mb);
l=l*(r/ml);
if(a>255)
a=255;
if(b>255)
b=255;
if(l>255)
l=255;
v1.val[0]=l;
v1.val[1]=a;
v1.val[2]=b;
*itout=v1;
}
return dst;
}
Mat clahe_conversion(Mat input)
{
vector<Mat> RGB; // Use the STL’s vector structure to store multiple Mat objects
split(input, RGB); // split the image into separate color planes (R G B)
//// Enhance Local Contrast (CLAHE)
Ptr<CLAHE> clahe = createCLAHE();
clahe->setClipLimit(4);
Mat RGB_eq;
//// Equalizes the histogram of a one channel image (8UC1) using Contrast Limited Adaptive Histogram Equalization.
clahe->apply(RGB[0],RGB[0]);
clahe->apply(RGB[1],RGB[1]);
clahe->apply(RGB[2],RGB[2]);
merge(RGB,RGB_eq); // now merge the results back
return RGB_eq;
}
double abs_distance(Point2f p1, Point2f p2)
{
double d = (p1.x - p2.x)*(p1.x - p2.x) +(p1.y - p2.y)*(p1.y - p2.y);
double dist = pow(d, 0.5);
return dist;
}
double cc_angle(Point2i vtx, Point2i p1, Point2i p2)
{
// 1st point is the vertex , then 1st point and 2nd point,angle is obtained in radians
double modv1 = abs_distance(vtx, p1); // Mod v1
double modv2 = abs_distance(vtx, p2); // Mod v2
double dot = (p1.x - vtx.x)*(p2.x - vtx.x) + (p1.y - vtx.y)*(p2.y - vtx.y);
double costheta = dot / (modv1*modv2);
double sign = 0;
double term = (p1.x - vtx.x)*(p2.y - vtx.y) - (p1.y - vtx.y)*(p2.x - vtx.x);
if (term <= 0)
sign = 1.0;
else
sign = -1.0;
double theta = 0;
if (costheta >= 1.0)
theta = 0;
else if (costheta <= -1.0)
theta = acos(-1.0);
else
theta = acos(costheta);
return theta*sign;
}
int main(){
VideoCapture cap("/home/shaggy/line_auv/GOPR0033.MP4");
if(!cap.isOpened()){
cout<< "Cannot open the video file" << endl;
return -1;
}
namedWindow("Video",CV_WINDOW_NORMAL);
while(1){
Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess){
cout << "Cannot read the frame from video file" << endl;
break;
}
Mat image = frame;
Mat image_hsv;
cent.y=frame.rows/2;
cent.x=frame.cols/2;
angle_base.y=frame.rows/2;
angle_base.x=0;
image = clahe_conversion(image);
namedWindow("normal",CV_WINDOW_NORMAL);
imshow("normal", frame);
addWeighted(frame,0.7, image, 0.3, 0.0, image);
//image = white_balance(image, 1,1);
namedWindow("white_balance",CV_WINDOW_NORMAL);
imshow("white_balance", image);
cvtColor(image, image_hsv, COLOR_BGR2HSV);
vector<Mat>hsv;
split(image_hsv, hsv);
imwrite("/home/harsha/Pictures/clahee.JPG",image);
namedWindow("hsv",CV_WINDOW_NORMAL);
namedWindow("h",CV_WINDOW_NORMAL);
namedWindow("saturation",CV_WINDOW_NORMAL);
imshow("hsv",image_hsv);
imshow("h",hsv[0]);
imshow("saturation", hsv[1]);
Mat hsv_image = hsv[0];
Mat hsv_image1 = hsv[1];
#pragma omp parallel for collapse(2)
for(int y=0; y<hsv_image.cols; y++){
for(int x=0; x<hsv_image.rows; x++){
if(hsv_image.at<uchar>(x,y) >=0 && hsv_image.at<uchar>(x,y) <25 || hsv_image.at<uchar>(x,y) >160 && hsv_image.at<uchar>(x,y)<180){
hsv_image.at<uchar>(x,y) = 255;
}
else
hsv_image.at<uchar>(x,y) = 0;
}
}
#pragma omp parallel for collapse(2)
for(int y=0; y<hsv_image1.cols; y++){
for(int x=0; x<hsv_image1.rows; x++){
if(hsv_image1.at<uchar>(x,y) >=0 && hsv_image1.at<uchar>(x,y) <40 ){
hsv_image.at<uchar>(x,y) = 0;
}
else
//hsv_image.at<uchar>(x,y);
continue;
}
}
/* #pragma omp parallel for collapse(2)
for(int y=0; y<hsv_image.cols; y++){
for(int x=0; x<hsv_image.rows; x++){
if(hsv_image1.at<uchar>(x,y) == 0){
hsv_image.at<uchar>(x,y) = 0;
}
else
hsv_image.at<uchar>(x,y);
}
}*/
//cvtColor(hsv_image,hsv_image,CV_BGR2GRAY);
Mat elm = getStructuringElement(MORPH_RECT, Size(17, 17));
erode(hsv_image,hsv_image, elm);
erode(hsv_image,hsv_image, elm);
dilate(hsv_image,hsv_image, elm);
//erode(src_gray, src_gray, elm);
//dilate(src_gray, src_gray, elm);
findContours(hsv_image.clone(), contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
int aHighIndex=0;
double aHigh = 0;
//cout<<contours.size()<<endl;
for( int i = 0; i< contours.size(); i++ )
{
if(contourArea(contours[i]) > aHigh)
{
aHighIndex = i;
aHigh = contourArea(contours[i]);
}
}
cout<<aHigh<<endl;
Mat path = Mat::zeros(hsv_image.size(),CV_8UC3);
drawContours(path,contours,aHighIndex,Scalar::all(255),2, 8, hierarchy, 0, Point());
Moments mc_path;
Point2f path_centre;
circle(path,cent,40,Scalar(0,255,0),5,8,0);
if(contours.size()!=0)
{
mc_path=moments(contours[aHighIndex],true);
path_centre=Point2f(mc_path.m10/mc_path.m00,mc_path.m01/mc_path.m00);
circle(path,path_centre,40,Scalar(255,0,0),5,8,0);
dist=abs_distance(path_centre,cent);
line(path,path_centre,cent,Scalar(255,255,0),1,8,0);
rotate_angle= cc_angle(cent,path_centre,angle_base);
cout<<"angle"<<rotate_angle<<endl;
}
cout<<path_centre<<endl;
//drawing, contours, i, color, 2, 8, hierarchy, 0, Point()
namedWindow("contours",CV_WINDOW_NORMAL);
namedWindow("saturation1",CV_WINDOW_NORMAL);
//namedWindow("path",CV_WINDOW_NORMAL);
//imshow("saturation1",hsv_image1);
imshow("path",hsv_image);
imshow("contours",path);
//imshow("MyVideo", frame);
if(waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
}