颜色分割:提取特定颜色

 

// Note:  颜色分割:提取特定颜色
// Version: 5/11/2011 skyseraph/zhaobo  zgzhaobo@gmail.com
/
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>=150 && H<185)
    {
     ((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
      = 160;  //灰度
     ((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]
      = 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]
      = 255;  //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     
    } 
  // /* //洋红:270-340
    else if(H>=270 && H<340)
    {
     ((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
      = 220;  //灰度
     ((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
      = 255;  //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
    {
     ((uchar*)(dstColorSegByColorGray->imageData + y*dstColorSegByColorGray->widthStep))[x]
      = 180;  //灰度
     ((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels]
      = 128;  //B  //紫色Purple
     ((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+1]
      = 0;  //G
     ((uchar*)(dstColorSegByColor->imageData + y*dstColorSegByColor->widthStep))[x*dstColorSegByColor->nChannels+2]
      = 128;  //R
    }
   }
  }
 }
 //cvNamedWindow("src",1);
 //cvShowImage("src",pSrc);
 cvNamedWindow("dstColorSegByColor",1);
 cvShowImage("dstColorSegByColor",dstColorSegByColor);
 cvNamedWindow("dstColorSegByColorGray",1);
 cvShowImage("dstColorSegByColorGray",dstColorSegByColorGray);
 cvSaveImage(".\\dstColorSegByColor.jpg",dstColorSegByColor);
 cvSaveImage(".\\dstColorSegByColorGray.jpg",dstColorSegByColorGray);

 cvWaitKey(0);
 cvDestroyAllWindows();
 cvReleaseImage(&pSrc);
 cvReleaseImage(&dstColorSegByColor);
 cvReleaseImage(&dstColorSegByColorGray);

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值