Example Color Space

From BoofCV
Revision as of 08:52, 17 August 2017 by Peter (talk | contribs)

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

Example Code:


  • Color space

Relevant Examples:

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(UtilIO.pathExample("sunflowers.jpg"));

		// Convert input image into a BoofCV RGB image
		Planar<GrayF32> rgb = ConvertBufferedImage.convertFromPlanar(image, null,true, GrayF32.class);

		//---- convert RGB image into different color formats
		Planar<GrayF32> hsv = rgb.createSameShape();
		ColorHsv.rgbToHsv_F32(rgb, hsv);

		Planar<GrayF32> yuv = rgb.createSameShape();
		ColorYuv.yuvToRgb_F32(rgb, yuv);

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

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

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

		System.out.printf("Found YUV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n",