Algorithm/Vision Sample Code

RGB to Luv

빠릿베짱이 2015. 5. 8. 17:46
반응형
template<typename T>
inline T Limit(const T& value)
{
	return ( (value > 255) ? 255 : ((value < 0) ? 0 : value) );
}

void ColorSpiltAndMerge::ConvertIplImageToRGBData(IplImage *colorSrcImage, unsigned char *colorRawData)
{
	
	int imageWidth = colorSrcImage->width;
	int imageHeight = colorSrcImage->height;
	int channelNum = colorSrcImage->nChannels;

	register int x, y;

	for(y=0; y<imageHeight; y++)
	{
		for(x=0; x<imageWidth*channelNum; x += channelNum)
		{
			colorRawData[y*imageWidth*channelNum+x+2] = (unsigned char)colorSrcImage->imageData[y*colorSrcImage->widthStep+x+2]; ///< red
			colorRawData[y*imageWidth*channelNum+x+1] = (unsigned char)colorSrcImage->imageData[y*colorSrcImage->widthStep+x+1]; /// green 
			colorRawData[y*imageWidth*channelNum+x] = (unsigned char)colorSrcImage->imageData[y*colorSrcImage->widthStep+x]; /// blue
		}
	}

}

void ColorSpiltAndMerge::ConvertRGBDataToLUVData(IplImage *colorSrcImage, unsigned char *colorRawData, unsigned char *LImage, unsigned char *UImage, unsigned char *VImage)
{

	ConvertIplImageToRGBData(colorSrcImage, colorRawData);

	int imageWidth = colorSrcImage->width;
	int imageHeight = colorSrcImage->height;

	register int i, j;

	double x = 0;
	double y = 0;
	double z = 0;

	double x0 = 0.98072;
	double y0 = 1.00000;
	double z0 = 1.18225;

	double u0 = 0;
	double v0 = 0;

	double l = 0;
	double u = 0;
	double v = 0;

	for(i=0; i<imageHeight; i++)
	{
		for (j=0; j<imageWidth; j++)
		{
			///< 1. RGB -> XYZ 컬러 공간으로 변환 
			double r = colorRawData[i*imageWidth*3+2+j*3];
			double g = colorRawData[i*imageWidth*3+1+j*3];
			double b = colorRawData[i*imageWidth*3+j*3];

			double X = 0.607*r + 0.174*g + 0.201*b;
			double Y = 0.299*r + 0.587*g + 0.144*b;
			double Z = 0.000*r + 0.066*g + 1.117*b;

			///< 2. XYZ -> LUV 컬러 공간으로 변환
			if ((Y / y0) > 0.008856)  
			{
				y = pow ((Y / y0), (1.0 / 3.0));
			}
			else
			{
				y = (7.787 * (Y / y0)) + (16.0 / 116.0);
			}

			l = (116.0 * y) - 16.0;

			if ((X + (15.0 * Y) + (3.0 * Z)) == 0.0) 
			{
				u = 0.0;
				v = 0.0;
			} 
			else 
			{
				u0 = (4.0 * x0) / (x0 + (15.0 * y0) + (3.0 * z0));
				v0 = (9.0 * y0) / (x0 + (15.0 * y0) + (3.0 * z0));
				u  = (4.0 * X)  / (X  + (15.0 * Y)  + (3.0 * Z));
				v  = (9.0 * Y)  / (X  + (15.0 * Y)  + (3.0 * Z));
				u = 13.0 * l * (u - u0);
				v = 13.0 * l * (v - v0);		
			}	


			LImage[i*imageWidth+j] = (unsigned char)Limit(l);
			UImage[i*imageWidth+j] = (unsigned char)Limit(u);
			VImage[i*imageWidth+j] = (unsigned char)Limit(v);

		}
	}


}
반응형

'Algorithm > Vision Sample Code' 카테고리의 다른 글

Face Alignment, Warping, Tracking  (0) 2016.07.14
CVPR2015 Code & Paper Link  (0) 2015.06.17
Active Contour Model  (0) 2015.05.08
Face Alignment 관련 기술  (0) 2015.04.07
Vision Datasets  (0) 2015.03.31