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;
}
{
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;
}