-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorkeyerhsv.cpp
202 lines (157 loc) · 4.78 KB
/
colorkeyerhsv.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
#include "colorkeyerhsv.h"
#include <QDebug>
#include <QColor>
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
ColorKeyerHSV::ColorKeyerHSV()
: alpha(0)
, useMedian(false)
, useOpening(false)
, useMaskSmallRegions(false)
, handAnalyzer( new HandAnalyzer())
{
}
Mat ColorKeyerHSV::process(const Mat &input){
Mat flipped;
flip(input,flipped,1);
// convert BGR -> HSV
Mat hsvFrame;
cvtColor(flipped, hsvFrame, CV_BGR2HSV);
float h = hueValue;
float s = saturationValue;
qColor.setHsv(h, s, 255, 255);
QRgb toRgb = qColor.rgb();
int r = qRed(toRgb);
int g = qGreen(toRgb);
int b = qBlue(toRgb);
Scalar color(b,g,r);
// qDebug()<<"R:" <<r<<"G:"<<g<<"B:" <<b<<"HUT"<<h<<"SAT"<<s;
// perform color keying
// output: 1 channel image
Mat binaryMask = colorKeying(hsvFrame);
if (useMedian){
medianBlur(binaryMask, binaryMask, 5);
}
if (useOpening){
erode(binaryMask, binaryMask, Mat());
dilate(binaryMask, binaryMask, Mat());
}
if (useMaskSmallRegions){
binaryMask = maskSmallRegions(binaryMask);
}
Mat final;
// calculate center of mass
centerOfMass(binaryMask);
final = handAnalyzer->getResultMatFromMat(binaryMask);
Point linksOben(0, 0);
Point rechtsUnten( + 20, final.rows/2);
rectangle(final,linksOben,rechtsUnten,color,CV_FILLED);
return final;
}
Mat ColorKeyerHSV::colorKeying(Mat& hsvFrame){
// initialize Mat object for output
Mat output(hsvFrame.rows, hsvFrame.cols, CV_8UC1);
// qDebug() << output.type();
//int saturationToleranceInBounds =
for(int x = 0; x < hsvFrame.cols; x++){
for(int y = 0; y < hsvFrame.rows; y++){
Vec3b hsvPixel = hsvFrame.at<Vec3b>(y,x);
int hue = hsvPixel[0];
int saturation = hsvPixel[1];
hue *= 2;
if (x == hsvFrame.cols / 2){
if (y == hsvFrame.rows / 2){
}
}
// Maskierung und Schwerpunktsberechnung
bool isWhite = false;
if (hueTolerance > std::abs(hueValue - hue)){
if (saturationTolerance > std::abs(saturationValue - saturation)){
isWhite = true;
}
}
if (isWhite){
output.at<uchar>(y,x) = 255;
}
else{
output.at<uchar>(y,x) = 0;
}
}
}
return output;
}
cv::Mat ColorKeyerHSV::maskSmallRegions(cv::Mat& mask){
// copy input image; findContours() will modify its contents
Mat copyOfMask;
mask.copyTo(copyOfMask);
// find all regions
vector<vector<Point> >contours;
findContours(copyOfMask, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// find index of largest regionIndex
int maxArea = 0;
int maxAreaIndex = 0;
for(int i = 0; i < contours.size(); i++){
vector<Point>contour = contours[i];
int area = contourArea(contour);
if (maxArea < area){
maxArea = area;
maxAreaIndex = i;
}
}
// erase all smaller regions
for(int i = 0; i < contours.size(); i++){
if (i != maxAreaIndex){
drawContours(mask, contours, i, Scalar(0,0,0,0), CV_FILLED);
}
}
return mask;
}
void ColorKeyerHSV::centerOfMass(Mat& image){
int sumx = 0;
int sumy = 0;
int count = 0;
for(int x = 0; x < image.cols; x++){
for (int y = 0; y < image.rows; y++){
if (image.at<uchar>(y,x) == 255){
sumx += x;
sumy += y;
count++;
}
}
}
if (count > 0){
center = (1 - alpha) * center + alpha * Point(sumx/count, sumy/count);
}
}
void ColorKeyerHSV::drawCross(Mat& image, Point center, int length, Scalar color){
if(center.x > 0 && center.y > 0){
line(image, center-Point(0, length), center+Point(0,length), color, 1);
line(image, center-Point(length, 0), center+Point(length, 0), color, 1);
}
}
void ColorKeyerHSV::setHueValue(int value){
hueValue = value;
}
void ColorKeyerHSV::setHueTolerance(int value){
hueTolerance = value;
}
void ColorKeyerHSV::setSaturationTolerance(int value){
saturationTolerance = value;
//qDebug() << "saturationTolerance: " << value;
}
void ColorKeyerHSV::setSaturationValue(int value){
saturationValue = value;
//qDebug() << "saturationValue: " << value;
}
void ColorKeyerHSV::setAlpha(float alpha){
this->alpha = alpha;
}
void ColorKeyerHSV::setMedianEnable(bool enable){
this->useMedian = enable;
}
void ColorKeyerHSV::setOpeningEnable(bool enable){
this->useOpening = enable;
}
void ColorKeyerHSV::setMaskSmallRegions(bool enable){
this->useMaskSmallRegions = enable;
}