/
analyzer.c
286 lines (243 loc) · 6.49 KB
/
analyzer.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include "cv.h"
#include "analyzer.h"
#include <time.h>
#include <opencv2/highgui/highgui.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
uchar *hsv_data = NULL;
IplImage *img_hsv = NULL;
int height,width,step,channels, i, j, k;
char key = 0;
int hues[100] = {};
int sats[100] = {};
int vals[100] = {};
unsigned int h_min, s_min, v_min, h_max, s_max, v_max=0;
unsigned int recorded_count = 0;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("usage: ./analyzer <cropped_image>\n");
exit(0);
}
IplImage* region = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR);
height = region->height;
width = region->width;
step = region->widthStep;
channels = region->nChannels;
img_hsv = convertRGBtoHSV(region);
hsv_data = (uchar *)img_hsv->imageData;
cvNamedWindow("window",CV_GUI_EXPANDED);
cvSetMouseCallback("window", my_mouse_callback, region);
cvShowImage("window",region);
while (key != 'q')
{
key = cvWaitKey(0);
}
printf("%d %d %d | %d %d %d\n", h_min,s_min,v_min, h_max, s_max, v_max);
cvDestroyAllWindows();
cvReleaseImage(®ion);
cvReleaseImage(&img_hsv);
return 0;
}
void my_mouse_callback( int event, int x, int y, int flags, void* param )
{
unsigned int hsv[3] = {};
for(k=0;k<channels;k++)
{
char c = 0;
if (k==0)
c = 'H';
else if (k==1)
c='S';
else
c='V';
hsv[k] = hsv_data[y*step+x*channels+k];
//printf("%c: %d\t", c, hsv[k]);
}
// Save <H,S,V>
if (event == CV_EVENT_LBUTTONDOWN)
{
hues[recorded_count] = hsv[0];
sats[recorded_count] = hsv[1];
vals[recorded_count] = hsv[2];
++recorded_count;
}
// Draw new image with whites in places that match
else if (event == CV_EVENT_RBUTTONDOWN)
{
h_min = getMinFromArray(hues,recorded_count);
s_min = getMinFromArray(sats,recorded_count);
v_min = getMinFromArray(vals,recorded_count);
h_max = getMaxFromArray(hues,recorded_count);
s_max = getMaxFromArray(sats,recorded_count);
v_max = getMaxFromArray(vals,recorded_count);
IplImage* img = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
cvInRangeS(img_hsv,cvScalar(h_min,s_min,v_min,0),cvScalar(h_max,s_max,v_max,0),img);
int count = cvCountNonZero(img);
printf("count: %d\n", count);
cvNamedWindow("secondwindow",CV_GUI_EXPANDED);
cvShowImage("secondwindow",img);
key = 0;
while (key != 's')
{
key = cvWaitKey(0);
}
memset(hues,0,recorded_count);
memset(sats,0,recorded_count);
memset(vals,0,recorded_count);
recorded_count=0;
cvReleaseImage(&img);
cvDestroyWindow("secondwindow");
}
//printf("\n");
}
unsigned int getMinFromArray(int arr[], int length)
{
int i=0;
unsigned int min=UINT_MAX;
for (i=0; i<length; ++i)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
unsigned int getMaxFromArray(int arr[], int length)
{
int i=0;
unsigned int max=0;
for (i=0; i<length; ++i)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
// Create a HSV image from the RGB image using the full 8-bits, since OpenCV only allows Hues up to 180 instead of 255.
// ref: "http://cs.haifa.ac.il/hagit/courses/ist/Lectures/Demos/ColorApplet2/t_convert.html"
// Remember to free the generated HSV image.
IplImage* convertRGBtoHSV(const IplImage *imageRGB)
{
float fR, fG, fB;
float fH, fS, fV;
const float FLOAT_TO_BYTE = 255.0f;
const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;
// Create a blank HSV image
IplImage *imageHSV = cvCreateImage(cvGetSize(imageRGB), 8, 3);
if (!imageHSV || imageRGB->depth != 8 || imageRGB->nChannels != 3) {
printf("ERROR in convertImageRGBtoHSV()! Bad input image.\n");
exit(1);
}
int h = imageRGB->height; // Pixel height.
int w = imageRGB->width; // Pixel width.
int rowSizeRGB = imageRGB->widthStep; // Size of row in bytes, including extra padding.
char *imRGB = imageRGB->imageData; // Pointer to the start of the image pixels.
int rowSizeHSV = imageHSV->widthStep; // Size of row in bytes, including extra padding.
char *imHSV = imageHSV->imageData; // Pointer to the start of the image pixels.
int y, x = 0;
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
// Get the RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
int bB = *(uchar*)(pRGB+0); // Blue component
int bG = *(uchar*)(pRGB+1); // Green component
int bR = *(uchar*)(pRGB+2); // Red component
// Convert from 8-bit integers to floats.
fR = bR * BYTE_TO_FLOAT;
fG = bG * BYTE_TO_FLOAT;
fB = bB * BYTE_TO_FLOAT;
// Convert from RGB to HSV, using float ranges 0.0 to 1.0.
float fDelta;
float fMin, fMax;
int iMax;
// Get the min and max, but use integer comparisons for slight speedup.
if (bB < bG) {
if (bB < bR) {
fMin = fB;
if (bR > bG) {
iMax = bR;
fMax = fR;
}
else {
iMax = bG;
fMax = fG;
}
}
else {
fMin = fR;
fMax = fG;
iMax = bG;
}
}
else {
if (bG < bR) {
fMin = fG;
if (bB > bR) {
fMax = fB;
iMax = bB;
}
else {
fMax = fR;
iMax = bR;
}
}
else {
fMin = fR;
fMax = fB;
iMax = bB;
}
}
fDelta = fMax - fMin;
fV = fMax; // Value (Brightness).
if (iMax != 0) { // Make sure it's not pure black.
fS = fDelta / fMax; // Saturation.
float ANGLE_TO_UNIT = 1.0f / (6.0f * fDelta); // Make the Hues between 0.0 to 1.0 instead of 6.0
if (iMax == bR) { // between yellow and magenta.
fH = (fG - fB) * ANGLE_TO_UNIT;
}
else if (iMax == bG) { // between cyan and yellow.
fH = (2.0f/6.0f) + ( fB - fR ) * ANGLE_TO_UNIT;
}
else { // between magenta and cyan.
fH = (4.0f/6.0f) + ( fR - fG ) * ANGLE_TO_UNIT;
}
// Wrap outlier Hues around the circle.
if (fH < 0.0f)
fH += 1.0f;
if (fH >= 1.0f)
fH -= 1.0f;
}
else {
// color is pure Black.
fS = 0;
fH = 0; // undefined hue
}
// Convert from floats to 8-bit integers.
int bH = (int)(0.5f + fH * 255.0f);
int bS = (int)(0.5f + fS * 255.0f);
int bV = (int)(0.5f + fV * 255.0f);
// Clip the values to make sure it fits within the 8bits.
if (bH > 255)
bH = 255;
if (bH < 0)
bH = 0;
if (bS > 255)
bS = 255;
if (bS < 0)
bS = 0;
if (bV > 255)
bV = 255;
if (bV < 0)
bV = 0;
// Set the HSV pixel components.
uchar *pHSV = (uchar*)(imHSV + y*rowSizeHSV + x*3);
*(pHSV+0) = bH; // H component
*(pHSV+1) = bS; // S component
*(pHSV+2) = bV; // V component
}
}
return imageHSV;
}