基于Opencv的HSV调节算法

bool CMyImage::HSVAddS(IplImage *img, double dH, double dS, double dV)
{
   bool  bResult = false;
   int   x, y;
   int   nValue;
   float fValue;
   int   nH, nS, nV;
   float fH, fS, fV;
   unsigned char *pLine = NULL;
   unsigned char *pPixel = NULL;
   float *pFloatPixel = NULL;
   CvRect rect = cvGetImageROI(img);
   switch (img->depth)
   {
   case IPL_DEPTH_8U:
      nH = (int) dH;
      nS = (int) dS;
      nV = (int) dV;
      pLine = (unsigned char*) (img->imageData + rect.y * img->widthStep + rect.x * img->nChannels);
      for (y = 0; y < rect.height; y++, pLine += img->widthStep)
      {
         for (pPixel = pLine, x = 0; x < rect.width; x++, pPixel += img->nChannels)
         {
            // 色调
            nValue = (int) pPixel[0] + nH;
            if (nValue > 180)
            {
               nValue -= 180;
            }
            else if (nValue < 0)
            {
               nValue += 180;
            }
            pPixel[0] = (unsigned char) nValue;
            // 饱和度
            nValue = (int) pPixel[1] + nS;
            if (nValue > 255)
            {
               nValue = 255;
            }
            else if (nValue < 0)
            {
               nValue = 0;
            }
            pPixel[1] = (unsigned char) nValue;
            // 纯度
            nValue = (int) pPixel[2] + nV;
            if (nValue > 255)
            {
               nValue = 255;
            }
            else if (nValue < 0)
            {
               nValue = 0;
            }
            pPixel[2] = (unsigned char) nValue;
         }
      }
      bResult = true;
      break;
   case IPL_DEPTH_32F:
      fH = (float) dH;
      fS = (float) dS;
      fV = (float) dV;
      pLine = (unsigned char*) (img->imageData + rect.y * img->widthStep + rect.x * img->nChannels);
      for (y = 0; y < rect.height; y++, pLine += img->widthStep)
      {
         pFloatPixel = (float*) pLine;
         for (pPixel = pLine, x = 0; x < rect.width; x++, pFloatPixel += img->nChannels)
         {
            // 色调
            fValue = pFloatPixel[0] + fH;
            if (fValue > 360)
            {
               fValue -= 360;
            }
            else if (fValue < 0)
            {
               fValue += 360;
            }
            pFloatPixel[0] = fValue;
            // 饱和度
            fValue = pFloatPixel[1] + fS;
            if (fValue > 1)
            {
               fValue = 1;
            }
            else if (fValue < 0)
            {
               fValue = 0;
            }
            pFloatPixel[1] = fValue;
            // 纯度
            fValue = pFloatPixel[2] + fV;
            if (fValue > 1)
            {
               fValue = 1;
            }
            else if (fValue < 0)
            {
               fValue = 0;
            }
            pFloatPixel[2] = fValue;
         }
      }      
      bResult = true;
      break;
   }
   return bResult;
}

// 图像色调调节
bool CMyImage::ImgAdjustHSV(IplImage *src, IplImage *dst, double dH, double dS, double dV)
{
   bool  bResult = false;
   if (src && dst)
   {
      if (src->depth == dst->depth &&
         src->nChannels == dst->nChannels &&
         src->width == dst->width &&
         src->height == dst->height)
      {
         int    nCOI = cvGetImageCOI(src);
         CvRect rect = cvGetImageROI(src);
         cvSetImageCOI(src, 0);
         cvSetImageCOI(dst, 0);
         cvSetImageROI(dst, rect);
         // 计算 HSV
         cvCvtColor(src, dst, CV_BGR2HSV);
         // 色调调节
         HSVAddS(dst, dH, dS, dV);
         // 还原图像
         cvCvtColor(dst, dst, CV_HSV2BGR);
         // 恢复 COI
         cvSetImageCOI(src, nCOI);
         cvSetImageCOI(dst, nCOI);
         bResult = true;
      }
   }
   return bResult;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值