-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinaledit12.cpp
234 lines (185 loc) · 7.85 KB
/
finaledit12.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
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
/*In this code intially background is taken in and made into 4 slices and
on each slice otsu thresholding is applied for grayscale and
hue channel and after that each frame of video is taken in and above method of slices is followed again.
Then we apply background subtraction and remove noise in image.
Finally we obtain only the mid points on laser line which are on foreground.Also here camera parameters are varied
using trackbars creaeted to get optimum result*/
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
using namespace std;
float Brightness;
float Contrast ;
float Saturation;
float Gain;
int B;
int C;
int S;
int G;
VideoCapture cap(0);
void cameratest(int,void*) //function that when called resets camera parameters with given values
{
Brightness =float(B)/100;
Contrast =float(C)/100;
Saturation =float(S)/100;
Gain =float(G)/100;
cap.set(CV_CAP_PROP_BRIGHTNESS,Brightness);
cap.set(CV_CAP_PROP_CONTRAST, Contrast);
cap.set(CV_CAP_PROP_SATURATION, Saturation);
cap.set(CV_CAP_PROP_GAIN, Gain);
}
int main(int argc,const char** argv)
{
int count=0;
if(!cap.isOpened()) // check if we succeeded
return -1;
Brightness = cap.get(CV_CAP_PROP_BRIGHTNESS); //recording camera parameters
Contrast = cap.get(CV_CAP_PROP_CONTRAST );
Saturation = cap.get(CV_CAP_PROP_SATURATION);
Gain = cap.get(CV_CAP_PROP_GAIN);
B=int(Brightness*100);
C=int(Contrast*100);
S=int(Saturation*100);
G=int(Gain*100);
Mat back,back2,backhsv;
cap >> back; //getting background
Mat backfinal1(back.rows,back.cols,CV_8UC1),backfinal2(back.rows,back.cols,CV_8UC1);
cvtColor(back,back2,COLOR_BGR2GRAY);
cvtColor(back,backhsv,COLOR_BGR2HSV);
while(count!=back.rows) //slicing background and applying otsu for both hue channel and gray image
{
Mat backslice=back2(Range(count,count+back.rows/4),Range(0,back.cols));
vector<Mat> channels(3);
split(backhsv(Range(count,count+back.rows/4),Range(0,back.cols)),channels);
Mat backhue=channels[0];
GaussianBlur(backslice,backslice,Size(5,5),0,0);
GaussianBlur(backhue,backhue,Size(5,5),0,0);
threshold(backhue,backhue, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
threshold(backslice,backslice, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
backfinal1(Range(count,count+back.rows/4),Range(0,back.cols)) = backslice;
backfinal2(Range(count,count+back.rows/4),Range(0,back.cols)) = backhue;
count=count+back.rows/4;
}
namedWindow("foreground1", CV_WINDOW_AUTOSIZE);
namedWindow("midgray", CV_WINDOW_AUTOSIZE);
namedWindow("midhsv", CV_WINDOW_AUTOSIZE);
namedWindow("foreground2", CV_WINDOW_AUTOSIZE);
ofstream afile;
string filename="pen1.txt";
afile.open(filename, ios::app);
while(1)
{
Mat hsv,gray,frame,foreground1,foreground2;
Mat img1(frame.rows,frame.cols,CV_8UC1),img2(frame.rows,frame.cols,CV_8UC1);
cap >> frame; // get a new frame from camera
cvtColor(frame,gray,COLOR_BGR2GRAY);
img1=gray;
img2=gray;
cvtColor(frame,hsv,COLOR_BGR2HSV);
int i=0,j;
while(i!=frame.rows)
{
//afile<<"reached";
Mat img3=gray(Range(i,i+frame.rows/4),Range(0,frame.cols));
Mat img4;
vector<Mat> channels(3);
split(hsv(Range(i,i+frame.rows/4),Range(0,frame.cols)),channels);
img4=channels[0];
GaussianBlur(img3,img3,Size(5,5),0,0);
GaussianBlur(img4,img4,Size(5,5),0,0);
threshold(img3,img3, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
threshold(img4,img4, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
img1(Range(i,i+frame.rows/4),Range(0,frame.cols)) = img3;
img2(Range(i,i+frame.rows/4),Range(0,frame.cols))=img4;
i=i+frame.rows/4;
}
foreground1=img1-backfinal1; //Background subtraction
foreground2=img2-backfinal2;
int c,a,b,f=0;
if(waitKey(30) == 97)
{
f++;
}
Mat mid1(frame.rows,frame.cols,CV_8UC1,Scalar(0));
for(i=0;i<foreground1.rows;i++) // This function is to remove noice from the background subtracted image for gray image
{
a=0;
b=0;
c=0;
for(j=0;j<foreground1.cols;j++)
{
if(foreground1.at<uchar>(i,j)!=foreground1.at<uchar>(i,j+1))
{
if(((foreground1.at<uchar>(i,j+1)+foreground1.at<uchar>(i,j+2)+foreground1.at<uchar>(i,j+3))<255*3) && ((foreground1.at<uchar>(i,j-1)+foreground1.at<uchar>(i,j-2)+foreground1.at<uchar>(i,j-3))<255*3))
continue;
if(c==0)
{
a=j+1;
c=c+1;
continue;
}
if(c==1)
{
b=j;
mid1.at<uchar>(i,(a+b)/2)=255;
if(f==1)
{
double l,d=50,x,y,z,angle=1.11;
l=(mid1.cols-(a+b))/2;
z=(20586*d-40*l)/(25*l+20586*(1.0/tan(angle))); // angle not assigned
x=(l*(25*z-40))/(20586);
y=((mid1.rows/2-j)*(25*z-40)/20586);
afile << x << " " << y << " " << z << " " << "\n";
}
break;
}
}
}
}
Mat mid2(frame.rows,frame.cols,CV_8UC1,Scalar(0)); // This function is to remove noice from the background subtracted image for gray image
for(i=0;i<foreground2.rows;i++)
{
a=0;
b=0;
c=0;
for(j=0;j<foreground2.cols;j++)
{
if(foreground2.at<uchar>(i,j)!=foreground2.at<uchar>(i,j+1))
{
if(((foreground2.at<uchar>(i,j+1)+foreground2.at<uchar>(i,j+2)+foreground2.at<uchar>(i,j+3))<255*3) && ((foreground2.at<uchar>(i,j-1)+foreground2.at<uchar>(i,j-2)+foreground2.at<uchar>(i,j-3))<255*3))
continue;
if(c==0)
{
a=j+1;
c=c+1;
continue;
}
if(c==1)
{
b=j;
mid2.at<uchar>(i,(a+b)/2)=255;
break;
}
}
}
}
imshow("foreground1", foreground1);
imshow("foreground2", foreground2);
imshow("midgray", mid1);
imshow("midhsv", mid2);
createTrackbar( "Brightness","foreground1", &B, 100, cameratest); //setting trackbar to foreground1 window
createTrackbar( "Contrast","foreground1", &C, 100,cameratest);
createTrackbar( "Saturation","foreground1", &S, 100,cameratest);
createTrackbar( "Gain","foreground1", &G, 100,cameratest);
if(waitKey(30) == 27)
break;
}
afile.close();
destroyWindow("foreground1");
destroyWindow("foreground2");
destroyWindow("midgray");
destroyWindow("midhsv");
return 0;
}