// Note: 颜色分割:提取特定颜色
/////////////////////////////////////////////////////////////////////////////
void CColorSegDlg::ColorSegByHSV(IplImage* img)
// 提取特定颜色
{
//====================== 变量定义====================//
int x,y; //循环
//====================== 输入彩色图像信息====================//
IplImage* pSrc = NULL;
pSrc = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopyImage(img,pSrc);
int width = pSrc->width; //图像宽度
int height = pSrc->height; //图像高度
int depth = pSrc->depth; //图像位深(IPL_DEPTH_8U...)
int channels = pSrc->nChannels; //图像通道数(1、2、3、4)
int imgSize = pSrc->imageSize; //图像大小 imageSize = height*widthStep
int step = pSrc->widthStep/sizeof(uchar); //相邻行的同列点之间的字节数: 注意widthStep != width*nChannels (有字节填零补充)
uchar* data = (uchar *)pSrc->imageData;
int imageLen = width*height; //
//=========================================//
double B=0.0,G=0.0,R=0.0,H=0.0,S=0.0,V=0.0;
IplImage* dstColorSegByColor = cvCreateImage(cvGetSize(pSrc),IPL_DEPTH_8U,3);
IplImage* dstColorSegByColorGray = cvCreateImage(cvGetSize(pSrc),IPL_DEPTH_8U,1);
//CvFont font = cvFont( 1, 1 );
for (y=0; y<height; y++)
{
for ( x=0; x<width; x++)
{
// 获取BGR值
B = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels];
G = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels+1];
R = ((uchar*)(pSrc->imageData + y*pSrc->widthStep))[x*pSrc->nChannels+2];
// RGB-HSV
pMyColorSpace.RGB2HSV(R,G,B,H,S,V);
H = (360*H)/(2*PI);
// 黑白
//黑色
if(V<0.35)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 0; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 0; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 0; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 0; //R
}
//白色
if(S<0.15 && V>0.75)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 255; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 255; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 255; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 255; //R
}
//灰色
if(S<0.15 && 0.35<V && V<0.75)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 128; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 128; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 128; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 128; //R
}
// 彩色
if(V>=0.35 && S>=0.15)
{
//红色相近
if((H>=0 && H<15) || (H>=340 && H<360))
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 40; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 0; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 0; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 255; //R
}
//黄色相近
else if(H>=15 && H<75)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 80; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 0; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 255; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 255; //R
}
//绿色相近
else if(H>=75 && H<150)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 120; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
= 0; //B
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
= 255; //G
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
= 0; //R
}
//
//蓝色相近
else if(H>=185 && H<270)
{
((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
= 200; //灰度
((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]