public int UV { get; set; }
The vertical subsampling period of U.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;
using Leadtools.ImageProcessing;
public string outputFile = Path.Combine(LEAD_VARS.ImagesDir, "ColorConversion", "YuvParameters.bmp");
public void YuvParametersPropertyExample()
{
using (RasterCodecs codecs = new RasterCodecs())
{
// StartUp the ColorConversion.
RasterColorConverterEngine.Startup();
// Load the input image as BGR
string inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
RasterImage bgrImage = codecs.Load(inputFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);
int width = bgrImage.Width;
int height = bgrImage.Height;
// Get the YUV buffer
byte[] yuvBuffer = GetYuvBufferFromImage(bgrImage);
// Initialize the Rgb buffer array
byte[] rgbBuffer = new byte[yuvBuffer.Length];
//Byte ordering of the format; for Y41P:
// U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 Y7
// 0 1 2 3 4 5 6 7 8 9 10 11
// Put the Y component order first, then the U and V last as follows:
// Y positions: 1,3,5,7,8,9,10,11
// U positions: 0,4
// V positions: 2,6
int[] offset = { 1, 3, 5, 7, 8, 9, 10, 11, 0, 4, 2, 6 };
// Initialize a new ConversionParameters
ConversionParameters convParams = new ConversionParameters();
// Initialize YuvParameters
ConversionYuvParameters yuvParameters = ConversionYuvParameters.Empty;
yuvParameters.UH = 4; // Horizontal sub-sampling of U
yuvParameters.UV = 1; // Vertical sub-sampling of U
yuvParameters.VH = 4; // Horizontal sub-sampling of V
yuvParameters.VV = 1; // Vertical sub-sampling of V
yuvParameters.Offsets = offset; // // Byte ordering
yuvParameters.Range = ConversionYuvRange.UseFull; // YUV values range
// This represents the macro pixels(smallest group of pixels allowed),
// which indicates how many actual pixels are in the macro pixel.
// This value is important only in non - planar format
yuvParameters.MacroPixel = 8;
//This is a Boolean value that represents the type of the YUV format (Planar = true, or non - Planar = false.)
yuvParameters.Planar = false;
// set the yuv parameters
convParams.YuvParameters = yuvParameters;
// Determine the type of conversion to be used in the conversion, for YUVGeneric, only use UseBuiltIn
convParams.Method = ConversionMethodFlags.UseBuiltIn;
//Determine the type of conversion to be activated. For YUVGeneric, only UseBuiltIn
convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn;
// Initialize a new Converter object
RasterColorConverterEngine converter = new RasterColorConverterEngine();
//Initialize the conversion
converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Rgb, convParams);
// Convert the Buffer from Yuv to Rgb
converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0);
// Change the Yuv Parameters
convParams.Method = ConversionMethodFlags.ChangeYuv;
// Change the MacroPixel value
yuvParameters.Mask = ConversionYuvMaskFlags.MacroPixel;
// Reset the MacroPixel Property.
yuvParameters.MacroPixel = 16;
convParams.YuvParameters = yuvParameters;
// Convert the Buffer from Yuv to Rgb
converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0);
// Stop the conversion
converter.Stop();
// Save the converted Image
RasterImage yuvImage = GetImageFromYuvBuffer(yuvBuffer, width, height);
codecs.Save(yuvImage, outputFile, RasterImageFormat.Bmp, 24);
// Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown();
}
}
byte[] GetYuvBufferFromImage(RasterImage bgrImage)
{
// StartUp the ColorConversion.
RasterColorConverterEngine.Startup();
// Image buffer array
byte[] bgrBuffer = new byte[bgrImage.Width * bgrImage.Height * 3];
// Get image buffer
bgrImage.Access();
for (int i = 0; i < bgrImage.Height; i++)
bgrImage.GetRow(i, bgrBuffer, (i * bgrImage.Width * 3), bgrImage.Width * 3);
bgrImage.Release();
// Initialize a new Converter object
RasterColorConverterEngine converter = new RasterColorConverterEngine();
byte[] yuvBuffer = new byte[bgrBuffer.Length];
// Start the color conversion
converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Yuv, null);
// convert the image buffer
converter.Convert(bgrBuffer, // input buffer
0, // offset from the beginning of the source buffer
yuvBuffer, // output buffer
0, // offset from the beginning of the destination buffer
bgrImage.Width, // pixels width
bgrImage.Height, // pixels height
0, // 0 bytes align
0); // 0 bytes align
// stop the conversion
converter.Stop();
RasterColorConverterEngine.Shutdown();
return yuvBuffer;
}
RasterImage GetImageFromYuvBuffer(byte[] yuvBuffer, int width, int height)
{
// StartUp the ColorConversion.
RasterColorConverterEngine.Startup();
// Initialize a new Converter object
RasterColorConverterEngine converter = new RasterColorConverterEngine();
// Initialize an image to hold the converted buffer.
RasterImage yuvImage = null;
try
{
yuvImage = new RasterImage(RasterMemoryFlags.Conventional, width, height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);
// Start the color conversion
converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Bgr, null);
// convert the image buffer
converter.ConvertToImage(yuvBuffer, // converted buffer
0, // offset from the beginning of the source buffer
yuvImage, // image to be save
width, // pixels width
height, // pixels height
0, // 0 bytes align
0); // 0 bytes align
// stop the conversion
converter.Stop();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
// Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown();
return yuvImage;
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}