汽车颜色识别--更新版 (三)

2014-11-24 03:28:26 · 作者: · 浏览: 2
ax = bin_val;
ridx = k;
}
}
cout< for(int i=0;i {
float bin_val = cvQueryHistValue_1D(g_hist,i);
int intensity = cvRound(bin_val*hist_height/gmax_value);
cvRectangle(hist_image,cvPoint(i*scale,2*hist_height),cvPoint((i+1)*scale - 1, 2*hist_height - intensity),CV_RGB(0,255,0));
float k = i*255.f/hist_size;
if (gmax {
gmax = bin_val;
gidx = k;
}
}
cout< for(int i=0;i {
float bin_val = cvQueryHistValue_1D(b_hist,i);
int intensity = cvRound(bin_val*hist_height/bmax_value);
cvRectangle(hist_image,cvPoint(i*scale,3*hist_height),cvPoint((i+1)*scale - 1, 3*hist_height - intensity),CV_RGB(0,0,255));
float k = i*255.f/hist_size;
if (bmax {
bmax = bin_val;
bidx = k;
}
}
cout< cvShowImage("ZHIFANGTUr",hist_image);

//颜色判断及匹配
float velue[] = {ridx,gidx,bidx};
float dmin = 100000; //保存最小的距离
int colorIdx = 0;
for(int i=0 ; i {
float d = sqrt(pow(colorVelue[i][0]-velue[0],2)+pow(colorVelue[i][1]-velue[1],2)+pow(colorVelue[i][2]-velue[2],2));
if (dmin > d)
{
dmin = d;
colorIdx = i;
}
}
cout<<"colorIdx:"< if (( ridx>100 )&&( ridx<220 )&&( gidx>100 )&&( gidx < 220 )&&(bidx>100)&&(bidx<220)&&
abs(ridx-gidx)<30&&abs(ridx-bidx)<30&&(gidx-bidx)<30)
{
type = 12;
}
else
type = colorIdx;

//cvDestroyWindow("hsv");
cvReleaseImage(&R);
cvReleaseImage(&G);
cvReleaseImage(&B);
cvReleaseImage(&hist_image);
cvReleaseHist(&r_hist);
cvReleaseHist(&g_hist);
cvReleaseHist(&b_hist);
return type;
}

int main(int argc, _TCHAR* argv[])
{
cvNamedWindow( "color recognition", 1 );//建立视频窗口
IplImage* src = cvLoadImage("D:\\tu\\U17328_20091230235830314-2.jpg");

//读图像//
image = cvCreateImage( cvGetSize(src), 8, 3 );
//cvCopy(src,image);

cvResize(src,image);
cvShowImage( "color recognition", image );//显示视频和直方图
cvWaitKey(1000);
cvSetMouseCallback( "color recognition", on_mouse ); // 设置鼠标回调函数

////车牌区域选择函数//
for ( int i=0;i<4;i++ )
{
if ( mark )
{
cout<<"区域坐标:"<<"x:"< cout<<"ok"< }
cvWaitKey(1000);
cvRectangle( image,cvPoint(r.x,r.y ),cvPoint( r.x+r.width,r.y+r.height ),cvScalar(255,0,255) ,2, 8, 0);
cvShowImage( "color recognition", image );

}
cvShowImage( "color recognition", image );
cout<<"selection is ok"< // cvWaitKey(0);

//颜色匹配识别//
int j;
j = Color_difference_RGB(src,r); //返回颜色类型
cout<<"j:"< switch (j)
{
case -1:
cout<<"please select ROI first!"< break;
case 0:
cout<<"the colorof thiscar is:黑"< break;
case 1:
cout<<"the colorof thiscar is:白"< break;
case 2:
cout<<"the colorof thiscar is:红"< break;
case 3:
cout<<"the colorof thiscar is:红"< break;
case 4:
cout<<"the colorof thiscar is:绿"< break;
case 5:
cout<<"the colorof thiscar is:绿"< break;
case 6:
cout<<"the colorof thiscar is:蓝"< break;
case 7:
cout<<"the colorof thiscar is:蓝"< break;
case 8:
cout<<"the colorof thiscar is:黄"< break;
case 9:
cout<<"the colorof thiscar is:黄"< break;
case 10:
cout<<"the colorof thiscar is:棕"< break;
case 11 :
cout<<"the colorof thiscar is:灰"< break;
default:
cout<<"the colorof thiscar is:其他"< }
cvWaitKey(0);
cvReleaseImage(&image);
cvReleaseImage(&src);
cvDestroyWindow("color recognition");
return 0;
}