-
Notifications
You must be signed in to change notification settings - Fork 0
/
Greenness.cpp
196 lines (162 loc) · 4.79 KB
/
Greenness.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
//
// Greenness module for determining greenness of plant
// Luntian
//
// Created by Jan Villarosa on 11/3/14.
// Copyright (c) 2014 Jan Villarosa. All rights reserved.
//
#include "Greenness.h"
Mat Greenness:: getResult(){
return result;
}
Scalar HSVtoRGBcvScalar(int H, int S, int V) {
int bH = H; // H component
int bS = S; // S component
int bV = V; // V component
float fH, fS, fV;
float fR, fG, fB;
const float FLOAT_TO_BYTE = 255.0f;
const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;
// Convert from 8-bit integers to floats
fH = (float)bH * BYTE_TO_FLOAT;
fS = (float)bS * BYTE_TO_FLOAT;
fV = (float)bV * BYTE_TO_FLOAT;
// Convert from HSV to RGB, using float ranges 0.0 to 1.0
int iI;
float fI, fF, p, q, t;
if( bS == 0 ) {
// achromatic (grey)
fR = fG = fB = fV;
}
else {
// If Hue == 1.0, then wrap it around the circle to 0.0
if (fH >= 1.0f)
fH = 0.0f;
fH *= 6.0; // sector 0 to 5
fI = floor( fH ); // integer part of h (0,1,2,3,4,5 or 6)
iI = (int) fH; // " " " "
fF = fH - fI; // factorial part of h (0 to 1)
p = fV * ( 1.0f - fS );
q = fV * ( 1.0f - fS * fF );
t = fV * ( 1.0f - fS * ( 1.0f - fF ) );
switch( iI ) {
case 0:
fR = fV;
fG = t;
fB = p;
break;
case 1:
fR = q;
fG = fV;
fB = p;
break;
case 2:
fR = p;
fG = fV;
fB = t;
break;
case 3:
fR = p;
fG = q;
fB = fV;
break;
case 4:
fR = t;
fG = p;
fB = fV;
break;
default: // case 5 (or 6):
fR = fV;
fG = p;
fB = q;
break;
}
}
// Convert from floats to 8-bit integers
int bR = (int)(fR * FLOAT_TO_BYTE);
int bG = (int)(fG * FLOAT_TO_BYTE);
int bB = (int)(fB * FLOAT_TO_BYTE);
// Clip the values to make sure it fits within the 8bits.
if (bR > 255)
bR = 255;
if (bR < 0)
bR = 0;
if (bG > 255)
bG = 255;
if (bG < 0)
bG = 0;
if (bB > 255)
bB = 255;
if (bB < 0)
bB = 0;
return Scalar(bB,bG,bR);
}
float Greenness::compareLCCValues(int H, int S, int V){
float val1 = abs(H-40);
float val2 = abs(H-45);
float val3 = abs(H-50);
float val4 = abs(H-70);
if(val1<val2 && val1<val3 && val1<val4){
if(val1 <= 2.5){
return 2;
}
else{
return 2.5;
}
}else if(val2<val1 && val2<val3 && val2<val4){
if(val2 <= 2.5){
return 3;
}
else{
return 3.5;
}
}else if(val3<val1 && val3<val2 && val3<val4){
if(val3 <= 10){
return 4;
}
else{
return 4.5;
}
}else{
return 5;
}
}
float Greenness::greenness(Mat im){
Mat rgbim = im.clone();
Mat3b image;
cvtColor(rgbim,image,CV_BGR2HSV);
int aveH = 0;
int aveS = 0;
int aveV = 0;
int pixelct = 0;
for (Mat3b::iterator it = image.begin(); it != image.end(); it++) {
Vec3b hsv = *it;
int H=hsv.val[0];
int S=hsv.val[1];
int V=hsv.val[2];
if((H != 0 && S != 0 && V != 255)){
pixelct++;
aveH+=H;
aveS+=S;
aveV+=V;
} else {
}
}
float lccval = compareLCCValues(aveH/pixelct,aveS/pixelct,aveV/pixelct);
lccval = floor(lccval * 1000.0) / 1000.0;
cvtColor(image, image, CV_HSV2BGR);
char name[30];
sprintf(name,"Average H=%d",aveH/pixelct);
putText(image,name, Point(10,20) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
sprintf(name,"Average S=%d",aveS/pixelct);
putText(image,name, Point(10,50) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
sprintf(name,"Average V=%d",aveV/pixelct);
putText(image,name, Point(10,80) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
sprintf(name,"Pixels=%d",pixelct);
putText(image,name, Point(10,110) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
sprintf(name,"LCC Value: %.1f", lccval);
putText(image,name, Point(10,140) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
rectangle(image, Point(10,150), Point(30,170), HSVtoRGBcvScalar(aveH/pixelct, aveS/pixelct, aveV/pixelct),CV_FILLED);
result = image;
return lccval;
}