-
Notifications
You must be signed in to change notification settings - Fork 1
/
ihlsandnhs.cpp
215 lines (180 loc) · 5.58 KB
/
ihlsandnhs.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
#include "ihlsandnhs.h"
#include "math_utils.h"
#include "opencv/ml.h"
#include "opencv2/opencv.hpp"
#include <cmath>
#include <iostream>
using namespace cv;
using namespace std;
/**
* It calculates theta bases on the equation provided in Valentine thesis.
*
* The returned theta is radian.
*/
float retrieve_theta( unsigned int r, unsigned int g, unsigned int b )
{
float theta;
// The numerator part of equation
float numerator = r - (g * 0.5) - (b * 0.5);
// The denominator part of equation
float denominator = (r * r) + (g * g) + (b * b) - (r * g) - (r * b) - (g * b);
float temp = numerator / sqrtf(denominator);
theta = acos(temp);
return theta;
}
/**
* Calculating the hue value based on the blow formula:
*
* H = θ if B <= G
* H = 2 * pi − θ if B > G
*
* The return value is normalised between 0 to 255.
*/
float retrieve_normalised_hue( unsigned int r, unsigned int g, unsigned int b )
{
float hue;
if ( b <= g )
hue = retrieve_theta(r, g, b);
else
hue = (2 * M_PI) - retrieve_theta(r, g, b);
return hue * 255 / (2 * M_PI);
}
/**
* Luminance is calculated as:
*
* L = 0.2126R + 0.7152G + 0.0722B
*/
float retrieve_luminance( unsigned int r, unsigned int g, unsigned int b )
{
return (0.2126f * r) + (0.7152f * g) + (0.0722f * b);
}
/**
* Saturation is calculates as below:
*
* S = max(R, G, B) − min(R, G, B)
*/
float retrieve_saturation( unsigned int r, unsigned int g, unsigned int b )
{
float saturation;
unsigned int max = get_maximum(r, g, b);
unsigned int min = get_minimum(r, g, b);
saturation = max - min;
return saturation;
}
cv::Mat convert_rgb_to_ihls( cv::Mat rgb_image )
{
assert(rgb_image.channels() == 3);
cv::Mat ihls_image(rgb_image.rows, rgb_image.cols, CV_8UC3);
for ( int i = 0; i < rgb_image.rows; ++i )
{
const uchar* rgb_data = rgb_image.ptr<uchar> (i);
uchar* ihls_data = ihls_image.ptr<uchar> (i);
for ( int j = 0; j < rgb_image.cols; ++j )
{
unsigned int b = *rgb_data++;
unsigned int g = *rgb_data++;
unsigned int r = *rgb_data++;
if (r >= 180) {
*ihls_data++ = (uchar) retrieve_saturation(r, g, b);
*ihls_data++ = (uchar) retrieve_luminance(r, g, b);
*ihls_data++ = (uchar) retrieve_normalised_hue(r, g, b);
}
else {
r = 0;
g = 0;
b = 0;
*ihls_data++ = (uchar) retrieve_saturation(r, g, b);
*ihls_data++ = (uchar) retrieve_luminance(r, g, b);
*ihls_data++ = (uchar) retrieve_normalised_hue(r, g, b);
}
}
}
return ihls_image;
}
cv::Mat nhs_red_segmentation ( cv::Mat ihls_image)
{
assert(ihls_image.channels() == 3);
cv::Mat seg_red_image(ihls_image.rows, ihls_image.cols, CV_8UC3);
cv::Mat medianImage(ihls_image.rows, ihls_image.cols, CV_8UC3);
for ( int i = 0; i < ihls_image.rows; ++i )
{
const uchar* ihls_data = ihls_image.ptr<uchar> (i);
uchar* seg_data = seg_red_image.ptr<uchar> (i);
for ( int j = 0; j < ihls_image.cols; ++j )
{
unsigned int s = *ihls_data++;
*ihls_data++;
unsigned int h = *ihls_data++;
if ((h < 15 || (h > 183 && s > 16)))
{
*seg_data++ = 255;
*seg_data++ = 255;
*seg_data++ = 255;
} else {
*seg_data++ = 0;
*seg_data++ = 0;
*seg_data++ = 0;
}
}
}
medianBlur(seg_red_image, medianImage, 5);
return seg_red_image;
}
cv::Mat nhs_blue_segmentation ( cv::Mat ihls_image )
{
assert(ihls_image.channels() == 3);
cv::Mat seg_blue_image(ihls_image.rows, ihls_image.cols, CV_8UC3);
cv::Mat medianImage(ihls_image.rows, ihls_image.cols, CV_8UC3);
for ( int i = 0; i < ihls_image.rows; ++i )
{
const uchar* ihls_data = ihls_image.ptr<uchar> (i);
uchar* seg_data = seg_blue_image.ptr<uchar> (i);
for ( int j = 0; j < ihls_image.cols; ++j )
{
unsigned int s = *ihls_data++;
*ihls_data++;
unsigned int h = *ihls_data++;
if (h > 143 && s > 36) {
*seg_data++ = 255;
*seg_data++ = 255;
*seg_data++ = 255;
}
else {
*seg_data++ = 0;
*seg_data++ = 0;
*seg_data++ = 0;
}
}
}
medianBlur(seg_blue_image, medianImage, 5);
return seg_blue_image;
}
cv::Mat nhs_white_segmentation(cv::Mat ihls_image)
{
assert(ihls_image.channels() == 3);
cv::Mat seg_blue_image(ihls_image.rows, ihls_image.cols, CV_8UC3);
cv::Mat medianImage(ihls_image.rows, ihls_image.cols, CV_8UC3);
for ( int i = 0; i < ihls_image.rows; ++i )
{
const uchar* ihls_data = ihls_image.ptr<uchar> (i);
uchar* seg_data = seg_blue_image.ptr<uchar> (i);
for ( int j = 0; j < ihls_image.cols; ++j )
{
unsigned int s = *ihls_data++;
unsigned int l = *ihls_data++;
unsigned int h = *ihls_data++;
if (l >= 95 && l <= 100) {
*seg_data++ = 255;
*seg_data++ = 255;
*seg_data++ = 255;
}
else {
*seg_data++ = 0;
*seg_data++ = 0;
*seg_data++ = 0;
}
}
}
medianBlur(seg_blue_image, medianImage, 5);
return seg_blue_image;
}