-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreaded.cpp
166 lines (123 loc) · 4.39 KB
/
threaded.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
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include "opencv2/videoio.hpp"
#include <thread>
#include <chrono>
using namespace cv;
using namespace std;
template<typename T>
T *PrepareFrame(Mat &in, uchar * dst, int &min, int &max) {
T *out = new T[in.rows * in.cols];
for (int i = 0; i < in.rows; ++i)
for (int j = 0; j < in.cols; ++j) {
Vec3b intensity = in.at<Vec3b>(i, j);//changing to grayscale while at it :D
out[i * (in.cols) + j] = (intensity.val[0]+intensity.val[1]+intensity.val[2])/3;
dst[i * (in.cols) + j]=0;//while at it, setting the resulting frame to 0
max=out[i * (in.cols) + j]>max?out[i * (in.cols) + j]:max;
min=out[i * (in.cols) + j]<min?out[i * (in.cols) + j]:min;
}
return out;
}
#define XY2I(Y,X,COLS) (((Y) * (COLS)) + (X))
// returns the gradient in the x direction
static inline long xGradient(uchar * image, long cols, long x, long y) {
return image[XY2I(y-1, x-1, cols)] +
2*image[XY2I(y, x-1, cols)] +
image[XY2I(y+1, x-1, cols)] -
image[XY2I(y-1, x+1, cols)] -
2*image[XY2I(y, x+1, cols)] -
image[XY2I(y+1, x+1, cols)];
}
// returns the gradient in the y direction
static inline long yGradient(uchar * image, long cols, long x, long y) {
return image[XY2I(y-1, x-1, cols)] +
2*image[XY2I(y-1, x, cols)] +
image[XY2I(y-1, x+1, cols)] -
image[XY2I(y+1, x-1, cols)] -
2*image[XY2I(y+1, x, cols)] -
image[XY2I(y+1, x+1, cols)];
}
void ProcessFrame(Mat frame,Mat* resultStorage ,bool sobel,int cols,int rows)
{
uchar * dst = new uchar[rows * cols];
int min=255, max=0;
uchar * src=PrepareFrame<uchar>(frame,dst,min,max);
for (int y = 1; y < rows-1; ++y){
for (int x = 1; x < cols-1; ++x){
if(sobel){
const long gx = xGradient(src, cols, x, y);
const long gy = yGradient(src, cols, x, y);
long sum = abs(gx) + abs(gy);
if (sum > 255) sum = 255;
else if (sum < 0) sum = 0;
dst[y*cols+x] = sum;
}else{
dst[y*cols+x] = 255 / (max - min)*(src[y*cols+x] - min);
}
}
}
(*resultStorage) = Mat(rows, cols, CV_8U, dst, Mat::AUTO_STEP);
}
int main(int argc, char* argv[])
{
if(argc != 5) {
cout << "Invalid arguments"<<endl<< "Example usage: " << argv[0] << " inputVideoPath outputVideoPath 2 sobel"<<" where 2 is the number of threads to use and sobel is the filter, otherwise contrast stretching"<<endl;
return(-1);
}
bool sobel=(string(argv[4])=="sobel");
int bufferSize=atoi(argv[3]);
VideoCapture cap(argv[1]);
VideoWriter vwr;
if (!cap.isOpened())
throw "Error when reading video file";
int cols=(int)cap.get(CV_CAP_PROP_FRAME_WIDTH),rows = (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size S = Size(cols,rows);
vwr.open(argv[2], CV_FOURCC('M','P','4','2'), cap.get(CV_CAP_PROP_FPS), S,false);
if (!vwr.isOpened())
throw "Error when opening the vide writer";
auto started = std::chrono::high_resolution_clock::now();
Mat workersFrames[bufferSize];
int wrkrCntr=0;
vector<thread*> workersThreads;
while (true)
{
Mat* frame = new Mat();
if(cap.read(*frame)){
workersFrames[wrkrCntr] = Mat();
workersThreads.push_back(new thread(ProcessFrame,(*frame), &(workersFrames[wrkrCntr]),sobel,cols,rows));
wrkrCntr++;//one frame = one worker
if (workersThreads.size()==bufferSize)//bs=4 -> instantiated 4 threads already idx[0->3]
{
//cout<<"flushing threads results"<<endl;
for (size_t x = 0; x < wrkrCntr; ++x)
{
try{
(*(workersThreads[x])).join();
}
catch (const std::exception& e) { cout<<e.what()<<endl; }
vwr.write(((workersFrames[x])));
}
wrkrCntr=0;
workersThreads.clear();
}
}
else{
//cout << "end of video file" << endl;
break;
}
delete frame;
}
if(wrkrCntr>0){
//cout<<"Final Flush "<<endl;
for(size_t i=0;i<wrkrCntr;i++){
(*(workersThreads[i])).join();
vwr.write(((workersFrames[i])));
}
}
vwr.release();
cap.release();
auto done = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(done-started).count()<<endl;
return 0;
}