-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater_correction.cpp
175 lines (149 loc) · 5.69 KB
/
water_correction.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
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
/*Underwater Object Detection involving color correction algorithm
Author : Sagnik Basu*/
#include<opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
double mul = 1;
int iter = 10; //iterations required for color correction
Point center;
Point critp;
Point lp;
float s1 = 0; float s2 = 0;
int angle;
int scale = 1;
Mat rot_mat(2, 3, CV_32FC1);
int lowcount = 0;
int critpoint = 0;
int gl, rl, bl, bh=255, rh=255, gh=255;
Mat img_bi, poly; // stores the binary image
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<vector<Point> > polygons;
Mat correctGamma(Mat& img, double gamma) {
double inverse_gamma = gamma;
Mat lut_matrix(1, 256, CV_8UC1);
uchar * ptr = lut_matrix.ptr();
for (int i = 0; i < 256; i++)
ptr[i] = (int)(pow((double)i / 255.0, inverse_gamma) * 255.0);
Mat result;
LUT(img, lut_matrix, result);
return result;
}
//moments()
//VideoCapture front(0);
int main()
{
namedWindow("correction", CV_WINDOW_NORMAL);
namedWindow("Track", CV_WINDOW_NORMAL);
namedWindow("input", CV_WINDOW_NORMAL);
namedWindow("contour", CV_WINDOW_NORMAL);
//namedWindow("", CV_WINDOW_NORMAL);
createTrackbar("blue low", "Track", &bl, 255, NULL);
createTrackbar("blue high", "Track", &bh, 255, NULL);
createTrackbar("green low ", "Track", &gl, 255, NULL);
createTrackbar("green high", "Track", &gh, 255, NULL);
createTrackbar("red low", "Track", &rl, 255, NULL);
createTrackbar("red high", "Track", &rh, 255, NULL);
Mat img = imread("/home/shaggy/line_auv/path.jpg", CV_LOAD_IMAGE_COLOR);
imshow("input",img);
Mat g_corr=correctGamma(img,1);
Mat final = Mat::zeros(img.size(), CV_8UC3);
Mat imgg = g_corr.clone();
//cvtColor(img, img, COLOR_BGR2HSV);
// block for color correction
for (int i = 0; i < iter; i++)
{
for (int p = 0; p < img.rows - img.rows / (64 * mul); p += img.rows / (64 * mul))
{
for (int q = 0; q < img.cols - img.cols / (48 * mul); q += img.cols / (48 * mul))
{
int bavg = 0;
int gavg = 0;
int ravg = 0;
for (int i = p; i < p + img.rows / (64 * mul); i++)
{
for (int j = q; j < q + img.cols / (48 * mul); j++)
{
Vec3b color = g_corr.at<Vec3b>(i, j);
int b = color[0];
int g = color[1];
int r = color[2];
bavg += b;
gavg += g;
ravg += r;
}
}
bavg = bavg * 64 * 48 * mul*mul / (img.rows*img.cols);
gavg = gavg * 64 * 48 * mul*mul / (img.rows*img.cols);
ravg = ravg * 64 * 48 * mul*mul / (img.rows*img.cols);
int gg = 2 * gavg - (ravg + bavg);
int rr = 2 * ravg - (gavg + bavg);
for (int i = p; i < p + img.rows / (64 * mul); i++)
{
for (int j = q; j < q + img.cols / (48 * mul); j++)
{
Vec3b color = g_corr.at<Vec3b>(i, j);
/*if (2 * color[1] >= color[2] + color[0] + iter&&gavg >= 45)//&&2*color[1]>color[2]+color[0])
{
color[1] = 165;
color[0] = 0;
color[2] = 255;
}*/
if (2 * color[2] >= color[1] + color[0] + iter + 15 && ravg >= 45)//&&2*color[2]>color[1]+color[0])
{
color[1] = 255;
color[0] =255;
color[2] = 255;
}
else
{
color[1] =0;
color[0] =0;
color[2] =0;
}
imgg.at<Vec3b>(i, j) = color;
}
}
}
}
}
//cvtColor(img, img_gray, CV_BGR2GRAY);
// while (1)
// {
// poly = Mat::zeros(img.size(), CV_8UC3);
//inRange(img, Scalar(bl, 0, rl), Scalar(bh, 0, rh), img_bi);
// bitwise_not(img_bi, img_bi);
// findContours(img_bi.clone(), contours,hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// polygons.resize(contours.size());
// for (int i = 0; i < contours.size(); i++)
// {
// approxPolyDP(Mat(contours[i]), polygons[i], arcLength(Mat(contours[i]), true)*0.019, true);
// }
// for (int i = 0; i < polygons.size(); i++)
// {
// Scalar color = Scalar(255, 255, 255);
// drawContours(poly, polygons, i, color, 1, 8, hierarchy, 0, Point());
// for (int j = 0; j < polygons[i].size();j++)
// circle(poly, polygons[i][j], 4, Scalar(255, 255, 0), 1, 8, 0);
//circle(poly, polygons[0][3], 5, Scalar(255, 0, 0), 1, 8, 0);
// }
// imshow("contour", poly);
// imshow("color correction", img);
// imshow("gamma correction", g_corr);
//if (waitKey(32) == 27)
// break;
//}
//imshow("color correction", imgg);
//medianBlur(imgg,imgg,5);
for(int i=0;i<5;i++)
{
erode(imgg,imgg,Mat());
}
// erode(imgg,imgg,Mat());
//erode(imgg,imgg,Mat());
imshow("correction", imgg);
waitKey(0);
}