ImageUtils.cpp
IplImage* convertImageHSVtoRGB(const IplImage *imageHSV)
{
	float fH, fS, fV;
	float fR, fG, fB;
	const float FLOAT_TO_BYTE = 255.0f;
	const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;

	// Create a blank RGB image
	IplImage *imageRGB = cvCreateImage(cvGetSize(imageHSV), 8, 3);
	if (!imageRGB || imageHSV->depth != 8 || imageHSV->nChannels != 3) {
		printf("ERROR in convertImageHSVtoRGB()! Bad input image.\n");
		exit(1);
	}

	int h = imageHSV->height;				// Pixel height
	int w = imageHSV->width;				// Pixel width
	int rowSizeHSV = imageHSV->widthStep;	// Size of row in bytes, including extra padding
	char *imHSV = imageHSV->imageData;		// Pointer to the start of the image pixels.
	int rowSizeRGB = imageRGB->widthStep;	// Size of row in bytes, including extra padding
	char *imRGB = imageRGB->imageData;		// Pointer to the start of the image pixels.
	for (int y=0; y<h; y++) {
		for (int x=0; x<w; x++) {
			// Get the HSV pixel components
			uchar *pHSV = (uchar*)(imHSV + y*rowSizeHSV + x*3);
			int bH = *(uchar*)(pHSV+0);	// H component
			int bS = *(uchar*)(pHSV+1);	// S component
			int bV = *(uchar*)(pHSV+2);	// V component

			// Convert from 8-bit integers to floats
			fH = (float)bH * BYTE_TO_FLOAT;
			fS = (float)bS * BYTE_TO_FLOAT;
			fV = (float)bV * BYTE_TO_FLOAT;

			// Convert from HSV to RGB, using float ranges 0.0 to 1.0
			int iI;
			float fI, fF, p, q, t;

			if( bS == 0 ) {
				// achromatic (grey)
				fR = fG = fB = fV;
			}
			else {
				//if (bH < 0 || bH >= 255 || bS < 0 || bS > 255 || bV < 0 || bV > 255) {
				//	cout << "ERROR: HSVi pixel(" << x << "," << y << ") is being clipped. " << bH << "," << bS << "," << bV << endl;
				//	cout << "ERROR: HSVf pixel(" << x << "," << y << ") is being clipped. " << fH << "," << fS << "," << fV << endl;
				//}

				// If Hue == 1.0, then wrap it around the circle to 0.0
				if (fH >= 1.0f)
					fH = 0.0f;

				fH *= 6.0;			// sector 0 to 5
				fI = floor( fH );		// integer part of h (0,1,2,3,4,5 or 6)
				iI = (int) fH;			//		"		"		"		"
				fF = fH - fI;			// factorial part of h (0 to 1)

				p = fV * ( 1.0f - fS );
				q = fV * ( 1.0f - fS * fF );
				t = fV * ( 1.0f - fS * ( 1.0f - fF ) );

				switch( iI ) {
					case 0:
						fR = fV;
						fG = t;
						fB = p;
						break;
					case 1:
						fR = q;
						fG = fV;
						fB = p;
						break;
					case 2:
						fR = p;
						fG = fV;
						fB = t;
						break;
					case 3:
						fR = p;
						fG = q;
						fB = fV;
						break;
					case 4:
						fR = t;
						fG = p;
						fB = fV;
						break;
					default:		// case 5 (or 6):
						fR = fV;
						fG = p;
						fB = q;
						break;
				}
			}

			// Convert from floats to 8-bit integers
			int bR = (int)(fR * FLOAT_TO_BYTE);
			int bG = (int)(fG * FLOAT_TO_BYTE);
			int bB = (int)(fB * FLOAT_TO_BYTE);

			// Clip the values to make sure it fits within the 8bits
			//if (bR > 255 || bR < 0 || bG > 255 || bG < 0 || bB > 255 || bB < 0) {
			//	cout << "Warning: RGB pixel(" << x << "," << y << ") is being clipped. " << bR << "," << bG << "," << bB << endl;
			//}
			if (bR > 255)
				bR = 255;
			if (bR < 0)
				bR = 0;
			if (bG > 255)
				bG = 255;
			if (bG < 0)
				bG = 0;
			if (bB > 255)
				bB = 255;
			if (bB < 0)
				bB = 0;

			// Set the RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
			uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
			*(pRGB+0) = bB;		// B component
			*(pRGB+1) = bG;		// G component
			*(pRGB+2) = bR;		// R component
		}
	}
	return imageRGB;
}
// Create an RGB image from the HSV image using the full 8-bits, since OpenCV only allows Hues up to 180 instead of 255.
// ref: "http://cs.haifa.ac.il/hagit/courses/ist/Lectures/Demos/ColorApplet2/t_convert.html"
// Remember to free the generated RGB image.

Create an RGB image from the HSV image using the full 8-bits, since OpenCV only allows Hues up to 180 instead of 255.
Create an RGB image from the HSV image using the full 8 - bits since OpenCV only allows Hues up to 180 instead of 255 .
IplImage* convertImageHSVtoRGB(const IplImage *imageHSV)
{
	float fH, fS, fV;
	float fR, fG, fB;
	const float FLOAT_TO_BYTE = 255.0f;
	const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;
	IplImage *imageRGB = cvCreateImage(cvGetSize(imageHSV), 8, 3);
	if (!imageRGB || imageHSV->depth != 8 || imageHSV->nChannels != 3) {
		printf("ERROR in convertImageHSVtoRGB()! Bad input image.\n");
		exit(1);
	}
	int h = imageHSV->height;				
	int w = imageHSV->width;				
	int rowSizeHSV = imageHSV->widthStep;	
	char *imHSV = imageHSV->imageData;		
	int rowSizeRGB = imageRGB->widthStep;	
	char *imRGB = imageRGB->imageData;		
	for (int y=0; y<h; y++) {
		for (int x=0; x<w; x++) {
			uchar *pHSV = (uchar*)(imHSV + y*rowSizeHSV + x*3);
			int bH = *(uchar*)(pHSV+0);	
			int bS = *(uchar*)(pHSV+1);	
			int bV = *(uchar*)(pHSV+2);	
			fH = (float)bH * BYTE_TO_FLOAT;
			fS = (float)bS * BYTE_TO_FLOAT;
			fV = (float)bV * BYTE_TO_FLOAT;
			int iI;
			float fI, fF, p, q, t;
			if( bS == 0 ) {
				fR = fG = fB = fV;
			}
			else {
				if (fH >= 1.0f)
					fH = 0.0f;
				fH *= 6.0;			
				fI = floor( fH );		
				iI = (int) fH;			
				fF = fH - fI;			
				p = fV * ( 1.0f - fS );
				q = fV * ( 1.0f - fS * fF );
				t = fV * ( 1.0f - fS * ( 1.0f - fF ) );
				switch( iI ) {
					case 0:
						fR = fV;
						fG = t;
						fB = p;
						break;
					case 1:
						fR = q;
						fG = fV;
						fB = p;
						break;
					case 2:
						fR = p;
						fG = fV;
						fB = t;
						break;
					case 3:
						fR = p;
						fG = q;
						fB = fV;
						break;
					case 4:
						fR = t;
						fG = p;
						fB = fV;
						break;
					default:		
						fR = fV;
						fG = p;
						fB = q;
						break;
				}
			}
			int bR = (int)(fR * FLOAT_TO_BYTE);
			int bG = (int)(fG * FLOAT_TO_BYTE);
			int bB = (int)(fB * FLOAT_TO_BYTE);
			if (bR > 255)
				bR = 255;
			if (bR < 0)
				bR = 0;
			if (bG > 255)
				bG = 255;
			if (bG < 0)
				bG = 0;
			if (bB > 255)
				bB = 255;
			if (bB < 0)
				bB = 0;
			uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
			*(pRGB+0) = bB;		
			*(pRGB+1) = bG;		
			*(pRGB+2) = bR;		
		}
	}
	return imageRGB;
}
IplImage * convertImageHSVtoRGB ( const IplImage * imageHSV ) { float fH , fS , fV ; float fR , fG , fB ; const float FLOAT_TO_BYTE = 255.0f ; const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE ; IplImage * imageRGB = cvCreateImage ( cvGetSize ( imageHSV ) , 8 , 3 ) ; if ( ! imageRGB || imageHSV -> depth != 8 || imageHSV -> nChannels != 3 ) { printf ( " \n " ) ; exit ( 1 ) ; } int h = imageHSV -> height ; int w = imageHSV -> width ; int rowSizeHSV = imageHSV -> widthStep ; char * imHSV = imageHSV -> imageData ; int rowSizeRGB = imageRGB -> widthStep ; char * imRGB = imageRGB -> imageData ; for ( int y = 0 ; y < h ; y ++ ) { for ( int x = 0 ; x < w ; x ++ ) { uchar * pHSV = ( uchar * ) ( imHSV + y * rowSizeHSV + x * 3 ) ; int bH = * ( uchar * ) ( pHSV + 0 ) ; int bS = * ( uchar * ) ( pHSV + 1 ) ; int bV = * ( uchar * ) ( pHSV + 2 ) ; fH = ( float ) bH * BYTE_TO_FLOAT ; fS = ( float ) bS * BYTE_TO_FLOAT ; fV = ( float ) bV * BYTE_TO_FLOAT ; int iI ; float fI , fF , p , q , t ; if ( bS == 0 ) { fR = fG = fB = fV ; } else { if ( fH >= 1.0f ) fH = 0.0f ; fH *= 6.0 ; fI = floor ( fH ) ; iI = ( int ) fH ; fF = fH - fI ; p = fV * ( 1.0f - fS ) ; q = fV * ( 1.0f - fS * fF ) ; t = fV * ( 1.0f - fS * ( 1.0f - fF ) ) ; switch ( iI ) { case 0 : fR = fV ; fG = t ; fB = p ; break ; case 1 : fR = q ; fG = fV ; fB = p ; break ; case 2 : fR = p ; fG = fV ; fB = t ; break ; case 3 : fR = p ; fG = q ; fB = fV ; break ; case 4 : fR = t ; fG = p ; fB = fV ; break ; default : fR = fV ; fG = p ; fB = q ; break ; } } int bR = ( int ) ( fR * FLOAT_TO_BYTE ) ; int bG = ( int ) ( fG * FLOAT_TO_BYTE ) ; int bB = ( int ) ( fB * FLOAT_TO_BYTE ) ; if ( bR > 255 ) bR = 255 ; if ( bR < 0 ) bR = 0 ; if ( bG > 255 ) bG = 255 ; if ( bG < 0 ) bG = 0 ; if ( bB > 255 ) bB = 255 ; if ( bB < 0 ) bB = 0 ; uchar * pRGB = ( uchar * ) ( imRGB + y * rowSizeRGB + x * 3 ) ; * ( pRGB + 0 ) = bB ; * ( pRGB + 1 ) = bG ; * ( pRGB + 2 ) = bR ; } } return imageRGB ; }
Create an RGB image from the HSV image using the full 8-bits, since OpenCV only allows Hues up to 180 instead of 255.
ref: "http://cs.haifa.ac.il/hagit/courses/ist/Lectures/Demos/ColorApplet2/t_convert.html"
Remember to free the generated RGB image.
Create an RGB image from the HSV image using the full 8 - bits since OpenCV only allows Hues up to 180 instead of 255 . ref : " http : // cs . haifa . ac . il / hagit / courses / ist / Lectures / Demos / ColorApplet2 / t_convert . html " Remember to free the generated RGB image .
// Create a blank RGB image
 // Pixel height
 // Pixel width
 // Size of row in bytes, including extra padding
 // Pointer to the start of the image pixels.
 // Size of row in bytes, including extra padding
 // Pointer to the start of the image pixels.
 // Get the HSV pixel components
 // H component
 // S component
 // V component
 // Convert from 8-bit integers to floats
 // Convert from HSV to RGB, using float ranges 0.0 to 1.0
 // achromatic (grey)
 //if (bH < 0 || bH >= 255 || bS < 0 || bS > 255 || bV < 0 || bV > 255) {
 //	cout << "ERROR: HSVi pixel(" << x << "," << y << ") is being clipped. " << bH << "," << bS << "," << bV << endl;
 //	cout << "ERROR: HSVf pixel(" << x << "," << y << ") is being clipped. " << fH << "," << fS << "," << fV << endl;
 //}
 // If Hue == 1.0, then wrap it around the circle to 0.0
 // sector 0 to 5
 // integer part of h (0,1,2,3,4,5 or 6)
 //		"		"		"		"
 // factorial part of h (0 to 1)
 // case 5 (or 6):
 // Convert from floats to 8-bit integers
 // Clip the values to make sure it fits within the 8bits
 //if (bR > 255 || bR < 0 || bG > 255 || bG < 0 || bB > 255 || bB < 0) {
 //	cout << "Warning: RGB pixel(" << x << "," << y << ") is being clipped. " << bR << "," << bG << "," << bB << endl;
 //}
 // Set the RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
 // B component
 // G component
 // R component
