Difference between revisions of "Example Color Space"
From BoofCV
Jump to navigationJump to search (Created page with "Example of how to convert between different color spaces in BoofCV. Either the whole image can be coverted or individual values. Example Code: * [https://github.com/lessthan...") |
m |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Example of how to convert between different color spaces in BoofCV. Either the whole image can be | Example of how to convert between different color spaces in BoofCV. Either the whole image can be converted or individual values. | ||
Example Code: | Example Code: | ||
* [https://github.com/lessthanoptimal/BoofCV/blob/v0. | * [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/main/java/boofcv/examples/imageprocessing/ExampleColorSpace.java ExampleColorSpace.java] | ||
Concepts: | Concepts: | ||
* Color space | * Color space | ||
Relevant | Relevant Examples: | ||
* [[ | * [[Example_Color_Segmentation| Color Segmentation]] | ||
= Example Code = | = Example Code = | ||
Line 20: | Line 20: | ||
*/ | */ | ||
public class ExampleColorSpace { | public class ExampleColorSpace { | ||
public static void main( String[] args ) { | |||
public static void main( String | BufferedImage image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("sunflowers.jpg")); | ||
BufferedImage image = UtilImageIO. | |||
// Convert input image into a BoofCV RGB image | // 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 | //---- convert RGB image into different color formats | ||
Planar<GrayF32> hsv = rgb.createSameShape(); | |||
ColorHsv. | ColorHsv.rgbToHsv(rgb, hsv); | ||
Planar<GrayF32> yuv = rgb.createSameShape(); | |||
ColorYuv. | ColorYuv.yuvToRgb(rgb, yuv); | ||
//---- Convert individual pixels into different formats | //---- Convert individual pixels into different formats | ||
float[] pixelHsv = new float[3]; | float[] pixelHsv = new float[3]; | ||
ColorHsv.rgbToHsv(10,50.6f,120,pixelHsv); | 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]); | System.out.printf("Found RGB->HSV = %5.2f %5.3f %5.1f\n", pixelHsv[0], pixelHsv[1], pixelHsv[2]); | ||
float[] pixelRgb = new float[3]; | float[] pixelRgb = new float[3]; | ||
ColorHsv.hsvToRgb(pixelHsv[0],pixelHsv[1],pixelHsv[2],pixelRgb); | 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", | System.out.printf("Found HSV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n", | ||
pixelRgb[0],pixelRgb[1],pixelRgb[2]); | pixelRgb[0], pixelRgb[1], pixelRgb[2]); | ||
float[] pixelYuv = new float[3]; | float[] pixelYuv = new float[3]; | ||
ColorYuv.rgbToYuv(10,50.6f,120,pixelYuv); | 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]); | 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); | 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", | System.out.printf("Found YUV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n", | ||
pixelRgb[0],pixelRgb[1],pixelRgb[2]); | pixelRgb[0], pixelRgb[1], pixelRgb[2]); | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 14:58, 17 January 2022
Example of how to convert between different color spaces in BoofCV. Either the whole image can be converted or individual values.
Example Code:
Concepts:
- 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.loadImageNotNull(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(rgb, hsv);
Planar<GrayF32> yuv = rgb.createSameShape();
ColorYuv.yuvToRgb(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]);
}
}