-
Notifications
You must be signed in to change notification settings - Fork 14
/
colorSeg.cpp
179 lines (135 loc) · 4.28 KB
/
colorSeg.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
#include "traffic.h"
void showHist(Mat src)
{
Mat hsv_src,dst;
if( !src.data )
cout<<"The image data is null!"<<endl;
/// 分割成3个单通道图像 ( R, G 和 B )
vector<Mat> hsv_planes;
cvtColor(src,hsv_src,CV_BGR2HSV);
split( hsv_src, hsv_planes );
/// 设定bin数目
int histSize = 255;
/// 设定取值范围 ( R,G,B) )
float range[] = { 0, 255 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat h_hist, s_hist, v_hist;
/// 计算直方图:
calcHist( &hsv_planes[0], 1, 0, Mat(), h_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &hsv_planes[1], 1, 0, Mat(), s_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &hsv_planes[2], 1, 0, Mat(), v_hist, 1, &histSize, &histRange, uniform, accumulate );
// 创建直方图画布
int hist_w = 400; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) );
/// 将直方图归一化到范围 [ 0, histImage.rows ]
normalize(h_hist, h_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(s_hist, s_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(v_hist, v_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// 在直方图画布上画出直方图
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(h_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(h_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(s_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(s_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(v_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(v_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
}
/// 显示直方图
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage );
waitKey(5);
}
void rgb2hsi(int red, int green, int blue, int& hue, int& saturation, int& intensity )
{
double r,g,b;
double h,s,i;
double sum;
double minRGB,maxRGB;
double theta;
r = red/255.0;
g = green/255.0;
b = blue/255.0;
minRGB = ((r<g)?(r):(g));
minRGB = (minRGB<b)?(minRGB):(b);
maxRGB = ((r>g)?(r):(g));
maxRGB = (maxRGB>b)?(maxRGB):(b);
sum = r+g+b;
i = sum/3.0;
if( i<0.001 || maxRGB-minRGB<0.001 )
{
//this is a black image or grayscale image
//in this circumstance, hue is undefined, not zero
h=0.0;
s=0.0;
//return ;
}
else
{
s = 1.0-3.0*minRGB/sum;
theta = sqrt((r-g)*(r-g)+(r-b)*(g-b));
theta = acos((r-g+r-b)*0.5/theta);
if(b<=g)
h = theta;
else
h = 2*PI - theta;
if(s<=0.01)
h=0;
}
hue = (int)(h*180/PI);
saturation = (int)(s*100);
intensity = (int)(i*255);
}
IplImage* colorSegmentation(IplImage* inputImage)
{
int colorB,colorG,colorR;
int colorH,colorS,colorI;
int iWidth = inputImage->width;
int iHeight = inputImage->height;
int iInWidthStep = inputImage->widthStep;
int Green_num=0,Red_num=0;
IplImage* imageSeg = cvCreateImage(cvSize(iWidth,iHeight),IPL_DEPTH_8U,1);
if(!imageSeg)
exit(EXIT_FAILURE);
int iOutWidthStep = imageSeg->widthStep;//widthStep ±íʾ´æ´¢Ò»ÐÐÏñËØËùÐèµÄ×Ö½ÚÊý
unsigned char* in = (unsigned char*)inputImage->imageData;
unsigned char* out = (unsigned char*)imageSeg->imageData;
for(int j=0; j<iHeight; j++)
{
in = (unsigned char*)inputImage->imageData + j*iInWidthStep;
out = (unsigned char*)imageSeg->imageData + j*iOutWidthStep;
if(j<=ROIHeight)
for(int i=0; i<iWidth; i++){
colorB = in[3*i];
colorG = in[3*i+1];
colorR = in[3*i+2];
rgb2hsi(colorR,colorG,colorB,colorH,colorS,colorI);
//下面的参数参考论文: A Method to Search for Color Segmentation Threshold in Traffic Sign Detection
if((colorR-colorG)<150&&(colorR-colorG)>30&&(colorG-colorB)<50)
{
out[i]=GREEN_PIXEL_LABEL;
Green_num++;
}
else if((colorR-colorG<75)&&(colorG-colorB)<120)
{
out[i]=NON_BLOB_PIXEL_LABEL;
Red_num++;
}
else
out[i]=RED_PIXEL_LABEL;
}
}
/*
if((Green_num>Red_num)&&(Green_num>10))
return RESULT_G;
else if((Green_num<Red_num)&&(Red_num>10))
return RESULT_R;
else
return RESULT_NON;*/
return imageSeg;
}