-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadedScaling.cpp
More file actions
139 lines (131 loc) · 2.93 KB
/
ThreadedScaling.cpp
File metadata and controls
139 lines (131 loc) · 2.93 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
#include <opencv2/opencv.hpp>
#include <pthread.h>
#include "ImageManipulator.hpp"
int getGrayPixel(int x, int y, cv::Mat & img)
{
return img.data[y * img.cols + x];
}
int avgSurrounds(int x, int y, cv::Mat & img)
{
//int avg_Gray = 0;
char kernel[3][3];
for (int i = 0 ; i < 3 ; ++i)
{
memset(kernel[i], -1, 3);
}
//kernel[0][0] = 1;
//kernel[0][2] = 1;
//kernel[2][0] = 1;
//kernel[2][2] = 1;
kernel[1][1] = 8;
int avg = 0;
int curr_x = 0, curr_y = 0;
int middle = 1;//floor(3 / 2);
for (int i = 0 ; i < 3 ; ++i)
{
for (int j = 0 ; j < 3 ; ++j)
{
if (i != middle)
{
curr_y = y - (i - middle);
}
else if (i == middle)
{
curr_y = y;
}
if (j != middle)
{
curr_x = x - (j - middle);
}
else if (j == middle)
{
curr_x = x;
}
avg += getGrayPixel(curr_x, curr_y, img) * kernel[i][j];
}
}
avg /= 1;
if (avg > 255)
avg = 255;
if (avg < 0)
avg = 0;
/*
avg_Gray += getGrayPixel(x - 1, y - 1, img);
avg_Gray += getGrayPixel(x, y - 1, img);
avg_Gray += getGrayPixel(x + 1, y - 1, img);
avg_Gray += getGrayPixel(x - 1, y, img);
avg_Gray += getGrayPixel(x, y, img);
avg_Gray += getGrayPixel(x + 1, y, img);
avg_Gray += getGrayPixel(x - 1, y + 1, img);
avg_Gray += getGrayPixel(x, y + 1, img);
avg_Gray += getGrayPixel(x + 1, y + 1, img);
avg_Gray /= 9;
if (avg_Gray > 255)
avg_Gray = 255;
if (avg_Gray < 0)
avg_Gray = 0;
*/
return avg;
}
void * averageImage(void * data)
{
cv::Mat * img = (cv::Mat*)data;
for (int y = 0 ; y < img->rows ; ++y)
{
for (int x = 0; x < img->cols ; ++x)
{
if (x > 0 && y > 0
&& x < img->cols - 1
&& y < img->rows - 1)
{
int avg = avgSurrounds(x, y, *img);
img->data[y * img->cols + x] = avg;
}
}
}
return (void*)img;
}
int main(void)
{
cv::VideoCapture cap(0);
int key = -1;
cv::Mat img;
cv::Mat *r = NULL, *g = NULL, *b = NULL;
ImageManipulator * imageManipulator = new ImageManipulator();
pthread_t p[3];
if (!cap.isOpened())
{
std::cout << "Unable to open device at index #0" << std::endl;
return 0;
}
while (key < 0)
{
cap >> img;
if (img.empty())
break;
if (r == NULL
&& b == NULL
&& g == NULL)
{
r = new cv::Mat(img.rows, img.cols, CV_8U);
g = new cv::Mat(img.rows, img.cols, CV_8U);
b = new cv::Mat(img.rows, img.cols, CV_8U);
}
imageManipulator->split(img, *r, *g, *b);
if (!pthread_create(&p[0], NULL, averageImage, r)
&& !pthread_create(&p[1], NULL, averageImage, g)
&& !pthread_create(&p[2], NULL, averageImage, b))
{
if (!pthread_join(p[0], (void**)&r)
&& !pthread_join(p[1], (void**)&g)
&& !pthread_join(p[2], (void**)&b))
{
imageManipulator->combine(img, *r, *g, *b);
}
}
cv::imshow("Original", img);
key = cv::waitKey(1);
}
cap.release();
return -1;
}