-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_bagsub.cpp
263 lines (216 loc) · 6.45 KB
/
my_bagsub.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
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
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define END 30
using namespace std;
using namespace cv;
void thinningGuoHallIteration(cv::Mat& im, int iter)
{
cv::Mat marker = cv::Mat::zeros(im.size(), CV_8UC1);
for (int i = 1; i < im.rows; i++)
{
for (int j = 1; j < im.cols; j++)
{
uchar p2 = im.at<uchar>(i-1, j);
uchar p3 = im.at<uchar>(i-1, j+1);
uchar p4 = im.at<uchar>(i, j+1);
uchar p5 = im.at<uchar>(i+1, j+1);
uchar p6 = im.at<uchar>(i+1, j);
uchar p7 = im.at<uchar>(i+1, j-1);
uchar p8 = im.at<uchar>(i, j-1);
uchar p9 = im.at<uchar>(i-1, j-1);
int C = (!p2 & (p3 | p4)) + (!p4 & (p5 | p6)) +
(!p6 & (p7 | p8)) + (!p8 & (p9 | p2));
int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
int N = N1 < N2 ? N1 : N2;
int m = iter == 0 ? ((p6 | p7 | !p9) & p8) : ((p2 | p3 | !p5) & p4);
if (C == 1 && (N >= 2 && N <= 3) & m == 0)
marker.at<uchar>(i,j) = 1;
}
}
im &= ~marker;
}
/**
* Function for thinning the given binary image
*
* @param im Binary image with range = 0-255
*/
void thinningGuoHall(cv::Mat& im)
{
im /= 255;
cv::Mat prev = cv::Mat::zeros(im.size(), CV_8UC1);
cv::Mat diff;
do {
thinningGuoHallIteration(im, 0);
thinningGuoHallIteration(im, 1);
cv::absdiff(im, prev, diff);
im.copyTo(prev);
}
while (cv::countNonZero(diff) > 0);
im *= 255;
}
int absolute_val(int a)
{
if(a>0)
{
return a;
}
else
{
return -1*a;
}
}
int main(int argc,char* argv[])
{
VideoCapture cap("vd22.mp4"); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
//namedWindow("MyVideo",CV_WINDOW_NORMAL); //create a window called "MyVideo"
//namedWindow("MyVideo2",CV_WINDOW_NORMAL);
//namedWindow("Original",CV_WINDOW_NORMAL);
Mat image,image1;
cap.read(image);
resize(image,image,Size(image.cols/3,image.rows/3));
cvtColor(image,image,CV_BGR2GRAY);
//for(int i=0;i<image.rows;i++)
//{
//for(int j=0;j<image.cols;j++)
//{
//image.at<uchar>(i,j)-=30;
//if(image.at<uchar>(i,j)<0)//checking if pixel value is negative
//{
//image.at<uchar>(i,j)=0;
//}
//}
//}
//equalizeHist(image,image1);
cout<<image.channels()<<endl;
//cout<<image<<endl;
//imshow("image",image);
/*if (image.channels() == 1) { // gray-level image
image.at<uchar>(j,i)= 255;
} else if (image.channels() == 3) { // color image
image.at<cv::Vec3b>(j,i)[0]= 255;
image.at<cv::Vec3b>(j,i)[1]= 255;
image.at<cv::Vec3b>(j,i)[2]= 255;
}*/
//cout<<image.at<cv::Vec3b>(0,0)[0]<<endl;
while (1)
{
int K=0;
//scanf("%d",&K);
Mat frame,diff,frame2;
int di1,di2,di3;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
resize(frame,frame,Size(frame.cols/3,frame.rows/3));
cvtColor(frame,frame,CV_BGR2GRAY);
equalizeHist(frame,frame2);
imshow("Original",frame);
for(int i=0;i<frame.rows;i++)
{
for(int j=0;j<frame.cols;j++)
{
/* if (frame.channels() == 3) { // color image
di1=image.at<cv::Vec3b>(i,j)[0]-frame.at<cv::Vec3b>(i,j)[0];
di2=image.at<cv::Vec3b>(i,j)[1]-frame.at<cv::Vec3b>(i,j)[1];
di3=image.at<cv::Vec3b>(i,j)[2]-frame.at<cv::Vec3b>(i,j)[2];
di1=absolute_val(di1);
di2=absolute_val(di2);
di3=absolute_val(di3);
if(di1>END || di2>END || di3>END)
{
frame.at<cv::Vec3b>(i,j)[0]= 0;
frame.at<cv::Vec3b>(i,j)[1]= 0;
frame.at<cv::Vec3b>(i,j)[2]= 0;
}
else
{
frame.at<cv::Vec3b>(i,j)[0]= 255;
frame.at<cv::Vec3b>(i,j)[1]= 255;
frame.at<cv::Vec3b>(i,j)[2]= 255;
}
}*/
//frame.at<uchar>(i,j)-=10;
//if(frame.at<uchar>(i,j)<0)//checking if pixel value is negative
//{
//frame.at<uchar>(i,j)=0;
//}
di1=image.at<uchar>(i,j)-frame.at<uchar>(i,j);
di1=absolute_val(di1);
if(di1>END)
{
frame.at<uchar>(i,j)=255;
}
else
{
frame.at<uchar>(i,j)=0;
}
/*di2=image1.at<uchar>(i,j)-frame2.at<uchar>(i,j);
di2=absolute_val(di2);
if(di2>END)
{
frame2.at<uchar>(i,j)=255;
}
else
{
frame2.at<uchar>(i,j)=0;
}*/
}}
//cout<<image-frame<<endl;
/*else if (image.channels() == 3) { // color image
image.at<cv::Vec3b>(j,i)[0]= 255;
image.at<cv::Vec3b>(j,i)[1]= 255;
image.at<cv::Vec3b>(j,i)[2]= 255;
}*/
/* Mat skel(frame.size(), CV_8UC1, cv::Scalar(0));
Mat temp;
Mat eroded;
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
bool done;
do
{
cv::erode(frame, eroded, element);
cv::dilate(eroded, temp, element); // temp = open(img)
cv::subtract(frame, temp, temp);
cv::bitwise_or(skel, temp, skel);
eroded.copyTo(frame);
done = (cv::countNonZero(frame) == 0);
} while (!done);
*/
Mat dis;
//morphological opening (removes small objects from the foreground)
erode(frame, dis, getStructuringElement(MORPH_ELLIPSE, Size(4,4)) );
dilate( frame, dis, getStructuringElement(MORPH_ELLIPSE, Size(7, 7)) );
imshow("MyVideo2", frame);
thinningGuoHall(frame);
//morphological closing (removes small holes from the foreground)
//dilate( frame, dis, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
// erode(frame, dis, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
imshow("MyVideo", frame); //show the frame in "MyVideo" window
//imshow("MyVideo2",dis);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}