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

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

	int h = imageRGB->height;				// Pixel height
	int w = imageRGB->width;				// Pixel width
	int rowSizeRGB = imageRGB->widthStep;	// Size of row in bytes, including extra padding
	char *imRGB = imageRGB->imageData;		// Pointer to the start of the image pixels.
	int rowSizeHSV = imageHSV->widthStep;	// Size of row in bytes, including extra padding
	char *imHSV = imageHSV->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 RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
			uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
			int bB = *(uchar*)(pRGB+0);	// Blue component
			int bG = *(uchar*)(pRGB+1);	// Green component
			int bR = *(uchar*)(pRGB+2);	// Red component

			// Convert from 8-bit integers to floats
			fR = bR * BYTE_TO_FLOAT;
			fG = bG * BYTE_TO_FLOAT;
			fB = bB * BYTE_TO_FLOAT;

			// Convert from RGB to HSV, using float ranges 0.0 to 1.0
			float fDelta;
			float fMin, fMax;
			int iMax;
			// Get the min & max, but use integer comparisons for slight speedup
			if (bB < bG) {
				if (bB < bR) {
					fMin = fB;
					if (bR > bG) {
						iMax = bR;
						fMax = fR;
					}
					else {
						iMax = bG;
						fMax = fG;
					}
				}
				else {
					fMin = fR;
					fMax = fG;
					iMax = bG;
				}
			}
			else {
				if (bG < bR) {
					fMin = fG;
					if (bB > bR) {
						fMax = fB;
						iMax = bB;
					}
					else {
						fMax = fR;
						iMax = bR;
					}
				}
				else {
					fMin = fR;
					fMax = fB;
					iMax = bB;
				}
			}
			fDelta = fMax - fMin;
			fV = fMax;					// Value (Brightness).
			if (iMax != 0) {			// Make sure its not pure black.
				fS = fDelta / fMax;		// Saturation.
				float ANGLE_TO_UNIT = 1.0f / (6.0f * fDelta);	// Make the Hues between 0.0 to 1.0 instead of 6.0
				if (iMax == bR) {		// between yellow & magenta.
					fH = (fG - fB) * ANGLE_TO_UNIT;
				}
				else if (iMax == bG) {	// between cyan & yellow.
					fH = (2.0f/6.0f) + ( fB - fR ) * ANGLE_TO_UNIT;
				}
				else {					// between magenta & cyan.
					fH = (4.0f/6.0f) + ( fR - fG ) * ANGLE_TO_UNIT;
				}
				// Wrap outlier Hues around the circle.
				if (fH < 0.0f)
					fH += 1.0f;
				if (fH >= 1.0f)
					fH -= 1.0f;
			}
			else {
				// color is pure Black.
				fS = 0;
				fH = 0;	// undefined hue
			}

			// Convert from floats to 8-bit integers
			int bH = (int)(0.5f + fH * 255.0f);
			int bS = (int)(0.5f + fS * 255.0f);
			int bV = (int)(0.5f + fV * 255.0f);

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

			// Set the HSV pixel components
			uchar *pHSV = (uchar*)(imHSV + y*rowSizeHSV + x*3);
			*(pHSV+0) = bH;		// H component
			*(pHSV+1) = bS;		// S component
			*(pHSV+2) = bV;		// V component
		}
	}
	return imageHSV;
}
// Create a HSV image from the RGB 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 HSV image.

Create a HSV image from the RGB image using the full 8-bits, since OpenCV only allows Hues up to 180 instead of 255.
Create a HSV image from the RGB image using the full 8 - bits since OpenCV only allows Hues up to 180 instead of 255 .
IplImage* convertImageRGBtoHSV(const IplImage *imageRGB)
{
	float fR, fG, fB;
	float fH, fS, fV;
	const float FLOAT_TO_BYTE = 255.0f;
	const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;
	IplImage *imageHSV = cvCreateImage(cvGetSize(imageRGB), 8, 3);
	if (!imageHSV || imageRGB->depth != 8 || imageRGB->nChannels != 3) {
		printf("ERROR in convertImageRGBtoHSV()! Bad input image.\n");
		exit(1);
	}
	int h = imageRGB->height;				
	int w = imageRGB->width;				
	int rowSizeRGB = imageRGB->widthStep;	
	char *imRGB = imageRGB->imageData;		
	int rowSizeHSV = imageHSV->widthStep;	
	char *imHSV = imageHSV->imageData;		
	for (int y=0; y<h; y++) {
		for (int x=0; x<w; x++) {
			uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
			int bB = *(uchar*)(pRGB+0);	
			int bG = *(uchar*)(pRGB+1);	
			int bR = *(uchar*)(pRGB+2);	
			fR = bR * BYTE_TO_FLOAT;
			fG = bG * BYTE_TO_FLOAT;
			fB = bB * BYTE_TO_FLOAT;
			float fDelta;
			float fMin, fMax;
			int iMax;
			if (bB < bG) {
				if (bB < bR) {
					fMin = fB;
					if (bR > bG) {
						iMax = bR;
						fMax = fR;
					}
					else {
						iMax = bG;
						fMax = fG;
					}
				}
				else {
					fMin = fR;
					fMax = fG;
					iMax = bG;
				}
			}
			else {
				if (bG < bR) {
					fMin = fG;
					if (bB > bR) {
						fMax = fB;
						iMax = bB;
					}
					else {
						fMax = fR;
						iMax = bR;
					}
				}
				else {
					fMin = fR;
					fMax = fB;
					iMax = bB;
				}
			}
			fDelta = fMax - fMin;
			fV = fMax;					
			if (iMax != 0) {			
				fS = fDelta / fMax;		
				float ANGLE_TO_UNIT = 1.0f / (6.0f * fDelta);	
				if (iMax == bR) {		
					fH = (fG - fB) * ANGLE_TO_UNIT;
				}
				else if (iMax == bG) {	
					fH = (2.0f/6.0f) + ( fB - fR ) * ANGLE_TO_UNIT;
				}
				else {					
					fH = (4.0f/6.0f) + ( fR - fG ) * ANGLE_TO_UNIT;
				}
				if (fH < 0.0f)
					fH += 1.0f;
				if (fH >= 1.0f)
					fH -= 1.0f;
			}
			else {
				fS = 0;
				fH = 0;	
			}
			int bH = (int)(0.5f + fH * 255.0f);
			int bS = (int)(0.5f + fS * 255.0f);
			int bV = (int)(0.5f + fV * 255.0f);
			if (bH > 255)
				bH = 255;
			if (bH < 0)
				bH = 0;
			if (bS > 255)
				bS = 255;
			if (bS < 0)
				bS = 0;
			if (bV > 255)
				bV = 255;
			if (bV < 0)
				bV = 0;
			uchar *pHSV = (uchar*)(imHSV + y*rowSizeHSV + x*3);
			*(pHSV+0) = bH;		
			*(pHSV+1) = bS;		
			*(pHSV+2) = bV;		
		}
	}
	return imageHSV;
}
IplImage * convertImageRGBtoHSV ( const IplImage * imageRGB ) { float fR , fG , fB ; float fH , fS , fV ; const float FLOAT_TO_BYTE = 255.0f ; const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE ; IplImage * imageHSV = cvCreateImage ( cvGetSize ( imageRGB ) , 8 , 3 ) ; if ( ! imageHSV || imageRGB -> depth != 8 || imageRGB -> nChannels != 3 ) { printf ( " \n " ) ; exit ( 1 ) ; } int h = imageRGB -> height ; int w = imageRGB -> width ; int rowSizeRGB = imageRGB -> widthStep ; char * imRGB = imageRGB -> imageData ; int rowSizeHSV = imageHSV -> widthStep ; char * imHSV = imageHSV -> imageData ; for ( int y = 0 ; y < h ; y ++ ) { for ( int x = 0 ; x < w ; x ++ ) { uchar * pRGB = ( uchar * ) ( imRGB + y * rowSizeRGB + x * 3 ) ; int bB = * ( uchar * ) ( pRGB + 0 ) ; int bG = * ( uchar * ) ( pRGB + 1 ) ; int bR = * ( uchar * ) ( pRGB + 2 ) ; fR = bR * BYTE_TO_FLOAT ; fG = bG * BYTE_TO_FLOAT ; fB = bB * BYTE_TO_FLOAT ; float fDelta ; float fMin , fMax ; int iMax ; if ( bB < bG ) { if ( bB < bR ) { fMin = fB ; if ( bR > bG ) { iMax = bR ; fMax = fR ; } else { iMax = bG ; fMax = fG ; } } else { fMin = fR ; fMax = fG ; iMax = bG ; } } else { if ( bG < bR ) { fMin = fG ; if ( bB > bR ) { fMax = fB ; iMax = bB ; } else { fMax = fR ; iMax = bR ; } } else { fMin = fR ; fMax = fB ; iMax = bB ; } } fDelta = fMax - fMin ; fV = fMax ; if ( iMax != 0 ) { fS = fDelta / fMax ; float ANGLE_TO_UNIT = 1.0f / ( 6.0f * fDelta ) ; if ( iMax == bR ) { fH = ( fG - fB ) * ANGLE_TO_UNIT ; } else if ( iMax == bG ) { fH = ( 2.0f / 6.0f ) + ( fB - fR ) * ANGLE_TO_UNIT ; } else { fH = ( 4.0f / 6.0f ) + ( fR - fG ) * ANGLE_TO_UNIT ; } if ( fH < 0.0f ) fH += 1.0f ; if ( fH >= 1.0f ) fH -= 1.0f ; } else { fS = 0 ; fH = 0 ; } int bH = ( int ) ( 0.5f + fH * 255.0f ) ; int bS = ( int ) ( 0.5f + fS * 255.0f ) ; int bV = ( int ) ( 0.5f + fV * 255.0f ) ; if ( bH > 255 ) bH = 255 ; if ( bH < 0 ) bH = 0 ; if ( bS > 255 ) bS = 255 ; if ( bS < 0 ) bS = 0 ; if ( bV > 255 ) bV = 255 ; if ( bV < 0 ) bV = 0 ; uchar * pHSV = ( uchar * ) ( imHSV + y * rowSizeHSV + x * 3 ) ; * ( pHSV + 0 ) = bH ; * ( pHSV + 1 ) = bS ; * ( pHSV + 2 ) = bV ; } } return imageHSV ; }
Create a HSV image from the RGB 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 HSV image.
Create a HSV image from the RGB 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 HSV image .
// Create a blank HSV 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 RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
 // Blue component
 // Green component
 // Red component
 // Convert from 8-bit integers to floats
 // Convert from RGB to HSV, using float ranges 0.0 to 1.0
 // Get the min & max, but use integer comparisons for slight speedup
 // Value (Brightness).
 // Make sure its not pure black.
 // Saturation.
 // Make the Hues between 0.0 to 1.0 instead of 6.0
 // between yellow & magenta.
 // between cyan & yellow.
 // between magenta & cyan.
 // Wrap outlier Hues around the circle.
 // color is pure Black.
 // undefined hue
 // Convert from floats to 8-bit integers
 // Clip the values to make sure it fits within the 8bits
 //if (bH > 255 || bH < 0 || bS > 255 || bS < 0 || bV > 255 || bV < 0) {
 //	cout << "Warning: HSV pixel(" << x << "," << y << ") is being clipped. " << bH << "," << bS << "," << bV << endl;
 //}
 // Set the HSV pixel components
 // H component
 // S component
 // V component
