Example Color Space

From BoofCV
Revision as of 12:24, 25 December 2013 by Peter (talk | contribs) (Updated for v0.16)
Jump to navigationJump to search

Example of how to convert between different color spaces in BoofCV. Either the whole image can be coverted or individual values.

Example Code:

Concepts:

  • Color space

Relevant Applets:

Example Code

/**
 * Simple demonstration for converting between color spaces in BoofCV. Currently RGB, YUV, HSV, and YCbCr are
 * supported.
 *
 * @author Peter Abeles
 */
public class ExampleColorSpace {

	public static void main( String args[] ) {
		BufferedImage image = UtilImageIO.loadImage("../data/applet/sunflowers.jpg");

		// Convert input image into a BoofCV RGB image
		MultiSpectral<ImageFloat32> rgb = ConvertBufferedImage.convertFromMulti(image, null,true, ImageFloat32.class);

		//---- convert RGB image into different color formats
		MultiSpectral<ImageFloat32> hsv = new MultiSpectral<ImageFloat32>(ImageFloat32.class,rgb.width,rgb.height,3);
		ColorHsv.rgbToHsv_F32(rgb, hsv);

		MultiSpectral<ImageFloat32> yuv = new MultiSpectral<ImageFloat32>(ImageFloat32.class,rgb.width,rgb.height,3);
		ColorYuv.yuvToRgb_F32(rgb, yuv);

		//---- Convert individual pixels into different formats
		float[] pixelHsv = new float[3];
		ColorHsv.rgbToHsv(10,50.6f,120,pixelHsv);
		System.out.printf("Found RGB->HSV = %5.2f %5.3f %5.1f\n",pixelHsv[0],pixelHsv[1],pixelHsv[2]);

		float[] pixelRgb = new float[3];
		ColorHsv.hsvToRgb(pixelHsv[0],pixelHsv[1],pixelHsv[2],pixelRgb);
		System.out.printf("Found HSV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n",
				pixelRgb[0],pixelRgb[1],pixelRgb[2]);

		float[] pixelYuv = new float[3];
		ColorYuv.rgbToYuv(10,50.6f,120,pixelYuv);
		System.out.printf("Found RGB->YUV = %5.1f %5.1f %5.1f\n",pixelYuv[0],pixelYuv[1],pixelYuv[2]);

		ColorYuv.yuvToRgb(pixelYuv[0],pixelYuv[1],pixelYuv[2],pixelRgb);
		System.out.printf("Found YUV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n",
				pixelRgb[0],pixelRgb[1],pixelRgb[2]);
	}
}